[ovs-discuss] [PATCH 1/2] Cleanup isdigit() warnings.

Ben Pfaff blp at nicira.com
Tue Aug 25 18:46:21 UTC 2009


Justin Pettit <jpettit at nicira.com> writes:

> On Aug 25, 2009, at 9:48 AM, Ben Pfaff wrote:
>
>> What warning does it give?
>
> lib/dpif-netdev.c: In function 'name_to_dp_idx':
> lib/dpif-netdev.c:159: warning: array subscript has type 'char'

Right, that makes sense.  It's risky to use "char" as an array
subscript because you don't know whether char is signed or
unsigned and indexing with a possibly negative value may cause
surprising results.

This doesn't warn with glibc because Ulrich Drepper thinks that
it should "just work" and glibc's does the right thing to make it
work.  I even reported a bug in the glibc manual's ctype examples
once saying that casting to unsigned char was safer and Ulrich
closed it with a note to the effect that it worked OK without the
cast in glibc and that was all that really mattered (ugh).

>> See C99 7.4 "Character handling <ctype.h>":
>>
>> 1    The header <ctype.h> declares several functions useful for
>> classifying and mapping
>>     characters.166) In all cases the argument is an int, the value
>> of which shall be
>>     representable as an unsigned char or shall equal the value of
>> the macro EOF. If the
>>     argument has any other value, the behavior is undefined.
>
>
> I was casting it based on the prototype shown in the isdigit() man
> page.  Thanks for the pointer to the C99 spec.  

Obviously you haven't read my FAQ page on this issue :-)
        http://benpfaff.org/writings/clc/ctype-cast.html       

> Should I go ahead and cast it to an unsigned char, then?

Yes please.




More information about the discuss mailing list