This is an automated email from the ASF dual-hosted git repository.

xiaoxiang781216 pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx.git

commit b7d5a9bb027d0cdc72a6e375a272df6817533c42
Author: Marco Casaroli <[email protected]>
AuthorDate: Thu Nov 5 06:51:55 2026 +0100

    libs/libc/stream: Do not truncate the digits of a float conversion.
    
    vsprintf_internal() kept the number of digits in a uint8_t, and set it
    from the precision before it applied the limit.  "%.255e" and "%.767e"
    asked for 0 digits, so the output began with a NUL byte.  "%.256f" asked
    for no limit of decimals and "%.257f" for 1 decimal.
    
    Apply the limit first, and use an unsigned int.
    
    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/stream/lib_libvsprintf.c | 27 +++++++++++++--------------
 1 file changed, 13 insertions(+), 14 deletions(-)

diff --git a/libs/libc/stream/lib_libvsprintf.c 
b/libs/libc/stream/lib_libvsprintf.c
index d19e11ed603..029e9bf934f 100644
--- a/libs/libc/stream/lib_libvsprintf.c
+++ b/libs/libc/stream/lib_libvsprintf.c
@@ -663,14 +663,13 @@ set_star:
           int exp;              /* Exponent of master decimal digit */
           int n;
           uint8_t sign;         /* Sign character (or 0) */
-          uint8_t ndigs;        /* Number of digits to convert */
-          uint8_t ndecimal;     /* Digits after decimal (for 'f' format), 0 if
+          unsigned int ndigs;   /* Number of digits to convert */
+          int ndecimal;         /* Digits after decimal (for 'f' format), 0 if
                                  * no limit */
 
           flags &= ~FL_FLTUPP;
 
 flt_oper:
-          ndigs = 0;
           if ((flags & FL_PREC) == 0)
             {
               prec = 6;
@@ -678,27 +677,27 @@ flt_oper:
 
           flags &= ~(FL_FLTEXP | FL_FLTFIX);
 
+          ndigs = MIN(prec, DTOA_MAX_DIG);
+          ndecimal = 0;
+
           if (c == 'e')
             {
-              ndigs = prec + 1;
-              ndecimal = 0;
+              if (ndigs < DTOA_MAX_DIG)
+                {
+                  ndigs++;
+                }
+
               flags |= FL_FLTEXP;
             }
           else if (c == 'f')
             {
+              ndecimal = ndigs;
               ndigs = DTOA_MAX_DIG;
-              ndecimal = prec;
               flags |= FL_FLTFIX;
             }
-          else
+          else if (ndigs == 0)
             {
-              ndigs = prec != 0 ? prec : 1;
-              ndecimal = 0;
-            }
-
-          if (ndigs > DTOA_MAX_DIG)
-            {
-              ndigs = DTOA_MAX_DIG;
+              ndigs = 1;
             }
 
 #ifdef CONFIG_LIBC_NUMBERED_ARGS

Reply via email to