【发布时间】:2016-01-15 19:55:32
【问题描述】:
在下面的 valgrind 输出中,能否解释一下在 valgrind 库中引用 strlen 的顶行的含义。这是否意味着 valgrind itslef 有 bug?
==26147== Invalid read of size 1
==26147== at 0x4C2E0E2: strlen (in/usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==26147== by 0x40263A: urldecode (server.c:1131)
==26147== by 0x401853: main (server.c:199)
==26147== Address 0xffefffaa0 is on thread 1's stack
==26147== 136 bytes below stack pointer
char* urldecode(const char* s)
{
// check whether s is NULL
if (s == NULL)
{
return NULL;
}
// allocate enough (zeroed) memory for an undecoded copy of s
char* t = calloc(strlen(s) + 1, 1); <--- line 1131
【问题讨论】:
-
你最好看看
server.c:1131 -
小心指责你的工具有问题。它通常(但不总是)是您自己的代码。
-
重现问题的邮政编码。
-
这看起来像是一个没有正确终止的 C 字符串。
strlen假设它传递的字符串以空字符结尾。如果 char 数组中没有空字符,strlen就会越界——这不是它自己的错。 -
valgrind 提供了自己的 strlen() 版本,遮蔽了 libc 中的版本。没关系。大小为 1 的无效读取可能意味着使用无效但非 NULL 的指针调用 urldecode(),该指针恰好指向 0x00。