Repository navigation
libs/libc/stream: Print nothing for %n when LIBC_PRINTF_PERCENT_N is off. - #21595
Merged
Merged
Conversation
…off. Since apache#21474, printf() takes the pointer of %n, stores nothing and prints "?" when LIBC_PRINTF_PERCENT_N is off. But the correct output of %n is empty, so "?" changes the text and the return value of the call. The safety comes from the missing store, not from the "?". Now %n prints nothing also with the option off. It still reads its pointer and stores nothing. With the option on nothing changes: %n stores the count and prints nothing. A width or flags on %n have no effect, as C leaves them undefined. Update the help of the option. Part of apache#21467 Assisted-by: Claude Code:claude-opus-5-5 Signed-off-by: Marco Casaroli <[email protected]>
%n now prints nothing also when LIBC_PRINTF_PERCENT_N is off. Change the text and the examples of %n on the page on printf. Part of apache#21467 Assisted-by: Claude Code:claude-opus-5-5 Signed-off-by: Marco Casaroli <[email protected]>
xiaoxiang781216
approved these changes
Nov 9, 2026
|
acassis
approved these changes
Nov 9, 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.
Part of #21467.
Summary
Since #21474,
printf()takes the pointer of%n, stores nothing and prints?whenLIBC_PRINTF_PERCENT_Nis off. But the correct output of%nis empty: the?changes the text and the return value of the call. The safety comes from the missing store, not from the?.Now
%nprints nothing also with the option off. It still reads its pointer and stores nothing, so a format still cannot write to memory. With the option on, nothing changes:%nstores the count and prints nothing.A width or flags on
%nhave no effect, as C leaves them undefined.Two commits: the code and the help of
LIBC_PRINTF_PERCENT_N, then the pageDocumentation/os/libs/libc/printf.rst.Impact
A program sees one change: with
LIBC_PRINTF_PERCENT_Noff (the default in all profiles),%nprinted?and now prints nothing, so the text and the return value of the call change. With the option on, nothing changes. No other output changes. No new option. No ABI change. No architecture code. Flat, protected and kernel builds.No program in nuttx or nuttx-apps prints with
%n, exceptprintftest. Its update is in apache/nuttx-apps#3994.Size (
textof the image, andlib_libvsprintf.o, in bytes; the stack frame ofvsprintf_internal()does not change):nucleo-f072rb:nsh(Cortex-M0), Defaultnucleo-f072rb:nsh, Minimalnucleo-f072rb:nsh, Fullqemu-armv8a:nsh(arm64), Defaultlib_libvsprintf.o, Cortex-M0, DefaultThe
?path of%nis gone, so the code is shorter. On Cortex-M0 GCC then puts the blocks ofvsprintf_internal()in another order, and three short conditional branches need one more jump each: +12 bytes in Default. In Full it is 8 bytes smaller.Testing
With apache/nuttx-apps#3994 (
printftest,printfengine), under QEMU with HVF, then ostest; each run passes and ostest exits with status 0:qemu-armv8a:knsh(kernel)qemu-armv8a:knshqemu-armv8a:knshqemu-armv8a:knshLIBC_PRINTF_OCTAL=n,LIBC_PRINTF_LEFT_JUSTIFY=nqemu-armv8a:knshLIBC_PRINTF_PERCENT_N=yqemu-armv8a:knshLIBC_PRINTF_PERCENT_N=y,LIBC_PRINTF_OCTAL=n,LIBC_PRINTF_LEFT_JUSTIFY=nqemu-armv8a:nsh(flat)qemu-armv8a:nshLIBC_PRINTF_PERCENT_N=yFull has
LIBC_NUMBERED_ARGSon, so%2$s%1$nruns too.printfengineneeds floating point, so it does not run with Minimal. The test checks the text and the return value, for examplesnprintf(NULL, 0, "ab%n|%s", &n, "str")returns 6, and checks the store only with the option on.On master the same test fails with the option off, as it must:
"ab%n|%s" returned 7: "ab?|str", expected "ab|str"(qemu-armv8a:knsh, Default). With the option on it passes on master too.The examples of
%non the page come from a run of the examples of the page onrv-virt:nsh(rv32, QEMU) with Full, Full withLIBC_PRINTF_PERCENT_N=y, Default, Minimal and Minimal with all options off. The page made from these runs is the same as the page in this PR. With the option on, the example stores 2 inn; in all other runs it stores nothing.Builds with
-Werror:qemu-armv8a:nsh(Default; Full withLIBC_PRINTF_PERCENT_N=y),qemu-armv8a:knsh(the same two),rv-virt:nsh64(the same two),nucleo-f072rb:nsh(Full withLIBC_PRINTF_PERCENT_N=y; Default, Minimal and Full for the size).qemu-intel64:nsh(GCC 16.2, Default, both test programs on) builds, but not with-Werror: its only warning is inlibs/libm/libm/lib_gamma.c(sinpishadows a built-in function), on master too.The documentation build with
-Wgives one warning, for another page:plantumlis not installed on the build host (guides/porting/port_bootsequence.rst). There is no warning forprintf.rst.tools/checkpatch.sh -c -u -m -gpasses.Not run: a CMake build, a protected build, hardware, the suite at https://github.com/casaroli/printf-report.