#344 packed structure access broken

4.6.x_development
closed-duplicate
gcc (183)
5
2013-01-31
2013-01-31
Dan
No

Compile the following code without any optimization. This is minimized code from a huge project to demonstrate the issue.
'TA.Handle' of the strcuture gets overwritten by LSB byte of TA.Count varaiable after calling SetCount (...) func.
as shown below.
This minimal code works OK with -Os, however we're facing the same issue as described above in the big project even with -Os enabled.
Tested on windows xp, msp430-gcc 4.6.3 and 4.7.0

console output:
Before
Handle = 0
Count = 199

After
Handle = 199
Count = 0

-------------------------------------
#include <stdio.h>

#include "config.h"
#include <msp430.h>
#include "delay.h"
#include "usart.h"
#include "serial.h"
#include "timers.h"

typedef struct
{
uint8 Handle;
uint32 Count;
} __attribute__ ((packed)) TADeviceType;

static void SetHandle (uint8 A8, TADeviceType *T)
{
uint8* PUint8 = &T->Handle;
*PUint8 = A8;
}

static void SetCount (uint32 A32, TADeviceType *T)
{
uint32* PUint32 = &T->Count;
*PUint32 = A32;
}

static TADeviceType TA;

int main(void)
{
WDTCTL = WDTPW + WDTHOLD;
DCOCTL = CALDCO_8MHZ;
BCSCTL1 = CALBC1_8MHZ;

USART0Init (USART0_BAUD_DIV(115200),
USART0_MODULATOR_VALUE(115200),
USART_8N1);
TimerInit ( );

_BIS_SR(GIE);

TA.Handle = 0;
TA.Count = 199;

printf ("\n\nBefore\n");
printf ("Handle = %d\n", TA.Handle);
printf ("Count = %ld\n", TA.Count);

SetCount ((uint32) 199, &TA);

printf ("\nAfter\n");
printf ("Handle = %d\n", TA.Handle);
printf ("Count = %ld\n", TA.Count);

return 0;
}

Discussion

  • Peter A. Bigot
    Peter A. Bigot
    2013-01-31

    • status: open --> closed-duplicate
     
  • Peter A. Bigot
    Peter A. Bigot
    2013-01-31

    Duplicates 3602798.