Valgrind打破了dirname?

时间:2012-10-03 12:30:59

标签: c++ valgrind dirname

==11136== Invalid read of size 8
==11136==    at 0x5AFC696: memrchr (memrchr.S:289)
==11136==    by 0x5B57FAF: dirname (dirname.c:45)
==11136==    by 0x405F43: push::lg_cmd_dirname(push::Env&) (LGExtension.cpp:379)
==11136==    by 0x42533C: push::Instruction::operator()(push::Env&) const (in /home/bots/svn/eco/branches/skynet_BigPUSH/src/push3.0/extension/push_bloodline)
==11136==    by 0x488ECD: push::Env::go(int) (Env.cpp:72)
==11136==    by 0x4A84D5: main (bloodline.cpp:99)
==11136==  Address 0x640daf8 is 8 bytes inside a block of size 10 alloc'd
==11136==    at 0x4C2B6CD: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==11136==    by 0x5AEF801: strdup (strdup.c:43)
==11136==    by 0x405EF2: push::lg_cmd_dirname(push::Env&) (LGExtension.cpp:369)
==11136==    by 0x42533C: push::Instruction::operator()(push::Env&) const (in /home/bots/svn/eco/branches/skynet_BigPUSH/src/push3.0/extension/push_bloodline)
==11136==    by 0x488ECD: push::Env::go(int) (Env.cpp:72)
==11136==    by 0x4A84D5: main (bloodline.cpp:99)
==11136==

这是合法的错误吗?看起来读取发生在有效块内。在我的程序中,调用如下所示:

        char *path = strdup(full_path.c_str());
        cerr << "Path is : " << path << endl;
        result = dirname(path);
        if(result < 0){
                cerr << "Dirname failed for some reason. Check log." << endl;
        }

错误时输出到cerr是:

Path is : /tmp/tmp/

这是一个有效的路径。 Dirname应该没有任何问题,并且它在堆分配的副本上运行。

编辑:

以下是产生此错误的最小示例:

#include <string.h>
#include <stdio.h>
#include <iostream>
#include <libgen.h>

int main(){

        char *path = strdup("/tmp/tmp/");
        char* result = dirname(path);
        std::cerr << result << std::endl;
}

用g ++编译。

用valgrind运行,你得到:

==32466== Memcheck, a memory error detector                                                                                                                                                                                                  
==32466== Copyright (C) 2002-2011, and GNU GPL'd, by Julian Seward et al.                                                                                                                                                                    
==32466== Using Valgrind-3.7.0 and LibVEX; rerun with -h for copyright info                                                                                                                                                                  
==32466== Command: ./a.out                                                                                                                                                                                                                   
==32466==                                                                                                                                                                                                                                    
==32466== Invalid read of size 8                                                                                                                                                                                                             
==32466==    at 0x51C7696: memrchr (memrchr.S:289)                                                                                                                                                                                           
==32466==    by 0x5222FAF: dirname (dirname.c:45)                                                                                                                                                                                            
==32466==    by 0x400865: main (in /home/j3doucet/a.out)                                                                                                                                                                                     
==32466==  Address 0x59ff048 is 8 bytes inside a block of size 10 alloc'd                                                                                                                                                                    
==32466==    at 0x4C2B6CD: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)                                                                                                                                                   
==32466==    by 0x51BA801: strdup (strdup.c:43)                                                                                                                                                                                              
==32466==    by 0x400855: main (in /home/j3doucet/a.out)                                                                                                                                                                                     
==32466==                                                                                                                                                                                                                                    
/tmp
==32466== 
==32466== HEAP SUMMARY:
==32466==     in use at exit: 10 bytes in 1 blocks
==32466==   total heap usage: 1 allocs, 0 frees, 10 bytes allocated
==32466== 
==32466== LEAK SUMMARY:
==32466==    definitely lost: 10 bytes in 1 blocks
==32466==    indirectly lost: 0 bytes in 0 blocks
==32466==      possibly lost: 0 bytes in 0 blocks
==32466==    still reachable: 0 bytes in 0 blocks
==32466==         suppressed: 0 bytes in 0 blocks
==32466== Rerun with --leak-check=full to see details of leaked memory
==32466== 
==32466== For counts of detected and suppressed errors, rerun with: -v
==32466== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 2 from 2)

1 个答案:

答案 0 :(得分:1)

Valgrind表示从字节nr 8 in读取大小为8 一个10字节的块。 这个读取由memrchr完成。 这些功能通常基于您可以阅读的假设进行优化 比分配的块多的字节。 为避免报告此类问题,Valgrind必须更换此类优化问题 通过自己的重新定义功能来实现。

memrchr仅在Valgrind中从3.8版本开始重新定义。

=&GT;你应该使用最新版本的Valgrind(3.8.1)重试。 然后可能不再报告错误(假设它实际上是误报 由于没有重新定义memrchr)。