gpt4 book ai didi

c++ - Valgrind 与 dirname 决裂?

转载 作者:太空宇宙 更新时间:2023-11-04 12:04:40 27 4
gpt4 key购买 nike

==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==

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

        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)

最佳答案

Valgrind 指示大小为 8 的读取是从中的字节 nr 8 完成的一个 10 字节的 block 。此读取由 memrchr 完成。此类功能通常基于您可以阅读的假设进行优化比分配的 block 更多的字节。为避免报告此类问题,Valgrind 必须更换此类优化通过其自身的重新定义功能发挥作用。

memrchr 仅在 Valgrind 3.8 及更高版本中被重新定义。

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

关于c++ - Valgrind 与 dirname 决裂?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12708501/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com