<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 - Input stream formatted input for `float` produces incorrect result for non-hexadecimal-prefixed input containing hexadecimal characters without an exponent"
href="https://bugs.llvm.org/show_bug.cgi?id=36099">36099</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>Input stream formatted input for `float` produces incorrect result for non-hexadecimal-prefixed input containing hexadecimal characters without an exponent
</td>
</tr>
<tr>
<th>Product</th>
<td>libc++
</td>
</tr>
<tr>
<th>Version</th>
<td>unspecified
</td>
</tr>
<tr>
<th>Hardware</th>
<td>PC
</td>
</tr>
<tr>
<th>OS</th>
<td>Linux
</td>
</tr>
<tr>
<th>Status</th>
<td>NEW
</td>
</tr>
<tr>
<th>Severity</th>
<td>normal
</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>brycelelbach@gmail.com
</td>
</tr>
<tr>
<th>CC</th>
<td>llvm-bugs@lists.llvm.org, mclow.lists@gmail.com
</td>
</tr></table>
<p>
<div>
<pre>=========
Test Case
=========
#include <sstream>
#include <iostream>
int main() {
std::istringstream s("1.000f32");
float f;
s >> f;
std::cout << f << std::endl;
}
==================
Steps to Reproduce
==================
<a href="https://wandbox.org/permlink/3d9Z9Fr6vqzcx29w">https://wandbox.org/permlink/3d9Z9Fr6vqzcx29w</a>
===============
Observed Output
===============
0
===============
Expected Output
===============
1
This appears to be a bug in libc++'s hexadecimal `float` formatted input.
libc++'s formatted stream input implementation for `float` calls `num_get`
(ISO/IEC 14882 [istream.formatted.arithmetic]).
In the case of `float`, `num_get` accumulates valid characters until it
encounters an invalid character (stage 1), and then does the equivalent of
calling the C library function `strtof` on the accumulated string (ISO/IEC
14882 [facet.num.get.virtuals] (3.3.3) "Stage 2"). My guess is that in this
case,
libc++ considers the characters `[a-z]` (hexadecimal digits) to be valid in
addition to `[0-9+-eE]` in stage 1, but in stage 2 (the equivalent-to-`strtof`
stage), the string "1.000f32" is rejected because it doesn't have the
hexadecimal prefix (e.g. it's not "0x1.000f32").
Feeding the entire string directly into `strtof` consumes "1.000" and returns
1:
<a href="https://wandbox.org/permlink/BRWaxfmoTNewQMTo">https://wandbox.org/permlink/BRWaxfmoTNewQMTo</a>
If you change the input string to contain a character that is not a hexadecimal
digit (e.g. "1.000g32"), libc++ returns 1:
<a href="https://wandbox.org/permlink/BRWaxfmoTNewQMTo">https://wandbox.org/permlink/BRWaxfmoTNewQMTo</a>
libc++ should only accept hexadecimal digits in floats when the hexadecimal
prefix is present. Otherwise, there are ambiguities. Consider "1.000e32" - is
it a hexadecimal `float`, or a `float` with an exponent.
libstdc++ and MSVC print 1 for the test case:
libstdc++: <a href="https://wandbox.org/permlink/4EF17CZxau2CrYTA">https://wandbox.org/permlink/4EF17CZxau2CrYTA</a>
MSVC: <a href="http://rextester.com/STNGQX93481">http://rextester.com/STNGQX93481</a>
===========
Environment
===========
Platform: Godbolt (Linux Docker)
Clang/LLVM Version: Top of trunk as of 01/25/2018</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>