<html>
    <head>
      <base href="https://bugs.llvm.org/">
    </head>
    <body><table border="1" cellspacing="0" cellpadding="8">
        <tr>
          <th>Bug ID</th>
          <td><a class="bz_bug_link 
          bz_status_NEW "
   title="NEW - libc++ incorrectly treats operator>>(istream& unsigned int&) as an error when converting negative values"
   href="https://bugs.llvm.org/show_bug.cgi?id=36914">36914</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>libc++ incorrectly treats operator>>(istream& unsigned int&) as an error when converting negative values
          </td>
        </tr>

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

        <tr>
          <th>Version</th>
          <td>6.0
          </td>
        </tr>

        <tr>
          <th>Hardware</th>
          <td>PC
          </td>
        </tr>

        <tr>
          <th>OS</th>
          <td>Windows NT
          </td>
        </tr>

        <tr>
          <th>Status</th>
          <td>NEW
          </td>
        </tr>

        <tr>
          <th>Severity</th>
          <td>enhancement
          </td>
        </tr>

        <tr>
          <th>Priority</th>
          <td>P
          </td>
        </tr>

        <tr>
          <th>Component</th>
          <td>All Bugs
          </td>
        </tr>

        <tr>
          <th>Assignee</th>
          <td>unassignedclangbugs@nondot.org
          </td>
        </tr>

        <tr>
          <th>Reporter</th>
          <td>billy.oneal@gmail.com
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>llvm-bugs@lists.llvm.org, mclow.lists@gmail.com
          </td>
        </tr></table>
      <p>
        <div>
        <pre>Consider the following program:

#include <iostream>
#include <sstream>

int main()
{
    std::istringstream a("-1");
    unsigned i;

    a >> i;
    if (a.fail())
        std::cout << "Bug\n";
    else
        std::cout << "Ok\n";

    std::istringstream b("4294967296");

    b >> i;
    if (b.fail())
        std::cout << "Ok\n";
    else
        std::cout << "Bug\n";
    return 0;
}

libc++ thinks that -1 isn't representable in the type unsigned int, and so
treats this as an error. However, the standard disagrees with this. N4727
[istream.formatted.arithmetic] says that the operator>> calls locale's num_get.
[facet.num.get.virtuals]/3 says it reads as if by scanf %u. C11 7.21.6.2/12
says that %u behaves as if by strtoul. C11 7.22.1.4/5 says:

If the subject sequence begins with a minus sign, the value resulting from
the conversion is negated (in the return type).

That means that the parsing should remember that there was a minus sign, parse
the number as an unsigned int (with usual ERANGE checks), then, because the
input started with a minus sign, negate the resulting value.

(I ran into this bug when a user reported that they expected MSVC++ to treat -1
input as an error here; but libstdc++ agrees with us in not setting failbit
here, and C's rules, however bizarre, do not support setting failbit here)</pre>
        </div>
      </p>


      <hr>
      <span>You are receiving this mail because:</span>

      <ul>
          <li>You are on the CC list for the bug.</li>
      </ul>
    </body>
</html>