This is not 'Nam. This is C. There are rules.

Nov 29, 2006 16:42

I was going about my day earlier, when spot sent me an IM asking for help with one of my favorite programs. Apparently, it was failing quite spectacularly on sparc, and he was having some trouble making the machine give useful debug data.

After a bit of hacking on it, I found this wonderful nugget: ( (cut for those that don't care) )

Leave a comment

Comments 7

ndykman November 29 2006, 22:19:46 UTC
Wow. I've been in my PhD program too long. None of this makes sense. Of course, this

&pl->pd

makes me itch. Parens are your friends!

Reply

deviant_ November 29 2006, 23:08:14 UTC
Nah, that part's fine.

The real problem here is that what you wind up doing is

char buf[1024];
struct lose {
uint64_t x;
} *s;
uint64_t x;

s = (struct lose *)buf[1];
x = *s;
About which the C Standard sez:A pointer to an object or incomplete type may be converted to a pointer to a different object or incomplete type. If the resulting pointer is not correctly aligned for the pointed-to type, the behavior is undefined.
So really an implementation can do whatever it wants here. Most implementations just let it work. On SPARC you get a SIGBUS and your program dumps core and goes away.

Reply

ndykman November 30 2006, 02:34:28 UTC
Check. I am in awe of your C foo. Can gcc warn about this if you crank up the warning levels?

Reply

deviant_ November 30 2006, 07:24:25 UTC
Not really -- it only happens when you use type-casting, which is really telling the compiler "I know what I'm doing, don't worry about this part".

The irony is wonderful. Too bad about the result.

Reply


fezzgig December 1 2006, 04:32:38 UTC
Best subject-line evar.

Evar.

Reply


Leave a comment

Up