Cast alignment warnings

Russ Allbery rra at stanford.edu
Mon Aug 1 21:41:00 UTC 2011


Julien ÉLIE <julien at trigofacile.com> writes:

> We have in timecaf/timehash an unsigned char class that is casted to an
> unsigned int* for the sake of sscanf().

>     n = sscanf(path, "timecaf-%02x/%02x/%04x.CF", (unsigned int*)&class, &t1, &t2);

> I bet it is not a problem either -- because a char is 1-byte aligned, so
> it can be cast to any other types, especially a 4-byte aligned int.
> Please correct me if I am wrong.

Actually, I think that one *is* a problem; the address of class is not
guaranteed to be aligned, since it's a char, which means that sscanf may
do an unaligned store of an integer.  It's also wrong for other reasons:
integers are probably either four or eight bytes, so sscanf is going to
write at least four bytes at the address of class, which is going to
blithely overwrite neighboring variables on the stack.

That looks like a real bug.  The code needs to declare a temporary
unsigned int variable, sscanf into it, and then store the results in
class.

-- 
Russ Allbery (rra at stanford.edu)             <http://www.eyrie.org/~eagle/>

    Please send questions to the list rather than mailing me directly.
     <http://www.eyrie.org/~eagle/faqs/questions.html> explains why.



More information about the inn-workers mailing list