Closed
Description
Consider the following program:
#include <string>
#include <fstream>
#include <iostream>
int main() {
std::string foo;
std::ifstream fin("whatever");
fin >> foo;
std::cout << "read " << foo << std::endl;
}
With -fsanitize=integer
, we get:
include/c++/v1/istream:1213:15: runtime error: implicit conversion from type 'size_type' (aka 'unsigned long') of value 18446744073709551607 (64-bit, unsigned) to type 'streamsize' (aka 'long') changed the value to -9 (64-bit, signed)
This is because the code does this:
__str.clear();
streamsize __n = __is.width();
if (__n <= 0)
__n = __str.max_size();
if (__n <= 0)
__n = numeric_limits<streamsize>::max();
So basically -fsanitize=integer
is complaining about the wraparound.
Although we're handling the wraparound afterwards with the second if
, it would be easy to write this code in a less confusing way.