Skip to content

libc/stdio: scanf fixes - #22240

Merged
xiaoxiang781216 merged 3 commits into
apache:masterfrom
Shanks0224:libc-stdio-scanf
Nov 25, 2026
Merged

xiaoxiang781216 merged 3 commits into
apache:masterfrom
Shanks0224:libc-stdio-scanf

Conversation

@Shanks0224

@Shanks0224 Shanks0224 commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

Note: Please adhere to Contributing Guidelines.

Summary

  • %% skips leading whitespace.
  • %p is correct on 64-bit systems.
  • Arguments of conversions that did not succeed are no longer cleared:
    sscanf("30 ", "%d %d", &a, &b) leaves b unchanged, 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:nsh builds with no warning.
  • Integer, floating point, string and char conversions on the simulator give
    the same results as glibc (%[ is not supported and was not tested).

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]>
Jackapyun and others added 2 commits November 24, 2026 13:53
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

Copy link
Copy Markdown
Contributor Author

@casaroli @jerpelea @xiaoxiang781216 could you review? These are the scanf fixes split from #22179 as requested.

@github-actions github-actions Bot added Area: OS Components OS Components issues Size: S The size of the change in this PR is small labels Nov 24, 2026
@github-actions

Copy link
Copy Markdown

@xiaoxiang781216
xiaoxiang781216 merged commit 586d2c4 into apache:master Nov 25, 2026
58 checks passed
@Shanks0224
Shanks0224 deleted the libc-stdio-scanf branch November 26, 2026 02:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Area: OS Components OS Components issues Size: S The size of the change in this PR is small

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants