3

In the linux source code version 3.18 (and previous), in the string.c file, in the function strncasecmp, the very first thing is:

/* Yes, Virginia, it had better be unsigned */
unsigned char c1, c2;

As can be seen here: http://lxr.free-electrons.com/source/lib/string.c

What is the meaning of this?

Shelvacu
  • 4,245
  • 25
  • 44

1 Answers1

4

string.c:strncasecmp() calls __tolower from include/linux/ctype.h which expects an unsinged char.

EDITed to add: In general you always want to pass unsigned char to ctype.h functions because of C Standard ยง7.4, which says the behavior is undefined if the argument to ctype.h functions is not representable as unsigned char or EOF. So that probably explains the "Yes, Virginia" bit.

What is a bit more mysterious is that include/linux/ctype.h actually appears idiot-proof in this respect, because it does its own safety-minded cast in #define __ismask(x) (_ctype[(int)(unsigned char)(x)]). I'm not sure when the "Yes, Virginia" comment was added relative to this other line, but with the current version of include/linux/ctype.h it appears that string.c:strncasecmp() would work fine even with char for c1 and c2. I haven't really tried to change & test it that way though...

Also if you go back to Linux 2.0.40's ctype.h, the safety-minded cast ((int)(unsigned char)) is not there anymore. There's no "Virginia" comment either in 2.0.40's string.c, but there's not even a strncasecmp in it. It looks like both changes were made somewhere in between Linux 2.0 and 2.2, but I can't tell you more right now which came first, etc.

Fizz
  • 4,782
  • 1
  • 24
  • 51
  • 1
    If you search for "virginia" in the linux tree, there are more instances of "yes, Virginia, something something". It's probably a reference to some old nerdy thing (_nerdy_ not in any way in a bad sense). โ€“ Shahbaz Jan 21 '15 at 15:57
  • The Wikipedia article on http://en.wikipedia.org/wiki/Yes,_Virginia,_there_is_a_Santa_Claus (which I linked in my answer) does mention this "nerdy" aspect. One referece it cites as illustration of the social phenomenon (meme) is http://vrt-blog.snort.org/2010/07/yes-virginia-there-is-cyberwar.html It's surely older though. โ€“ Fizz Jan 21 '15 at 16:03
  • I think it refers to the C standard that defines three different types: `char`, `unsigned char`, and `signed char`. And they are slightly different on some architectures and / or compilers. โ€“ 0andriy Jan 21 '15 at 18:12