Search code examples
c++gccnull

potential null deference warning


From one of the more highly voted answers here: Read whole ASCII file into C++ std::string

And with a newer compiler (gcc), given this code to read a file into a string:

std::ifstream t("file.txt");
std::string str((std::istreambuf_iterator<char>(t)),
                 std::istreambuf_iterator<char>());

The newer compiler gives a potential null deference warning - see: https://godbolt.org/z/KojKWT4WK

We are using the std::string constructor that takes first/last iterators - and I am happy they are set to correct values, so I don't think there is anything wrong with this code.

But the compiler is unhappy about the std library code (I guess) - perhaps that this code does not check for/handle null pointers correctly?

So the question here is, is "my" code acceptable or does it have a potential bug? - or is this a possible bug in the std lib? or is this just a false posative?


Solution

  • is this just a false positive?

    The program is well-formed. This seems to be a false positive. Here is the submitted gcc bug:

    GCC emits warning potential null dereference