[Repo Assist] fix: clamp INT_MIN dynamic width in l_vsnprintf to avoid UB#149
Draft
github-actions[bot] wants to merge 1 commit intomainfrom
Draft
[Repo Assist] fix: clamp INT_MIN dynamic width in l_vsnprintf to avoid UB#149github-actions[bot] wants to merge 1 commit intomainfrom
github-actions[bot] wants to merge 1 commit intomainfrom
Conversation
When a negative value is passed as a dynamic width via %*d (and
friends), the spec says to treat it as a left-justify flag and a
positive width. The previous code did:
width = -width;
If the caller passes INT_MIN, negating it is undefined behaviour in
C (signed integer overflow). On x86 the result wraps back to
INT_MIN, which is still negative, and the subsequent pad calculation
overflows too — making the return value wrap to a large negative
number.
Fix: clamp INT_MIN to INT_MAX before negation, so the output is
well-defined (content printed left-justified, return value ==
INT_MAX). Add a test that exercises this path.
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This was referenced Apr 26, 2026
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 PR was created by Repo Assist, an automated AI assistant.
Summary
Fix undefined behaviour when
INT_MINis passed as a dynamic field width inl_vsnprintf(via%*dand similar specifiers).Root Cause
When a negative value is passed as a
*width argument,l_vsnprintfsetsflag_minus = 1and then negates the width:If
width == INT_MIN, the expression-INT_MINis undefined behaviour in C (signed integer overflow). On x86 hardware the result wraps back toINT_MIN, which is still negative, making the subsequent padding calculation overflow too and returning a large negative value froml_snprintf.Fix
l_os.h— one-line guard:Full
./Taskfile testsuite passes.