的应用程序的目的是读取/ PROC /净/ dev文件,并显示关于由用户通过接口信息。
每当自由方法执行我有一个无效的下一个大小(快速)错误。
我从杂志上复制了代码,但正如文章作者所说,它应该按原样工作。
给我的问题的功能是本
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)
谢谢你, 安德鲁·博格
2013-07-13
Andrew
+0
从'ReadLine'未分配的内存,'Length'是垃圾。 –
+0
你是什么意思?我从杂志中复制了这个内容,并且我教导了getline的长度为零方法来扩展缓冲区 –
+0
@GrijeshChauhan ['man 3 getline'](http://man7.org/linux/man-pages/man3/getline .3.html) - 如果你仔细观察:它会为你分配内存。指针指针传递的原因有很多,而不仅仅是指向某个缓冲区的第一个元素的指针。 –
2013-07-13 08:46:05