Skip to content

[libcxx] operator>>(integer): incorrect hex prefix recognition for basefield == 16 #158100

@morinmorin

Description

@morinmorin

For basefield == 16, there is a small bug (L159 in __locale_dir/num.h) on how to recognize the prefix. As a result, reading from "10xFFx01" (with basefield == 16) consumes "10xFF".

Testcase:

#include <iostream>
#include <iomanip>
#include <sstream>

int main()
{
    std::istringstream is("10xFFx01");
    is >> std::setbase(16);

    int x;
    is >> x;
    bool failbit = is.fail();
    if (failbit) is.clear();
    auto next_pos = is.tellg();

    std::cout << "value: " << x << '\n'; // expected: 16, actual: 0
    std::cout << "fail: " << failbit << '\n'; // expected: 0, actual: 1
    std::cout << "next: " << next_pos << '\n'; // expected: 2 (1st 'x'), actual: 5 (2nd 'x')
}

The fix would be

--- a/libcxx/include/__locale_dir/num.h
+++ b/libcxx/include/__locale_dir/num.h
@@ -159 +159,4 @@ int __num_get<_CharT>::__stage2_int_loop(
-    if (__a_end != __a && __a_end - __a <= 2 && __a_end[-1] == '0') {
+    if (
+        (__a_end - __a == 1 && __a_end[-1] == '0')
+     || (__a_end - __a == 2 && (__a_end[-2] == '+' || __a_end[-2] == '-') && __a_end[-1] == '0')
+) {

Metadata

Metadata

Assignees

No one assigned

    Labels

    libc++libc++ C++ Standard Library. Not GNU libstdc++. Not libc++abi.localeissues related to localization

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions