0

I'm porting a huge application from PowerPC to ARM. I'd decided to use gcc option -Wcast-align to detect the instances that might cause alignment issues. But then, I thought about a situation that the -Wcast-align option won't detect.

Eg:

typedef struct sample_s {
  uint8_t c;
  double i;
} __attribute__((packed)) sample_t;

void main ( void ) {
  double *i;
  sample_t  check;
  check.i = 2;
  i = &check.i;
  printf("i= %a\n", *i);
}

ARM will raise an alignment exception for the above code, but -Wcast-align won't report this. Is there any other way to detect such instances?

Nisse Engström
  • 4,738
  • 23
  • 27
  • 42
linuxfreak
  • 2,068
  • 3
  • 20
  • 29
  • I see your concern but in your example you don't think compiler will pad another 4 bytes after c in sample_t structure? – auselen Oct 27 '14 at 07:54
  • `grep -r "((packed))" .`? – Notlikethat Oct 27 '14 at 09:11
  • @auselen - No, I don't think the compiler will pad bytes after c. After all, that's the whole point of using packed attribute, right? – linuxfreak Oct 27 '14 at 11:29
  • @Notlikethat - I can find the packed structures using grep command. But not all packed structures will have alignment issues. Only the ones that are accessed as in the above program will raise exceptions. I just want to know if there's any other way to find such instances. – linuxfreak Oct 27 '14 at 11:34
  • 1
    One thing you can do is grep through for all uses of things like `alignment((packed))`, and any casts. Those are flags that may indicate non-portable behaviour nearby. – M.M Oct 27 '14 at 11:35
  • See related: http://stackoverflow.com/questions/8568432/is-gccs-attribute-packed-pragma-pack-unsafe – Mark Nunberg Oct 27 '14 at 15:55

0 Answers0