From 8111946731a0e12462328a51b8d751f3ea58a095 Mon Sep 17 00:00:00 2001 From: acqn Date: Sun, 15 Oct 2023 15:53:03 +0800 Subject: [PATCH 1/2] Fixed array subscript with a bit-field with patch by kugelfuhr. --- src/cc65/expr.c | 5 +++-- test/val/bug2186.c | 39 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+), 2 deletions(-) create mode 100644 test/val/bug2186.c diff --git a/src/cc65/expr.c b/src/cc65/expr.c index 8b0824c31..aef0679d4 100644 --- a/src/cc65/expr.c +++ b/src/cc65/expr.c @@ -3001,8 +3001,9 @@ static void parseadd (ExprDesc* Expr, int DoArrayRef) } if (!AddDone) { - if (ED_IsLocQuasiConst (&Expr2) && - rscale == 1 && + if (ED_IsLocQuasiConst (&Expr2) && + !IsTypeBitField (Expr2.Type) && + rscale == 1 && CheckedSizeOf (rhst) == SIZEOF_CHAR) { /* Change the order back */ RemoveCode (&Mark); diff --git a/test/val/bug2186.c b/test/val/bug2186.c new file mode 100644 index 000000000..143111d97 --- /dev/null +++ b/test/val/bug2186.c @@ -0,0 +1,39 @@ +/* Bug #2186 - Wrong array indexing when index comes from bit-field */ + +#include + +unsigned failures; + +typedef struct { + char flag : 1; + char index : 7; +} weird_type; + +const char array[] = { '6', '5', '0', '2' }; + +weird_type data; + +int main(void) { + data.flag = 1; + + data.index = 0; + if (array[data.index] != array[0]) + { + ++failures; + printf("Got '%c', expected '%c'\n", array[data.index], array[0]); + } + + data.index = 1; + if (array[data.index] != array[1]) + { + ++failures; + printf("Got '%c', expected '%c'\n", array[data.index], array[1]); + } + + if (failures > 0) + { + printf("Failures: %u\n", failures); + } + + return failures; +} \ No newline at end of file From 3e602682438c6916d6ae0e50d833d026f8201ef7 Mon Sep 17 00:00:00 2001 From: Bob Andrews Date: Tue, 17 Oct 2023 20:39:15 +0200 Subject: [PATCH 2/2] add newline at the end --- test/val/bug2186.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/val/bug2186.c b/test/val/bug2186.c index 143111d97..24546fb3d 100644 --- a/test/val/bug2186.c +++ b/test/val/bug2186.c @@ -36,4 +36,4 @@ int main(void) { } return failures; -} \ No newline at end of file +}