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 83f6e93141acd1a2ccab8a9c5d6900ad16b8f264
Author: anpeiyun <[email protected]>
AuthorDate: Tue Sep 2 15:48:46 2025 +0800

    libc/stdio: Skip leading whitespace when scanf() processes "%%".
    
    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]>
---
 libs/libc/stream/lib_libvscanf.c | 9 +++++++++
 1 file changed, 9 insertions(+)

diff --git a/libs/libc/stream/lib_libvscanf.c b/libs/libc/stream/lib_libvscanf.c
index 7915d57cfc5..f4e0971d39e 100644
--- a/libs/libc/stream/lib_libvscanf.c
+++ b/libs/libc/stream/lib_libvscanf.c
@@ -1212,6 +1212,15 @@ static int vscanf_internal(FAR struct lib_instream_s 
*stream, FAR int *lastc,
             }
           else if (fmt_char(fmt) == '%')
             {
+              /* In the C Standard "%%" is a "conversion specification" and
+               * the rule about consuming whitespace applies.
+               */
+
+              while (isspace(c))
+                {
+                  c = lib_stream_getc(stream);
+                }
+
               if (c != '%')
                 {
                   break;

Reply via email to