2013-07-13 41 views
0

我有一個問題釋放正由getLine函數分配一個字符指針。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) 

謝謝你, 安德魯·博格

+0

從'ReadLine'未分配的內存,'Length'是垃圾。 –

+0

你是什麼意思?我從雜誌中複製了這個內容,並且我教導了getline的長度爲零方法來擴展緩衝區 – Andrew

+0

@GrijeshChauhan ['man 3 getline'](http://man7.org/linux/man-pages/man3/getline .3.html) - 如果你仔細觀察:它會爲你分配內存。指針指針傳遞的原因有很多,而不僅僅是指向某個緩衝區的第一個元素的指針。 – 2013-07-13 08:46:05

回答

0

我設法通過改變無符號整數長度是可變的,以size_t類型來解決這個問題。

unsigned int Length = 0; 

size_t Length = 0; 
相關問題