[英]Invalid read of size 1 when using valgrind
I am getting this output when using valgrind: 使用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)
Here is where I have narrowed it down to being the problem in my code: 在这里,我将其范围缩小为代码中的问题:
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;
}
}
I have many errors, and providing the entire valgrind output would be overkill I feel. 我有很多错误,我觉得提供整个valgrind输出会过大。 All of the errors I am getting come back to where I am using asprintf() ("addToList (pa1.c:124)" line in the report)
. 我得到的所有错误都返回到我使用asprintf()的位置("addToList (pa1.c:124)" line in the report)
。 Above the code I initialize char *buff = NULL;
在代码上方,我初始化了char *buff = NULL;
, and currentList->value
holds a string that I am wanting to append onto using asprintf. ,并且currentList->value
包含一个我想使用asprintf附加到的字符串。 Any guesses to why I am getting the numerous Invalid read size 1 errors would be greatly appreciated. 对于我为什么会收到大量无效读取大小1错误的任何猜测,将不胜感激。 Also, the string is appended as expected when I do print out the result from asprintf() Thanks. 另外,当我确实打印出asprintf()的结果时,该字符串将按预期追加。
The invalid read is at the start of an already freed block of memory; 无效的读取是在已释放的内存块的开头; you are trying to use memory after you've freed it. 您要在释放内存后尝试使用内存。
currentList->value = buff;
free(buff);
printf("Here is the new string that holds the directory paths: %s\n", currentList->value);
return;
You use currentList->value = buff;
您使用currentList->value = buff;
in the printf()
, but you've just freed buff
. 在printf()
,但是您刚刚释放了buff
。 This spells an impending disaster. 这意味着一场即将来临的灾难。 If currentList->value
is going to be used in calling functions after the return
, you've got major problems. 如果在return
之后将在调用函数中使用currentList->value
,则会遇到重大问题。
You can fix the one warning by moving the printf()
before the free()
, but the code looks like you're going to have major issues. 您可以通过将printf()
移到free()
printf()
前面来解决一个警告,但是代码看起来将遇到重大问题。 It might be better to delete the free(buff);
删除free(buff);
可能更好free(buff);
— though you'd have to work out where the data is actually freed instead. -尽管您必须找出实际释放数据的位置。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.