[Repo Assist] fix: l_strtoul/l_strtoull return ULONG_MAX on negative overflow#161
Draft
github-actions[bot] wants to merge 1 commit intomainfrom
Draft
Conversation
C99 §7.20.1.4 requires strtoul/strtoull to return ULONG_MAX/ULLONG_MAX
when the converted value cannot be represented (i.e. on overflow),
regardless of the sign of the input.
Previously, when both the overflow flag and the neg flag were set
(e.g. strtoul("-99999999999999999999999", NULL, 10)), the code applied
a signed negation to the overflow sentinel ULONG_MAX:
(unsigned long)(-(long)ULONG_MAX)
On a typical two's-complement 64-bit host this yields 1, not ULONG_MAX.
Fix: return ULONG_MAX/ULLONG_MAX immediately when overflow is detected,
before the sign negation. Also replace the signed intermediate cast
with unsigned subtraction (0UL - acc) to avoid implementation-defined
behaviour on the non-overflow negative path.
Add regression tests:
strtoul("-99999999999999999999999") -> ULONG_MAX
strtoull("-99999999999999999999999") -> ULLONG_MAX
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
26 tasks
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.
🤖 This is an automated pull request from Repo Assist.
Problem
l_strtoulandl_strtoullreturn the wrong value when both the overflow and negative sign conditions fire simultaneously — e.g.:C99 §7.20.1.4 requires these functions to return
ULONG_MAX(orULLONG_MAX) whenever the correct mathematical value cannot be represented, regardless of the sign of the input.Root cause
When overflow is detected, the accumulator is clamped to
ULONG_MAX. Then the return path applied a signed negation: