2 Commits

Author SHA1 Message Date
Philip Top
c4a6f31bd9 Tests (#1165)
Handle RTTI in a consistent way for locale inclusion for integral conversion.  
Resolve some missing code coverage lines.

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Alexander Galanin <agalanin@nvidia.com>
2025-05-26 10:35:22 -07:00
Alexander Galanin
3d9eb4e0a0 Default value conversion error if the application locale is set (#1160)
**Short description**
Application fails with `CLI::ConversionError` in some locales if the
application locale is set.

**Root cause**
CLI11 uses `std::stringstream` to format default value and
`strtol`/`strtod` to parse them back. The first method adds
locale-specific thousand separators but the second method parses the
number only up to the first separator. This causes an exception when the
same value is converted to string representation and then parsed back.

Examples: in _en_US_ locale 1234.56 is "1,234.56" and in de_DE locale
1234.56 is "1.234,56".

**Reproduction**
```c++
#include <string>
#include <CLI/CLI.hpp>

int main(int argc, const char **argv)
{
    std::locale::global(std::locale(""));
    CLI::App app{"Bug report app"};
    long foo;

    app.add_option("FOO", foo, "Foo option")
        ->envname("FOO")
        ->default_val(1234567);
    CLI11_PARSE(app, argc, argv);

    return 0;
}
```
Output:
```
$ g++ -Wall -Wextra -I CLI11/include/ cli11-bug-locale-string.cpp -o cli11-bug-locale-string
$ for locale in C en_US de_DE fr_FR de_CH; do echo "locale $locale"; LC_NUMERIC=$locale.UTF-8 ./cli11-bug-locale-string && echo OK; echo; done
locale C
OK

locale en_US
terminate called after throwing an instance of 'CLI::ConversionError'
what():  The value FOO is not an allowed value for given default value("1,234,567") produces an error : Could not convert: FOO = 1,234,567
Aborted (core dumped)

locale de_DE
terminate called after throwing an instance of 'CLI::ConversionError'
what():  The value FOO is not an allowed value for given default value("1.234.567") produces an error : Could not convert: FOO = 1.234.567
Aborted (core dumped)

locale fr_FR
terminate called after throwing an instance of 'CLI::ConversionError'
what():  The value FOO is not an allowed value for given default value("1 234 567") produces an error : Could not convert: FOO = 1 234 567
Aborted (core dumped)

locale de_CH
OK
```

**Fix description**
The fix strips group separators from the input string. This extends a
bit the logic introduced in change #968 for `_` and `'` separators.
There are no unit tests for the change because it requires to have all
mentioned locales to be installed in the system.

---------

Co-authored-by: Philip Top <phlptp@gmail.com>
2025-05-25 16:26:47 -07:00