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

标签 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)”行) 的地方。在我初始化 char *buff = NULL; 的代码上方,currentList->value 包含一个字符串,我想使用 asprintf 将其附加到该字符串上。任何关于为什么我会收到大量 Invalid read size 1 错误的猜测都将不胜感激。此外,当我打印出 asprintf() 的结果时,字符串会按预期附加,谢谢。

最佳答案

无效读取位于已释放内存块的开头;您在释放内存后尝试使用内存。

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

您在 printf() 中使用了 currentList->value = buff;,但您刚刚释放了 buff。这预示着一场迫在眉睫的灾难。如果 currentList->value 将用于在 return 之后调用函数,那你就有大问题了。

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

关于c - 使用 valgrind 时大小为 1 的读取无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41670087/

相关文章:

c - 链表程序崩溃

c - 在 Windows 上使用 asprintf()

c - asprintf - 如何在 C 中获取字符串输入

C++ 表达式模板

c - 具有两个相同大小的操作数的表达式的类型是什么?

c - 为什么这个程序在使用大字符串时不能给出正确的结果?

java - Java中如何从链表中的节点获取对象?

c - 如何检测 openGL/card 是否支持 2 的非幂?

c - 后面添加一个链表