0

我在释放由getLine函数分配的字符指针时遇到问题。

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

每当执行免费方法时,我都会遇到 Invalid next Size (fast) 错误。

我从杂志上复制了代码,但正如文章作者所说,它应该可以正常工作。

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

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)

谢谢你,安德鲁·博格

4

1 回答 1

0

我设法通过将无符号整数长度变量更改为 size_t 类型来解决问题。

unsigned int Length = 0;

size_t Length = 0;
于 2013-07-13T09:03:47.957 回答