Repository navigation
libc/stdio: scanf fixes - #22240
Merged
Merged
libc/stdio: scanf fixes#22240
Conversation
vscanf_internal() cleared the destination of every integer, floating
point and string conversion before it knew whether the conversion would
succeed. When the input ran out or did not match, the argument of the
failed directive was therefore set to 0 or "" instead of keeping its
value, which C and POSIX require. For example
a = b = -7;
sscanf("30 ", "%d %d", &a, &b);
returned 1 with b == 0 instead of b == -7.
The argument pointer still has to be fetched up front so that the
va_list stays in step, but nothing is written to it until a conversion
has succeeded. Run on the simulator against glibc for integer (%d %x %o
%u with hh/h/l/ll), float and %s/%c cases, the results now match.
Signed-off-by: ganjing <[email protected]>
In C, "%%" is a conversion specification although nothing is converted
or assigned, so the rule that a conversion first skips white space
applies to it. For example, scanf("%%%c", &a) with the input " \t\n%a"
must set a to 'a'. NuttX did not skip the white space, so the '%' did
not match and a was not set. Skip white space before matching "%%".
Signed-off-by: anpeiyun <[email protected]>
Signed-off-by: ganjing <[email protected]>
The %p format specifier in sscanf was not properly handling pointer
sizes on 64-bit systems. Without an explicit length modifier, the
conversion defaulted to 'unsigned int' (typically 4 bytes), causing
the upper 32 bits of 64-bit pointers to be truncated.
This patch adds automatic modifier selection based on pointer size
before processing the %p conversion. The modifier is set according
to sizeof(void *):
- 2 bytes -> H_MOD (short)
- 4 bytes -> NO_MOD (int)
- 8 bytes -> L_MOD (long) or LL_MOD (long long)
The %p case in the switch statement continues to share the hexadecimal
parsing logic with %x and %X, but now uses the correct type based on
the platform's pointer size.
This ensures that sscanf("%p", &ptr) correctly reads the full pointer
value on both 32-bit and 64-bit architectures.
Signed-off-by: liaoao <[email protected]>
Signed-off-by: ganjing <[email protected]>
Shanks0224
force-pushed
the
libc-stdio-scanf
branch
from
November 24, 2026 05:54
a359c71 to
f02cde3
Compare
Contributor
Author
|
@casaroli @jerpelea @xiaoxiang781216 could you review? These are the scanf fixes split from #22179 as requested. |
|
xiaoxiang781216
approved these changes
Nov 24, 2026
GUIDINGLI
approved these changes
Nov 24, 2026
jerpelea
approved these changes
Nov 24, 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.
Note: Please adhere to Contributing Guidelines.
Summary
%%skips leading whitespace.%pis correct on 64-bit systems.sscanf("30 ", "%d %d", &a, &b)leavesbunchanged, as C and POSIX require.Split from #22179.
Impact
Code that relied on unmatched scanf arguments being zeroed now sees the old
value. No impact on the build system, documentation or hardware.
Testing
Build host: Linux x86_64.
sim:nshbuilds with no warning.the same results as glibc (
%[is not supported and was not tested).