fix: type-conversion edge cases in TypeTools and Option reduce#1362
Merged
Conversation
Fix five verified type-conversion bugs: - Reject unsigned integral_conversion when the first non-whitespace character is '-' so " -1" no longer wraps to UINT64_MAX. - Fail float lexical_cast when strtold consumes nothing, so whitespace-only strings no longer convert to 0.0. - Value-initialize the pair elements in container-of-pairs lexical_conversion and fail on a missing second element, avoiding inserting an uninitialized value (UB). - Accumulate sum_string_vector as int64 first (with overflow check) so integer sums >= 1e16 stay exact and parseable instead of rendering in scientific notation. - Use original.size() rather than results_.size() for the Join policy in _reduce_results so default_str-derived results join. Assisted-by: ClaudeCode:claude-opus-4-8
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
🤖 AI text below 🤖
Fixes five verified type-conversion bugs in
TypeTools.hppandOption_inl.hpp, each with a regression test.Unsigned
integral_conversionaccepted" -1"asUINT64_MAX. The old guard only checkedinput.front() == '-', butstrtoullskips leading whitespace and silently wraps negatives. Now the input is rejected when its first non-whitespace character is'-'. (include/CLI/TypeTools.hpp)Float
lexical_castconverted whitespace-only strings to0.0successfully. Whenstrtoldperforms no conversion the end pointer equals the start; the trailing-whitespace skip loop then walked to the end and returnedtrue. Now we returnfalsewhen nothing was consumed (val == input.c_str()). (include/CLI/TypeTools.hpp)Uninitialized
v2could be inserted into a container-of-pairs (UB). With an odd element count the second conversion was skipped whileretvalstayedtrue, inserting{v1, v2}withv2never assigned. Both values are now value-initialized and a missing second element setsretval = false, so it raises aConversionErrorinstead of silently inserting{key, 0}. Reachable viaadd_result("onlykey"); results(map);. (include/CLI/TypeTools.hpp)sum_string_vectorbroke for integer sums >= 1e16 and lost precision above 2^53. Sums accumulated asdoubleand printed at precision 16, rendering large totals in scientific notation thatintegral_conversioncould not parse. Anint64_taccumulation (with overflow check) is now tried first, falling back to the existingdoublepath for floats/flags/non-numeric input. (include/CLI/TypeTools.hpp)Joinpolicy in_reduce_resultstestedresults_.size()instead oforiginal.size().Option::results(T&)reduces results synthesized fromdefault_str_whileresults_is empty, so the join was skipped (yielding"a"instead of"a\nb"). Now testsoriginal.size() > 1like every other policy branch. (include/CLI/impl/Option_inl.hpp)No false positives — all five reproduced against the code before the fix.
Tests added in
tests/HelpersTest.cpp(unsigned leading-whitespace negative, whitespace-only float, odd-count pair container,sum_string_vector) andtests/OptionTypeTest.cpp(Join +default_strviaresults(), large-integer Sum policy).Part of #1357