From 7ee82c0168c0b8464aa6e36781d06f48a3eccee2 Mon Sep 17 00:00:00 2001 From: David Sidrane Date: Sat, 15 Oct 2016 05:15:40 -1000 Subject: [PATCH] Fixes sscan %sn where strlen(data) < n --- libc/stdio/lib_sscanf.c | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/libc/stdio/lib_sscanf.c b/libc/stdio/lib_sscanf.c index ae8774cc57e..50bcd8956eb 100644 --- a/libc/stdio/lib_sscanf.c +++ b/libc/stdio/lib_sscanf.c @@ -174,6 +174,7 @@ int vsscanf(FAR const char *buf, FAR const char *fmt, va_list ap) bool noassign; int count; int width; + int fwidth; int base = 10; char tmp[MAXLN]; @@ -275,16 +276,22 @@ int vsscanf(FAR const char *buf, FAR const char *fmt, va_list ap) buf++; } - /* Was a fieldwidth specified? */ + /* Guess a field width using some heuristics */ - if (!width) + fwidth = findwidth(buf, fmt); + + /* Use the actual field's width if 1) no fieldwidth + * specified or 2) the actual field's width is smaller + * than fieldwidth specified + */ + + if (!width || fwidth < width) { - /* No... Guess a field width using some heuristics */ - - int tmpwidth = findwidth(buf, fmt); - width = MIN(sizeof(tmp) - 1, tmpwidth); + width = fwidth; } + width = MIN(sizeof(tmp) - 1, width); + /* Copy the string (if we are making an assignment) */ if (!noassign)