[llvm-bugs] [Bug 36099] New: Input stream formatted input for `float` produces incorrect result for non-hexadecimal-prefixed input containing hexadecimal characters without an exponent
via llvm-bugs
llvm-bugs at lists.llvm.org
Thu Jan 25 14:20:29 PST 2018
https://bugs.llvm.org/show_bug.cgi?id=36099
Bug ID: 36099
Summary: Input stream formatted input for `float` produces
incorrect result for non-hexadecimal-prefixed input
containing hexadecimal characters without an exponent
Product: libc++
Version: unspecified
Hardware: PC
OS: Linux
Status: NEW
Severity: normal
Priority: P
Component: All Bugs
Assignee: unassignedclangbugs at nondot.org
Reporter: brycelelbach at gmail.com
CC: llvm-bugs at lists.llvm.org, mclow.lists at gmail.com
=========
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
==================
https://wandbox.org/permlink/3d9Z9Fr6vqzcx29w
===============
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:
https://wandbox.org/permlink/BRWaxfmoTNewQMTo
If you change the input string to contain a character that is not a hexadecimal
digit (e.g. "1.000g32"), libc++ returns 1:
https://wandbox.org/permlink/BRWaxfmoTNewQMTo
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++: https://wandbox.org/permlink/4EF17CZxau2CrYTA
MSVC: http://rextester.com/STNGQX93481
===========
Environment
===========
Platform: Godbolt (Linux Docker)
Clang/LLVM Version: Top of trunk as of 01/25/2018
--
You are receiving this mail because:
You are on the CC list for the bug.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-bugs/attachments/20180125/47297be2/attachment.html>
More information about the llvm-bugs
mailing list