Why does clang MemorySanitizer produce error in fstream open?

207 Views Asked by At

I am trying to integrate Memory Sanitizer into my project. And following code raise an use-of-uninitialized-value error:

#include <fstream>

int main () {
  std::ifstream ifs;
  ifs.open ("test.txt", std::ifstream::in);

  char c = ifs.get(); // this line doesn't matter

  if (!ifs.good()) {
    return -1;
  }

  ifs.close();

  return 0;
}

$ clang -fsanitize=memory -fPIE -pie -fno-omit-frame-pointer -g -O3 main.cpp -lstdc++
$ ./a.out 
==43028==WARNING: MemorySanitizer: use-of-uninitialized-value
    #0 0x55986e5d76ae in std::basic_ifstream<char, std::char_traits<char> >::open(char const*, std::_Ios_Openmode) /usr/bin/../lib/gcc/x86_64-linux-gnu/11/../../../../include/c++/11/fstream
    #1 0x55986e5d76ae in main /home/promanjuk/learn/sanityzers/main.cpp:5:7
    #2 0x7f6389ffdd8f in __libc_start_call_main csu/../sysdeps/nptl/libc_start_call_main.h:58:16
    #3 0x7f6389ffde3f in __libc_start_main csu/../csu/libc-start.c:392:3
    #4 0x55986e551334 in _start (/home/promanjuk/learn/sanityzers/a.out+0x21334) (BuildId: 597886c0634c006622192d0fa4c5ab7f169b7625)

SUMMARY: MemorySanitizer: use-of-uninitialized-value /usr/bin/../lib/gcc/x86_64-linux-gnu/11/../../../../include/c++/11/fstream in std::basic_ifstream<char, std::char_traits<char> >::open(char const*, std::_Ios_Openmode)
Exiting

$ clang --version
Ubuntu clang version 14.0.0-1ubuntu1
Target: x86_64-pc-linux-gnu
Thread model: posix
InstalledDir: /usr/bin

GodBolt behavior is the same (from clang 5.0 to 15.0, older versions produce a linker error).

Any ideas?

Thanks.

Code example I take from https://cplusplus.com/reference/fstream/ifstream/open/

So, this I've tried to run with/without

char c = ifs.get();

Tried different version of the clang.

0

There are 0 best solutions below