C编程 - 免费方法给我一个无效的下一个大小(快)错误

时间:2013-07-13 08:38:55

标签: c free valgrind getline

我遇到一个问题,即释放由getLine函数分配的字符指针。

应用程序的目的是读取/ proc / net / dev文件并显示有关用户传递的接口的信息。

每当执行free方法时,我的下一个大小(快)错误就会无效。

我从杂志上复制了代码,但正如文章作者所说,它应该按原样运作。

给我带来问题的功能就是这个

int ParseDevFile(const char * Interface, ull *bRx, ull *pRx, ull *bTx, ull *pTx)
{
FILE *FilePointer = NULL;

char *ReadLine = NULL;

unsigned int Length = 0;

FilePointer = fopen("/proc/net/dev", "r");

if (FilePointer == NULL)
{
    perror("Error");
    return -1;
}

while (getline(&ReadLine, &Length, FilePointer) != -1 )
{
    if (strstr(ReadLine, Interface) != NULL)
    {
        sscanf(strstr(ReadLine, ":") + 1, "%llu%llu%*u%*u%*u%*u%*u%*u%llu%llu", bRx, bRx, bTx, pTx);
    }
}

free(ReadLine);

fclose(FilePointer);

return 0;
}

我是C语言的新手,但如果我正确地读取了getline手册页,它会根据线的大小增加ReadLine的缓冲区大小。

在valgrind下运行时,应用程序也能正常工作(除了应用程序开头的一些无效写入)。

以下是Valgrind错误。请注意,在这些错误之后,应用程序继续正常运行。

==31361== Command: ./ifstat wlan0
==31361== 
==31361== Invalid write of size 8
==31361==    at 0x4EC63FA: __GI_memcpy (memcpy.S:125)
==31361==    by 0x4EA40BF: getdelim (iogetdelim.c:116)
==31361==    by 0x4008FE: ParseDevFile (in /home/andrew/C Programming/Magazine Tutorials/ifstat)
==31361==    by 0x400996: DumpInterfaceUsage (in /home/andrew/C Programming/Magazine Tutorials/ifstat)
==31361==    by 0x400B4F: main (in /home/andrew/C Programming/Magazine Tutorials/ifstat)
==31361==  Address 0x51fc333 is 115 bytes inside a block of size 120 alloc'd
==31361==    at 0x4C2CD7B: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==31361==    by 0x4EA404D: getdelim (iogetdelim.c:67)
==31361==    by 0x4008FE: ParseDevFile (in /home/andrew/C Programming/Magazine Tutorials/ifstat)
==31361==    by 0x400996: DumpInterfaceUsage (in /home/andrew/C Programming/Magazine Tutorials/ifstat)
==31361==    by 0x400B4F: main (in /home/andrew/C Programming/Magazine Tutorials/ifstat)
==31361== 
==31361== Invalid write of size 1
==31361==    at 0x4EA41B4: getdelim (iogetdelim.c:123)
==31361==    by 0x4008FE: ParseDevFile (in /home/andrew/C Programming/Magazine Tutorials/ifstat)
==31361==    by 0x400996: DumpInterfaceUsage (in /home/andrew/C Programming/Magazine Tutorials/ifstat)
==31361==    by 0x400B4F: main (in /home/andrew/C Programming/Magazine Tutorials/ifstat)
==31361==  Address 0x51fc33b is 3 bytes after a block of size 120 alloc'd
==31361==    at 0x4C2CD7B: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==31361==    by 0x4EA404D: getdelim (iogetdelim.c:67)
==31361==    by 0x4008FE: ParseDevFile (in /home/andrew/C Programming/Magazine Tutorials/ifstat)
==31361==    by 0x400996: DumpInterfaceUsage (in /home/andrew/C Programming/Magazine Tutorials/ifstat)
==31361==    by 0x400B4F: main (in /home/andrew/C Programming/Magazine Tutorials/ifstat)
==31361== 
==31361== Invalid read of size 1
==31361==    at 0x4C302E4: strstr (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==31361==    by 0x40089F: ParseDevFile (in /home/andrew/C Programming/Magazine Tutorials/ifstat)
==31361==    by 0x400996: DumpInterfaceUsage (in /home/andrew/C Programming/Magazine Tutorials/ifstat)
==31361==    by 0x400B4F: main (in /home/andrew/C Programming/Magazine Tutorials/ifstat)
==31361==  Address 0x51fc338 is 0 bytes after a block of size 120 alloc'd
==31361==    at 0x4C2CD7B: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==31361==    by 0x4EA404D: getdelim (iogetdelim.c:67)
==31361==    by 0x4008FE: ParseDevFile (in /home/andrew/C Programming/Magazine Tutorials/ifstat)
==31361==    by 0x400996: DumpInterfaceUsage (in /home/andrew/C Programming/Magazine Tutorials/ifstat)
==31361==    by 0x400B4F: main (in /home/andrew/C Programming/Magazine Tutorials/ifstat)
==31361== 
==31361== Invalid read of size 1
==31361==    at 0x4C2FF14: __GI___rawmemchr (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==31361==    by 0x4EB237F: _IO_str_init_static_internal (strops.c:44)
==31361==    by 0x4E9306F: __isoc99_vsscanf (isoc99_vsscanf.c:41)
==31361==    by 0x4E93006: __isoc99_sscanf (isoc99_sscanf.c:32)
==31361==    by 0x4008E4: ParseDevFile (in /home/andrew/C Programming/Magazine Tutorials/ifstat)
==31361==    by 0x400996: DumpInterfaceUsage (in /home/andrew/C Programming/Magazine Tutorials/ifstat)
==31361==    by 0x400B4F: main (in /home/andrew/C Programming/Magazine Tutorials/ifstat)
==31361==  Address 0x51fc338 is 0 bytes after a block of size 120 alloc'd
==31361==    at 0x4C2CD7B: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==31361==    by 0x4EA404D: getdelim (iogetdelim.c:67)
==31361==    by 0x4008FE: ParseDevFile (in /home/andrew/C Programming/Magazine Tutorials/ifstat)
==31361==    by 0x400996: DumpInterfaceUsage (in /home/andrew/C Programming/Magazine Tutorials/ifstat)
==31361==    by 0x400B4F: main (in /home/andrew/C Programming/Magazine Tutorials/ifstat)

谢谢你, 安德鲁博格

1 个答案:

答案 0 :(得分:0)

我管理通过将无符号整数长度变量更改为type_t来解决问题。

unsigned int Length = 0;

size_t Length = 0;