wprintf:带有NULL指针的%p

Dre*_*wen 7 c printf glibc widechar

在我编写单元测试时,我偶然发现了一些奇怪的行为glibc,关于"%p"NULL指针.

如果我有一条这样的线printf("NULL pointer is %p\n", NULL);,那么我看到NULL pointer is (nil)印在屏幕上,正如我所料.

如果我改为使用宽字符版本:wprintf(L"NULL pointer is %p\n", NULL);,则打印出来NULL pointer is (,并在左括号处停止.如果我打印非NULL指针,它会打印该指针,包括普通和宽字符版本.这是一个已知的错误glibc,或者我只是遗漏了什么?

注意:我意识到C标准说指针以%p实现定义的方式转换; 只是打印(一个NULL指针似乎不寻常.

eca*_*mur 8

这肯定是一个错误:https://sourceware.org/git/gitweb.cgi?p = glibc.git; a = blob; f = stdio-common/vfprintf.c; hb = c15cf13a8a672bd27bf3d94b995c52872eed537d#l932

 934             /* Write "(nil)" for a nil pointer.  */                           \
 935             string = (CHAR_T *) L_("(nil)");                                  \
 936             /* Make sure the full string "(nil)" is printed.  */              \
 937             if (prec < 5)                                                     \
 938               prec = 5;                                                       \
 939             is_long = 0;        /* This is no wide-char string.  */           \
 940             goto LABEL (print_string);                                        \
Run Code Online (Sandbox Code Playgroud)

L_("(nil)")膨胀到L"(nil)"了wprintf,但几行后的is_long设置为0(即假).结果string被解释为一个窄字符串,所以打印它将停止在它的第一个零字节,即在(.

报告的错误链接:https://sourceware.org/bugzilla/show_bug.cgi?id = 16890 - 这是在glibc版本2.20中修复的.

有趣的是,这个bug似乎已经存在了将近15年才发现并修复 - 在报告后的2天内!