Part Number: RM57L843
#include <stdbool.h>
#include <stdint.h>
typedef struct {
bool field_a : 1; // bit 0
unsigned int : 7; // bits 1-7 (anonymous padding)
} __attribute__((packed)) packed_t;
typedef union {
packed_t s;
uint8_t byte;
} test_t;
/* At -O0, result.byte may contain garbage in bits 1-7.
* At -O1 or higher, result.byte is correct.
* Naming the padding (e.g., "unsigned int reserved : 7")
* also produces correct results at -O0.
*/
test_t make(bool val) {
test_t result = { .s.field_a = val };
return result;
}
tiarmclang -mcpu=cortex-r5 -mthumb -O0 -std=c17 -S anonimous_members_bug.c -o anonimous_members_bug_O0.s
tiarmclang -mcpu=cortex-r5 -mthumb -O1 -std=c17 -S anonimous_members_bug.c -o anonimous_members_bug_O1.s
make:
sub sp, #4
strb.w r0, [sp, #2] @ save parameter
ldrb.w r0, [sp, #2] @ reload parameter
and r1, r0, #1 @ extract bit 0
ldrb.w r0, [sp, #3] @ BUG: read UNINITIALIZED byte from stack
and r0, r0, #254 @ clear only bit 0, preserve bits 1-7 (garbage)
add r0, r1 @ set bit 0
strb.w r0, [sp, #3] @ write back (bits 1-7 still garbage)
ldrb.w r0, [sp, #3] @ return value contains garbage in bits 1-7
add sp, #4
bx lr