Treat Warnings as Errors


If you are reading this blog and have even a iotic sense of humour, you must have heard (and liked) this joke:


A man is smoking a cigarette and blowing smoke rings into the air. His girlfriend becomes irritated with the smoke and says, “Can’t you see the warning on the cigarette pack? Smoking is hazardous to your health!”

To which the man replies, “I am a programmer. We don’t worry about warnings; we only worry about errors.”


Ofcourse, I laughed my ass off when I first heard the joke. Awesome it was! But then, the last month totally changed my perception. (Probably because I faced the side-effects of that).

So, ARM architecture is very specific about alignment. It hates mis-aligned access (Its the side-effect of being an arch for embedded systems). Here is an example of unaligned access:

1 short short_arr[4];
2 long *long_ptr = short_arr;
3 blah = *long_ptr;

since the data type of short_arr is "short", gcc forces only a 16-bit alignment on the array (on 32-bit systems). but then, long_ptr is probably 32-bit and that screws up alignment for line 3. Although, i386 would work perfectly fine (but require one extra memory cycle), ARM will just give garbage data (if misaligned).

And many many many software packages are filled with such errors. Just to give you an example:
# cat /proc/cpu/alignment
User: 1265555

(this file is present only an ARM architecture because of ARMs peculiar alignment requirements)

See the number of alignment errors?
Packages like mhash (which calculate hash) build fine but fail run-time, this makes such errors difficult to track down.

So, always make it a point to compile your C programs with "-Werror" option. It treat warnings as errors. It is *very* important for your programs to work on just about any platform. Please pay attention to warnings, because warnings are there FOR A REASON. gcc developers aren’t stupid!
(Although sometimes, you might be sure that even if gcc warns about misalignment, the access is aligned. eg, when you typecast an IP struct into a MAC struct, etc. These are the *only* times you can let go off the -Werror option)

But, until this point sinks into the mind of every developer, there’s no option but to live in this mess. 😦


3 Responses to “Treat Warnings as Errors”

  1. Kedar Says:

    May be I am misunderstanding something, but will the example above always lead to unaligned accesses? Usually it will align the array to the word boundary, and hence it should NOT lead to unaligned access while accessing through a word ptr.

    Although if you did something like

    long_ptr = &short_arr[1];

    that should certainly be an unaligned access since it will most likely be on a half-word boundary instead of a word-boundary.

  2. Jitesh Says:

    kedar: the short array is aligned to half-word. So, if the ARM processors have the ability to access half-word addresses, a valid value will be returned (although it will cause a trap to the kernel and we can see the count in /proc/cpu/alignment increasing). (and yes, the example I gave is *not* foolproof.. but with a bit of help from the context, like adding “char tp” before that statement, it can cause an unaligned access.)

    Also, in the example you gave, if short_arr is only half-word aligned, then, short_arr[1] will be word aligned and there will be no alignment error reported.

    I did a printf(“%u”, short_arr); in the code, here is the address: 2130183914. It is only half-word aligned.

  3. chantix Says:


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: