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 586d2c4c751c9f38b08782b507c0d638519af936 Author: ganjing <[email protected]> AuthorDate: Mon Nov 23 10:24:03 2026 +0800 libc/stdio: Do not overwrite scanf arguments that were not converted. vscanf_internal() cleared the destination of every integer, floating point and string conversion before it knew whether the conversion would succeed. When the input ran out or did not match, the argument of the failed directive was therefore set to 0 or "" instead of keeping its value, which C and POSIX require. For example a = b = -7; sscanf("30 ", "%d %d", &a, &b); returned 1 with b == 0 instead of b == -7. The argument pointer still has to be fetched up front so that the va_list stays in step, but nothing is written to it until a conversion has succeeded. Run on the simulator against glibc for integer (%d %x %o %u with hh/h/l/ll), float and %s/%c cases, the results now match. Signed-off-by: ganjing <[email protected]> --- libs/libc/stream/lib_libvscanf.c | 11 ----------- 1 file changed, 11 deletions(-) diff --git a/libs/libc/stream/lib_libvscanf.c b/libs/libc/stream/lib_libvscanf.c index a574979b9e8..12cd425e354 100644 --- a/libs/libc/stream/lib_libvscanf.c +++ b/libs/libc/stream/lib_libvscanf.c @@ -401,7 +401,6 @@ static int vscanf_internal(FAR struct lib_instream_s *stream, FAR int *lastc, if (!noassign) { tv = next_arg_width(varg, vabuf, FAR char *, width); - tv[0] = '\0'; } /* Skip over white space */ @@ -461,7 +460,6 @@ static int vscanf_internal(FAR struct lib_instream_s *stream, FAR int *lastc, if (!noassign) { tv = next_arg_width(varg, vabuf, FAR char *, width); - tv[0] = '\0'; } /* But we only perform the data conversion is we still have @@ -518,7 +516,6 @@ static int vscanf_internal(FAR struct lib_instream_s *stream, FAR int *lastc, if (!noassign) { tv = next_arg_width(varg, vabuf, FAR char *, width); - tv[0] = '\0'; } /* But we only perform the data conversion is we still have @@ -612,29 +609,24 @@ static int vscanf_internal(FAR struct lib_instream_s *stream, FAR int *lastc, { case HH_MOD: pchar = next_arg(varg, vabuf, FAR unsigned char *); - *pchar = 0; break; case H_MOD: pshort = next_arg(varg, vabuf, FAR unsigned short *); - *pshort = 0; break; case NO_MOD: pint = next_arg(varg, vabuf, FAR unsigned int *); - *pint = 0; break; default: case L_MOD: plong = next_arg(varg, vabuf, FAR unsigned long *); - *plong = 0; break; case LL_MOD: plonglong = next_arg(varg, vabuf, FAR unsigned long long *); - *plonglong = 0; break; } } @@ -983,7 +975,6 @@ static int vscanf_internal(FAR struct lib_instream_s *stream, FAR int *lastc, if (modifier == LL_MOD) { pld = next_arg(varg, vabuf, FAR long double *); - *pld = 0.0; } else #endif @@ -991,14 +982,12 @@ static int vscanf_internal(FAR struct lib_instream_s *stream, FAR int *lastc, if (modifier >= L_MOD) { pd = next_arg(varg, vabuf, FAR double *); - *pd = 0.0; } else #endif #ifdef CONFIG_HAVE_FLOAT { pf = next_arg(varg, vabuf, FAR float *); - *pf = 0.0; } #endif }
