Home > Net >  MemorySanitzer warns of use of undefined memory in `struct stat`; I do check the `stat` return value
MemorySanitzer warns of use of undefined memory in `struct stat`; I do check the `stat` return value

Time:11-20

        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

What could go wrong there in my code? As far as I can tell, I only touch st.st_mode when call to stat succeeded. I am at the point of blaming a bug in my compiler, sanitizer, or glibc.

The only thing I can think of doing with is to unpack the if a little bit. Do a if ( ... != 0) return, remove the else as it is not needed (due to return before), to make the code more conventional. That would not change the meaning, though.

CodePudding user response:

MemorySanitizer does not instrument all the code involved in the execution of your program. It cannot instrument external libraries, including the standard library, or kernel code.

MemorySanitizer requires that all program code is instrumented. This also includes any libraries that the program depends on, even libc. Failing to achieve this may result in false reports.

Full MemorySanitizer instrumentation is very difficult to achieve. To make it easier, MemorySanitizer runtime library includes 70 interceptors for the most common libc functions.

Obviously libc has much much more than 70 functions, so false positives are inevitable.

  • Related