Closed
Description
The following code fails with use-of-uninitialized-value
:
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
int main()
{
struct stat st;
printf("sizeof(struct stat) = %zd\n", sizeof(struct stat));
int fd = open(__FILE__, O_RDONLY);
if (fd < 0) {
return 1;
}
if (fstat(fd, &st)) {
return 1;
}
if (st.st_size > 1) {
return 2;
}
close(fd);
return 0;
}
Output:
$ clang x.c -o x -fsanitize=memory && ./x; echo $?
sizeof(struct stat) = 144
==134123==WARNING: MemorySanitizer: use-of-uninitialized-value
#0 0x49d9e5 in main (/home/vstinner/python/main/x+0x49d9e5)
#1 0x7f874ea5355f in __libc_start_call_main (/lib64/libc.so.6+0x2d55f)
#2 0x7f874ea5360b in __libc_start_main@GLIBC_2.2.5 (/lib64/libc.so.6+0x2d60b)
#3 0x41c344 in _start (/home/vstinner/python/main/x+0x41c344)
SUMMARY: MemorySanitizer: use-of-uninitialized-value (/home/vstinner/python/main/x+0x49d9e5) in main
Exiting
1
I tested on Fedora 35 with versions:
$ clang --version
clang version 13.0.0 (Fedora 13.0.0-3.fc35)
Target: x86_64-redhat-linux-gnu
Thread model: posix
InstalledDir: /usr/bin
$ rpm -q clang
clang-13.0.0-3.fc35.x86_64
Issue discovered in Python: https://bugs.python.org/issue46887