cproc

Unnamed repository; edit this file 'description' to name the repository.
git clone git://git.nihaljere.xyz/cproc
Log | Files | Refs | Submodules | README | LICENSE

commit 1712e38057b824f79002011f0ca074b802f9b953
parent c683dbd6e4da3a6aba22779c99810f8684c38208
Author: Michael Forney <mforney@mforney.org>
Date:   Thu, 18 Apr 2019 12:11:01 -0700

Fix potential overflow of bit-field initializers into following member

Fixes #46. Thanks to Andrew Chambers for the bug report.

Diffstat:
Mqbe.c | 7++++++-
Atest/bitfield-initializer-overflow.c | 3+++
Atest/bitfield-initializer-overflow.qbe | 1+
3 files changed, 10 insertions(+), 1 deletion(-)

diff --git 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); diff --git a/test/bitfield-initializer-overflow.c b/test/bitfield-initializer-overflow.c @@ -0,0 +1,3 @@ +struct { + unsigned : 30, a : 2, b; +} s = {5}; diff --git a/test/bitfield-initializer-overflow.qbe b/test/bitfield-initializer-overflow.qbe @@ -0,0 +1 @@ +export data $s = align 4 { z 3, b 64, z 4 }