使用valgrind时读取大小为1无效

时间:2017-01-16 05:40:25

标签: c linked-list asprintf

使用valgrind时,我得到了这个输出:

==19923== Invalid read of size 1
==19923==    at 0x52CCCC0: vfprintf (vfprintf.c:1632)
==19923==    by 0x52F4772: vasprintf (vasprintf.c:59)
==19923==    by 0x52D3A56: asprintf (asprintf.c:35)
==19923==    by 0x400D77: addToList (pa1.c:124)
==19923==    by 0x4010AF: md5Hash (pa1.c:220)
==19923==    by 0x401138: newFile (pa1.c:244)
==19923==    by 0x401260: searchDirects (pa1.c:280)
==19923==    by 0x401451: main (pa1.c:339)
==19923==  Address 0x585b720 is 0 bytes inside a block of size 27 free'd
==19923==    at 0x4C2EDEB: free (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==19923==    by 0x401144: newFile (pa1.c:246)
==19923==    by 0x401260: searchDirects (pa1.c:280)
==19923==    by 0x401382: directoryCheck (pa1.c:312)
==19923==    by 0x4012CD: searchDirects (pa1.c:290)
==19923==    by 0x401451: main (pa1.c:339)
==19923==  Block was alloc'd at
==19923==    at 0x4C2DB8F: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==19923==    by 0x52F47D7: vasprintf (vasprintf.c:73)
==19923==    by 0x52D3A56: asprintf (asprintf.c:35)
==19923==    by 0x40112C: newFile (pa1.c:239)
==19923==    by 0x401260: searchDirects (pa1.c:280)
==19923==    by 0x401382: directoryCheck (pa1.c:312)
==19923==    by 0x4012CD: searchDirects (pa1.c:290)
==19923==    by 0x401451: main (pa1.c:339)

以下是我将其缩小到我的代码中的问题:

else
{    
    currentList = headList;
    printf("This is where I will put the code to create the Linked list!\n");
    if(currentList != 0)
    {
        while(currentList->nextList != 0)
        {
            if(currentList->key = key)
            {
                asprintf(&buff, "%s %s", currentList->value, dirValue); // PROBLEM IS HERE
                printf("Here is the new string that holds the directory paths: %s\n", buff);
                currentList->value = buff;
                free(buff);
                printf("Here is the new string that holds the directory paths: %s\n", currentList->value);
                return;
            }
            currentList = currentList->nextList;
        }

        currentList->nextList = malloc(sizeof(struct List));
        printf("Adding a new node\n");
        //Reached the end of the Linked list and didn't find the
        //same key so create a new node.
        currentList = currentList->nextList;
        currentList->key = key;
        currentList->nextList = NULL;
        currentList->value = dirValue;
    }
}

我有很多错误,提供整个valgrind输出会觉得有点过分。我得到的所有错误都回到了我使用asprintf()("addToList (pa1.c:124)" line in the report)的地方。在代码上方我初始化char *buff = NULL;,而currentList->value包含一个我想要使用asprintf追加的字符串。任何猜测为什么我得到众多无效读取大小1错误将不胜感激。此外,当我从asprintf()打印出结果时,字符串按预期附加,谢谢。

1 个答案:

答案 0 :(得分:2)

无效读取位于已释放的内存块的开头;你释放它后试图使用内存。

currentList->value = buff;
free(buff);
printf("Here is the new string that holds the directory paths: %s\n", currentList->value);
return;

您在currentList->value = buff;中使用printf(),但您刚刚释放了buff。这意味着即将发生的灾难。如果在currentList->value之后调用函数将使用return,则会遇到重大问题。

您可以通过在printf()之前移动free()来修复一个警告,但代码看起来会出现重大问题。删除free(buff);可能更好 - 尽管您必须确定实际释放数据的位置。