<table border="1" cellspacing="0" cellpadding="8">
    <tr>
        <th>Issue</th>
        <td>
            <a href=https://github.com/llvm/llvm-project/issues/106261>106261</a>
        </td>
    </tr>

    <tr>
        <th>Summary</th>
        <td>
            [libc++] Wraparound in string `operator>>` causes issues with `-fsanitize=integer`
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            libc++
      </td>
    </tr>

    <tr>
      <th>Assignees</th>
      <td>
            ldionne
      </td>
    </tr>

    <tr>
      <th>Reporter</th>
      <td>
          ldionne
      </td>
    </tr>
</table>

<pre>
    Consider the following program:

```c++
#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:

```c++
__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.
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJyUVdGOozoPfpr0xpoKAhR6wcXsdPsC_y_tZRWIAZ8TkioJZbtPf2RoO9PR7OocCZGSxJ_tz_5cFQL1FrEWxTchpdHkrEUhpSgOGzXFwfn6trlpnL7Wb84G0ughDgidM8bNZHs4e9d7NYrsVSQHkdzfu2R9WiG_8bPuyoxsayaNILK3ED3ZXmTfvzrsQvTIsF-ekvt8vLzJRhgVWSErIfcgyptfAIAQNceYva5uoXNOZF-d0801dDcgOQ8q4gU9kyP3T1YdWeAgsu_PgA-01k2RIxbZGwgpPSrN632rc-7-82GCVpsHkigPnxj9mPAPigOIXfLSBWUp0i8U2YFsxB49X5VvM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