Cast alignment warnings
Russ Allbery
rra at stanford.edu
Mon Aug 1 21:43:09 UTC 2011
Russ Allbery <rra at stanford.edu> writes:
> 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.
Also, I think this is only going to work on little-endian systems, isn't
it? Otherwise, it's always going to write 0 into 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