Skip to content

<xlocnum>: num_get::do_get for bool stores true in the result when the input has bad grouping, even if the input is zero #5470

Description

@cpplearner

Describe the bug

In num_get::do_get, if _Parse_result._Bad_grouping is true, the result is set to true, regardless of the converted value of the input (_Ans).

STL/stl/inc/xlocnum

Lines 376 to 379 in 8e9f4ee

if (_Ep == _Ac || _Errno != 0 // N4950 [facet.num.get.virtuals]/3
|| _Parse_result._Bad_grouping) { // N4950 [facet.num.get.virtuals]/4
_Val = true;
_State = ios_base::failbit;

However, if I understand correctly, false should be stored when _Ans is 0 (which may happen if the input is a sequence of 0 with a misplaced thousand separator, e.g. 0,0000).

N5008 [facet.num.get.virtuals]/6, emphasis mine:

If (str.flags() & ios_base::boolalpha) == 0 then input proceeds as it would for a long
except that if a value is being stored into val, the value is determined according to the following: If
the value to be stored is 0 then false is stored.
If the value is 1 then true is stored. Otherwise true
is stored and ios_base::failbit is assigned to err.

The test case below uses istream::operator>> which is defined in terms of num_get::do_get.

Command-line test case

D:\test>type test-get-bool.cpp
#include <iostream>
#include <sstream>

int main() {
    bool x;
    std::istringstream s{"0,0000"};

    std::locale loc{"en_US"};
    s.imbue(loc);

    s >> x;

    std::cout << x << '\n';
}

D:\test>cl /EHs test-get-bool.cpp
Microsoft (R) C/C++ Optimizing Compiler Version 19.44.35109.1 for x64
Copyright (C) Microsoft Corporation.  All rights reserved.

test-get-bool.cpp
Microsoft (R) Incremental Linker Version 14.44.35109.1
Copyright (C) Microsoft Corporation.  All rights reserved.

/out:test-get-bool.exe
test-get-bool.obj

D:\test>test-get-bool.exe
1

Expected behavior

test-get-bool.exe should output 0.

STL version

Microsoft Visual Studio Community 2022
Version 17.14.0 Preview 3.0

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingfixedSomething works now, yay!

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions