2011-06-30 56 views
0

嘗試pclose()以前用popen()打開的管道時出現valgrind錯誤。錯誤發生在Mac OS X上,但不在Linux上。請看下面的例子:由Mac OS X上的pclose()引起的Valgrind錯誤

#include <stdlib.h> 
#include <stdio.h> 

int main() { 
    FILE *fp; 
    char buf[4096]; 

    if (!(fp = popen("ls", "r"))) 
    exit(-1); 

    while (fscanf(fp, "%s", buf) == 1) 
    printf("%s\n", buf); 

    pclose(fp); 

    return 0; 
} 

我得到的是Mac(OS X 10.6.7,Valgrind的版本3.6.0)以下Valgrind的錯誤,除非我刪除pclose()電話:

==21455== Conditional jump or move depends on uninitialised value(s) 
==21455== at 0xB1992: pclose (in /usr/lib/libSystem.B.dylib) 
==21455== by 0x1F16: main (in ./a.out) 
==21455== 
==21455== Syscall param wait4(pid) contains uninitialised byte(s) 
==21455== at 0x504FA: wait4 (in /usr/lib/libSystem.B.dylib) 
==21455== by 0x1F16: main (in ./a.out) 

但是,我在valgrind 3.5.0版的Linux系統上沒有發現任何錯誤。

關於什麼可能導致Mac上的錯誤的任何想法?

更新

中的valgrind打開--track-origins表明,問題的根源可能在popen()通話。用gcc 4.2.1和4.5.3得到了同樣的結果。

==4425== Conditional jump or move depends on uninitialised value(s) 
==4425== at 0xB1992: pclose (in /usr/lib/libSystem.B.dylib) 
==4425== by 0x1F18: main (in ./a.out) 
==4425== Uninitialised value was created by a stack allocation 
==4425== at 0xB14C5: popen$UNIX2003 (in /usr/lib/libSystem.B.dylib) 
==4425== 
==4425== Syscall param wait4(pid) contains uninitialised byte(s) 
==4425== at 0x504FA: wait4 (in /usr/lib/libSystem.B.dylib) 
==4425== by 0x1F18: main (in ./a.out) 
==4425== Uninitialised value was created by a stack allocation 
==4425== at 0xB14C5: popen$UNIX2003 (in /usr/lib/libSystem.B.dylib) 
+0

很難明白爲什麼它會這麼說。我有一種愚蠢的建議是在main()的第一行初始化你的fp爲NULL。 –

+0

@Brian謝謝,但如你所說,初始化fp爲NULL後無效。 – Leo

+0

您的代碼適用於我,也適用於Mac OS X v10.6.7和valgrind 3.6.0。我沒有看到你的代碼有任何問題 - 這可能沒有什麼可擔心的。 –

回答

2

系統庫將未初始化的字節傳遞給系統調用很常見。條件跳轉依賴於未初始化的值的情況並不常見,但它確實發生了(我的Linux版本中的glibc-2.X.supp在glibc中包含了8次抑制)。

因爲無論如何你對這些錯誤都無能爲力,所以你應該壓制它們。請參閱Valgrind文檔中的--gen-suppressions

2

報告的問題似乎是系統庫內部的問題,而不是您的代碼。

我也使用MacOS X 10.6.8,Valgrind 3.6.0和(Apple的)GCC 4.2.1或(我的)GCC 4.6.0都沒有錯誤。我從你的代碼(如圖4.6.0)得到編譯警告 - 事實上,我有「製作」運行命令和makefile文件包含了所有這些-Wxxx參數:

$ gcc -g -std=c99 -Wall -Wextra -Wmissing-prototypes -Wstrict-prototypes -Wold-style-definition  vg.c -o vg 
vg.c:4:5: warning: function declaration isn’t a prototype [-Wstrict-prototypes] 
vg.c: In function ‘main’: 
vg.c:4:5: warning: old-style function definition [-Wold-style-definition] 
$ valgrind vg 
==40593== Memcheck, a memory error detector 
==40593== Copyright (C) 2002-2010, and GNU GPL'd, by Julian Seward et al. 
==40593== Using Valgrind-3.6.0 and LibVEX; rerun with -h for copyright info 
==40593== Command: vg 
==40593== 
vg 
vg.c 
vg.dSYM 
==40593== 
==40593== HEAP SUMMARY: 
==40593==  in use at exit: 4,184 bytes in 2 blocks 
==40593== total heap usage: 6 allocs, 4 frees, 26,848 bytes allocated 
==40593== 
==40593== LEAK SUMMARY: 
==40593== definitely lost: 0 bytes in 0 blocks 
==40593== indirectly lost: 0 bytes in 0 blocks 
==40593==  possibly lost: 0 bytes in 0 blocks 
==40593== still reachable: 4,184 bytes in 2 blocks 
==40593==   suppressed: 0 bytes in 0 blocks 
==40593== Rerun with --leak-check=full to see details of leaked memory 
==40593== 
==40593== For counts of detected and suppressed errors, rerun with: -v 
==40593== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0) 
$ cc --version 
i686-apple-darwin10-llvm-gcc-4.2 (GCC) 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2335.9) 
Copyright (C) 2007 Free Software Foundation, Inc. 
This is free software; see the source for copying conditions. There is NO 
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 

$ gcc --version 
gcc (GCC) 4.6.0 
Copyright (C) 2011 Free Software Foundation, Inc. 
This is free software; see the source for copying conditions. There is NO 
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 

$ valgrind --version 
valgrind-3.6.0 
Localhost JL: uname -a 
Darwin localhost 10.8.0 Darwin Kernel Version 10.8.0: Tue Jun 7 16:33:36 PDT 2011; root:xnu-1504.15.3~1/RELEASE_I386 i386 
$ otool -L /usr/lib/libSystem.B.dylib 
/usr/lib/libSystem.B.dylib: 
    /usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 125.2.11) 
    /usr/lib/system/libmathCommon.A.dylib (compatibility version 1.0.0, current version 315.0.0) 

-v --gen-suppressions=yes運行,valgrind報告很多更多信息,但仍然沒有壓制錯誤。

0

此錯誤在最新的Valgrind SVN源代碼中出現。 Valgrind中的一些內部錯誤已得到解決,以及已知的Apple系統庫錯誤被抑制。

注意這是在OS X上運行10.10.4

$ ./vg-in-place ../../test 
==55558== Memcheck, a memory error detector 
==55558== Copyright (C) 2002-2013, and GNU GPL'd, by Julian Seward et al. 
==55558== Using Valgrind-3.11.0.SVN and LibVEX; rerun with -h for copyright info 
==55558== Command: ../../test 
==55558== 
--55558-- ../../test: 
--55558-- dSYM directory is missing; consider using --dsymutil=yes 
AUTHORS 
COPYING 
COPYING.DOCS 
Makefile 
... 
vg-in-place 
xfree-3.supp 
xfree-4.supp 
==55558== 
==55558== HEAP SUMMARY: 
==55558==  in use at exit: 39,331 bytes in 419 blocks 
==55558== total heap usage: 523 allocs, 104 frees, 68,971 bytes allocated 
==55558== 
==55558== LEAK SUMMARY: 
==55558== definitely lost: 0 bytes in 0 blocks 
==55558== indirectly lost: 0 bytes in 0 blocks 
==55558==  possibly lost: 0 bytes in 0 blocks 
==55558== still reachable: 0 bytes in 0 blocks 
==55558==   suppressed: 39,331 bytes in 419 blocks 
==55558== 
==55558== For counts of detected and suppressed errors, rerun with: -v 
==55558== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0) 
$ ./vg-in-place --version 
valgrind-3.11.0.SVN