Skip to content

testing/ostest: Add a test of printf() for unknown and large formats. - #3978

Draft
casaroli wants to merge 1 commit into
apache:masterfrom
casaroli:apps-printf-safety-test-up
Draft

casaroli wants to merge 1 commit into
apache:masterfrom
casaroli:apps-printf-safety-test-up

Conversation

@casaroli

@casaroli casaroli commented Nov 5, 2026 •

Copy link
Copy Markdown
Contributor

depends-on: apache/nuttx/pull/21474

Summary

A test of printf() for apache/nuttx#21474. Part of apache/nuttx#21467.

printf_test() runs with the libc tests of ostest. It checks that:

  • %a, %L and %n take each argument at its right size
  • an unknown conversion takes no argument and ends the conversions
  • printf() reads no further than the end of the format
  • a width above 255 gives the right length
  • output above INT_MAX fails with EOVERFLOW
  • numbered arguments stay in order

The test adapts to CONFIG_LIBC_FLOATINGPOINT and CONFIG_LIBC_NUMBERED_ARGS. An error prints the format and stops with an assertion.

Impact

A new file in ostest and the option CONFIG_TESTING_OSTEST_PRINTF. It is on unless DEFAULT_SMALL is set, so small boards do not grow. No change to any other code.

This test fails on NuttX master until apache/nuttx#21474 merges, so this PR must merge after it.

Testing

ostest with Make, NuttX flat builds under QEMU:

Config NuttX Result
rv-virt:nsh, float off master 6e1126a2362 fails, status 256
rv-virt:nsh, float off #21474 status 0
rv-virt:nsh, float and numbered arguments on #21474 status 0
qemu-armv8a:nsh, float and numbered arguments on #21474 status 0

The failure on master:

printf_test: ERROR: "%a|%s" returned 9: "%a|(null)", expected "?|str"

These runs used f4044a05e50 of #21474.

tools/checkpatch.sh -c -u -m -g passes.

Add printf_test() to the libc tests of ostest.  It checks that printf()
takes each argument at its right size for %a, %L and %n, that an unknown
conversion takes no argument and ends the conversions, that it reads no
further than the end of the format, that a width above 255 gives the
right length, that output above INT_MAX fails with EOVERFLOW, and that
numbered arguments stay in order.

Each format goes through a function with no format attribute, because a
compiler warns about some of them.  An error prints the format and stops
with an assertion.  The test adapts to CONFIG_LIBC_FLOATINGPOINT and
CONFIG_LIBC_NUMBERED_ARGS.

Option TESTING_OSTEST_PRINTF selects the test, and it is on unless
DEFAULT_SMALL is set, so small boards do not grow.

Part of apache/nuttx#21467

Assisted-by: Claude Code:claude-opus-5-5
Signed-off-by: Marco Casaroli <marco.casaroli@gmail.com>
@casaroli
casaroli force-pushed the apps-printf-safety-test-up branch from 11cc571 to f31f43e Compare November 5, 2026 08:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants