usual arithmetic promotions
Posted Jan 10, 2005 12:59 UTC (Mon) by
jejones3141 (guest, #27116)
In reply to:
Tone and correctness by BruceRamsay
Parent article:
grsecurity 2.1.0 and kernel vulnerabilities
>After a quick look at the bugs listed I have a few questions about some of
>the analysis. For example:
>>> if(len > sizeof(moxaBuff))
>> ^ signed int has only upper-bound checked
>>> return -EINVAL;
>On all systems I know of, sizeof() produces an unsigned number. In C,
>comparisons between unsigned numbers and signed numbers are done as
>unsigned comparisons. In fact, -1 > sizeof(moxaBuff) is true. Therefore
>the comment "signed int has only upper-bound checked" is incorrect. After
>the test we are guaranteed that 0 <= len <= sizeof(moxaBuff). (I am
>speaking about real world C implementations and not theoretically possible
>C compilers.)
Some pre-ANSI/ISO C compilers did "unsigned preserving" promotions,
and for them your analysis is correct. ANSI/ISO, however, has "value
preserving" promotions, i.e. the compiler tries to find a type that
will represent all possible values of the types of both operands of >
to widen the operands to. Pre-C9X, since 64-bit arithmetic isn't
mandated, given that size_t (the type that sizeof() yields) is an
unsigned 32-bit type, the compiler will fail to find such a type.
I _think_ it ends up using an unsigned 32-bit integer. If C9X, which
requires 64-bit integer types, follows through with value preserving
rules (and size_t is still 32-bit), it may well do the comparison
in 64-bit signed integers, in which case -1 will not be greater than
sizeof(moxaBuff).
(
Log in to post comments)