gpt4 book ai didi

C++ strncpy 参数 "optimized out";覆盖随机内存

转载 作者:太空狗 更新时间:2023-10-29 12:05:14 25 4
gpt4 key购买 nike

首先,对于未能将问题范围缩小到能够共享重现错误的简短程序,我深表歉意。

strncpy 的外部库 (FUSE) 调用“随机”覆盖 shared_ptr<mutex>在我的代码中,当我尝试锁定该互斥锁时会导致段错误。我用 valgrind 运行我的程序,它没有发现任何内存错误(下面的 valgrind 标志)。当我在 gdb 中运行代码并在 shared_ptr 上设置观察点时,它在调用 strncpy 时中断。 gdb 表示所有 strncpy的参数(dest、src 和 nbytes)已被“优化掉”,这让我认为它正在为该调用使用未初始化的内存。我的解释正确吗?知道可能是什么原因吗?

这是指针被覆盖时来自 gdb 的堆栈跟踪:

#0  __strncpy_ssse3 () at ../sysdeps/x86_64/multiarch/strcpy-ssse3.S:2482
#1 0x0000003245809094 in strncpy (__len=<optimized out>, __src=<optimized out>, __dest=<optimized out>) at /usr/include/bits/string3.h:120
#2 add_name (buf=<optimized out>, bufsize=<optimized out>, s=<optimized out>, name=<optimized out>) at fuse.c:907
#3 0x000000324580997c in try_get_path (f=<optimized out>, nodeid=<optimized out>, name=<optimized out>, path=<optimized out>, wnodep=<optimized out>, need_lock=<optimized out>) at fuse.c:956
#4 0x000000324580a281 in get_path_common (f=<optimized out>, nodeid=<optimized out>, name=<optimized out>, path=<optimized out>, wnode=<optimized out>) at fuse.c:1152
#5 0x0000003245812432 in fuse_lib_unlink (req=<optimized out>, parent=<optimized out>, name=<optimized out>) at fuse.c:1198
#6 0x0000003245817057 in fuse_ll_process_buf (data=0x6f5650, buf=0x7fffffffd850, ch=<optimized out>) at fuse_lowlevel.c:2441
#7 0x000000324581388f in fuse_session_loop (se=0x6f8410) at fuse_loop.c:40
#8 0x000000324580b698 in fuse_loop (f=<optimized out>) at fuse.c:4309
#9 0x000000324581bb8f in fuse_main_common (argc=<optimized out>, argv=<optimized out>, op=<optimized out>, op_size=<optimized out>, user_data=<optimized out>, compat=<optimized out>) at helper.c:355
#10 0x000000000046f1b6 in main (argc=4, argv=0x7fffffffdec8) at ../src/fuse.cpp:100

这是我运行 valgrind 时使用的参数:

valgrind --tool=memcheck --leak-check=yes --show-reachable=yes --num-callers=20 --track-fds=yes

最佳答案

此错误是由于使用 new 创建 shared_ptr,然后将其类型转换为 weak_ptr 并将其删除。

关于C++ strncpy 参数 "optimized out";覆盖随机内存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16027993/

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