简体   繁体   中英

valgrind invalid read size

In the valgrind output below, can some explain the meaning of the top line that refrences strlen in a valgrind library. Does this mean that valgrind itslef has a 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

This is a stack trace. It says that:

  • strlen() tried to read 1 byte of memory that it should not do (probably it has gone 1 byte past a buffer you have dynamically allocated)
  • that strlen() call was called from server.c line 1131, the urldecode() function.
  • the urldecode() function was called from server.c line 199

This means there's a bug in your code. You find the topmost element in the stack trace that is your code, likely this is line 1131 in server.c.

From line 1131 you start figuring out why you're passing an invalid string to strlen(). Perhaps it's a string that uninitialized or not properly nul terminated.

Eventually you might need to track down the place where your string is created, the new code you've posted for the urldecode() function looks fine, so you might need to go back to server.c line 199 and see how the string you've passed to urldecode() has been made.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM