【问题标题】:MemorySanitzer warns of use of undefined memory in `struct stat`; I do check the `stat` return value, thoughMemorySanitzer 警告在 `struct stat` 中使用未定义的内存;不过,我确实检查了`stat`返回值
【发布时间】:2021-12-30 01:26:12
【问题描述】:
        struct stat st;
        if (stat(python_pkgdir, &st)) {
            qd_error_errno(errno, "Cannot find Python library path '%s'", python_pkgdir);
            return NULL;
        } else if (!S_ISDIR(st.st_mode)) {  // dispatch.c, line 99
            qd_error(QD_ERROR_RUNTIME, "Python library path '%s' not a directory", python_pkgdir);
            return NULL;
        }
==2028==WARNING: MemorySanitizer: use-of-uninitialized-value
    #0 0x56b3c3 in qd_dispatch /home/runner/work/qpid-dispatch/qpid-dispatch/qpid-dispatch/src/dispatch.c:99:20
    #1 0x4c2346 in main_process /home/runner/work/qpid-dispatch/qpid-dispatch/qpid-dispatch/router/src/main.c:92:16
    #2 0x4c05d8 in main /home/runner/work/qpid-dispatch/qpid-dispatch/qpid-dispatch/router/src/main.c:369:9
    #3 0x7f398fd39b74 in __libc_start_main (/lib64/libc.so.6+0x27b74)
    #4 0x43fdbd in _start (/__w/qpid-dispatch/qpid-dispatch/qpid-dispatch/build/router/qdrouterd+0x43fdbd)

  Uninitialized value was created by an allocation of 'st' in the stack frame of function 'qd_dispatch'
    #0 0x56ab90 in qd_dispatch /home/runner/work/qpid-dispatch/qpid-dispatch/qpid-dispatch/src/dispatch.c:77

SUMMARY: MemorySanitizer: use-of-uninitialized-value /home/runner/work/qpid-dispatch/qpid-dispatch/qpid-dispatch/src/dispatch.c:99:20 in qd_dispatch

我的代码可能出了什么问题?据我所知,当调用stat 成功时,我只触摸st.st_mode。我正在责备我的编译器、消毒剂或 glibc 中的错误。

我唯一能想到的就是稍微解开if 的包装。执行if ( ... != 0) return,删除不需要的else(由于之前的return),使代码更常规。不过,这不会改变意思。

【问题讨论】:

  • 随便struct stat st = {0};

标签: c clang glibc stat memory-sanitizer


【解决方案1】:

MemorySanitizer 不会检测执行程序所涉及的所有代码。它不能检测外部库,包括标准库或内核代码。

MemorySanitizer 要求检测所有程序代码。这还包括程序所依赖的任何库,甚至是 libc。未能做到这一点可能会导致虚假报告。

Full MemorySanitizer 检测很难实现。为方便起见,MemorySanitizer 运行时库包含 70 多个拦截器,用于最常见的 libc 函数。

显然 libc 有 70 多个函数,因此误报是不可避免的。

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-03-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多