"Yes, Virginia, it had better be unsigned"是什么意思?
What is the meaning of "Yes, Virginia, it had better be unsigned"?
在 linux 源代码版本 3.18(及之前)中,在 string.c 文件中,在函数 strncasecmp 中,第一件事是:
/* Yes, Virginia, it had better be unsigned */
unsigned char c1, c2;
在这里可以看到:http://lxr.free-electrons.com/source/lib/string.c
这是什么意思?
string.c:strncasecmp() calls __tolower
from include/linux/ctype.h 期望 unsinged char
.
编辑添加:一般来说,您总是希望将 unsigned char
传递给 ctype.h 函数 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" 位。
比较神秘的是#define __ismask(x) (_ctype[(int)(unsigned char)(x)])
中的include/linux/ctype.h actually appears idiot-proof in this respect, because it does its own safety-minded cast。我不确定 "Yes, Virginia" 注释是什么时候相对于另一行添加的,但是对于 include/linux/ctype.h
的当前版本,string.c:strncasecmp()
似乎可以正常工作,即使 char
对于 c1
和 c2
。我还没有真正尝试过以这种方式更改和测试它...
同样,如果您返回 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,但其中甚至没有 strncasecmp
。看起来这两项更改都是在 Linux 2.0 和 2.2 之间的某处进行的,但我现在不能告诉你更多,哪个先出现等等。
在 linux 源代码版本 3.18(及之前)中,在 string.c 文件中,在函数 strncasecmp 中,第一件事是:
/* Yes, Virginia, it had better be unsigned */
unsigned char c1, c2;
在这里可以看到:http://lxr.free-electrons.com/source/lib/string.c
这是什么意思?
string.c:strncasecmp() calls __tolower
from include/linux/ctype.h 期望 unsinged char
.
编辑添加:一般来说,您总是希望将 unsigned char
传递给 ctype.h 函数 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" 位。
比较神秘的是#define __ismask(x) (_ctype[(int)(unsigned char)(x)])
中的include/linux/ctype.h actually appears idiot-proof in this respect, because it does its own safety-minded cast。我不确定 "Yes, Virginia" 注释是什么时候相对于另一行添加的,但是对于 include/linux/ctype.h
的当前版本,string.c:strncasecmp()
似乎可以正常工作,即使 char
对于 c1
和 c2
。我还没有真正尝试过以这种方式更改和测试它...
同样,如果您返回 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,但其中甚至没有 strncasecmp
。看起来这两项更改都是在 Linux 2.0 和 2.2 之间的某处进行的,但我现在不能告诉你更多,哪个先出现等等。