This is an automated email from the ASF dual-hosted git repository. acassis pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/nuttx.git
commit ea20d05e544dcb2cc39a6dc020001651c7a2da81 Author: Marco Casaroli <[email protected]> AuthorDate: Thu Nov 5 12:06:49 2026 +0100 libs/libc/stream: Add an option for the precision of an integer in printf(). With LIBC_PRINTF_INT_PRECISION off, a conversion such as %.3d reads its argument and prints "?". The precision of %s and of a floating point conversion does not change. The option is on in the Default and Full profiles. Part of https://github.com/apache/nuttx/issues/21467 Assisted-by: Claude Code:claude-opus-5-5 Signed-off-by: Marco Casaroli <[email protected]> --- libs/libc/stdio/Kconfig | 8 ++++++++ libs/libc/stream/lib_libvsprintf.c | 23 ++++++++++++++++++++--- 2 files changed, 28 insertions(+), 3 deletions(-) diff --git a/libs/libc/stdio/Kconfig b/libs/libc/stdio/Kconfig index 6727c0dbb97..ab129096019 100644 --- a/libs/libc/stdio/Kconfig +++ b/libs/libc/stdio/Kconfig @@ -115,6 +115,14 @@ config LIBC_PRINTF_LEFT_JUSTIFY Enables left-justified output, as in %-5d. Without it, printf() ignores the flag and pads on the left. +config LIBC_PRINTF_INT_PRECISION + bool "Enable the precision of an integer in printf" + default !LIBC_PRINTF_MINIMAL + ---help--- + Enables a precision such as %.3d. Without it, printf() reads the + argument and prints "?". The precision of %s and of a floating + point conversion stays. + config LIBC_FLOATINGPOINT bool "Enable floating point in printf" default LIBC_PRINTF_FULL || (!DEFAULT_SMALL && ARCH_FPU && !LIBC_PRINTF_MINIMAL) diff --git a/libs/libc/stream/lib_libvsprintf.c b/libs/libc/stream/lib_libvsprintf.c index 3572073a8dd..bfb904eceb9 100644 --- a/libs/libc/stream/lib_libvsprintf.c +++ b/libs/libc/stream/lib_libvsprintf.c @@ -124,6 +124,18 @@ #define FL_ALT_IS_HEX(f) (FL_ALT_FORM == 0 || !HAVE_OCTAL_OUTPUT || \ ((f) & FL_ALTHEX) != 0) +/* An integer conversion with a feature that is off prints a question mark. + * A flag that is 0 lets the compiler remove the code of its feature. + */ + +#ifdef CONFIG_LIBC_PRINTF_INT_PRECISION +# define FL_INT_PREC FL_PREC +# define FL_INT_PREC_UNAVAIL 0 +#else +# define FL_INT_PREC 0 +# define FL_INT_PREC_UNAVAIL FL_PREC +#endif + /* A value longer than long has output only with this option */ #if defined(CONFIG_LIBC_PRINTF_LONG_LONG) || ULLONG_MAX == ULONG_MAX @@ -1417,7 +1429,7 @@ str_lpad: flags |= FL_NEGATIVE; } - if ((flags & FL_PREC) != 0 && prec == 0 && x == 0) + if ((flags & FL_INT_PREC) != 0 && prec == 0 && x == 0) { c = 0; } @@ -1611,7 +1623,7 @@ str_lpad: break; } - if ((flags & FL_PREC) != 0 && prec == 0 && x == 0) + if ((flags & FL_INT_PREC) != 0 && prec == 0 && x == 0) { c = 0; } @@ -1623,9 +1635,14 @@ str_lpad: flags &= ~FL_NEGATIVE; } + if ((flags & FL_INT_PREC_UNAVAIL) != 0) + { + goto put_unavailable; + } + len = c; - if ((flags & FL_PREC) != 0) + if ((flags & FL_INT_PREC) != 0) { flags &= ~FL_ZFILL; if (len < prec)
