aboutsummaryrefslogtreecommitdiff
path: root/qbe.c
diff options
context:
space:
mode:
authorMichael Forney <mforney@mforney.org>2019-04-18 12:11:01 -0700
committerMichael Forney <mforney@mforney.org>2019-04-18 12:12:12 -0700
commit1712e38057b824f79002011f0ca074b802f9b953 (patch)
treee955abec236950475c81ce558ec91bdc23d0510b /qbe.c
parentc683dbd6e4da3a6aba22779c99810f8684c38208 (diff)
Fix potential overflow of bit-field initializers into following member
Fixes #46. Thanks to Andrew Chambers for the bug report.
Diffstat (limited to 'qbe.c')
-rw-r--r--qbe.c7
1 files changed, 6 insertions, 1 deletions
diff --git a/qbe.c b/qbe.c
index c496c41..deb5520 100644
--- a/qbe.c
+++ b/qbe.c
@@ -1266,7 +1266,12 @@ emitdata(struct decl *d, struct init *init)
bits |= cur->expr->constant.i << cur->bits.before % 8;
for (offset = start; offset < end; ++offset, bits >>= 8)
printf("b %u, ", (unsigned)bits & 0xff);
- bits &= 0xff >> cur->bits.after % 8;
+ /*
+ clear the upper `after` bits in the last byte,
+ or all bits when `after` is 0 (we ended on a
+ byte boundary).
+ */
+ bits &= 0x7f >> (cur->bits.after + 7) % 8;
} else {
printf("%c ", cur->expr->type->kind == TYPEARRAY ? cur->expr->type->base->repr->ext : cur->expr->type->repr->ext);
dataitem(cur->expr, cur->end - cur->start);