C语言中getdelim函数,C语言编程 - 免费的方法给我一个无效的下一个尺寸(快)错误...

的应用程序的目的是读取/ 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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值