diff options
author | Gregory Nutt <gnutt@nuttx.org> | 2014-02-18 14:14:07 -0600 |
---|---|---|
committer | Gregory Nutt <gnutt@nuttx.org> | 2014-02-18 14:14:07 -0600 |
commit | 35b53fe82438622edaf30148926e6c9e4d1f8e27 (patch) | |
tree | 4236f70356f9625db17cf7f8d2532ebd1adbaa0f | |
parent | 871f5cbf4c2f6f675982023256638074c0fb51d0 (diff) | |
download | nuttx-35b53fe82438622edaf30148926e6c9e4d1f8e27.tar.gz nuttx-35b53fe82438622edaf30148926e6c9e4d1f8e27.tar.bz2 nuttx-35b53fe82438622edaf30148926e6c9e4d1f8e27.zip |
sscanf() bug fixes from David Sidrane
-rw-r--r-- | nuttx/libc/stdio/lib_sscanf.c | 22 |
1 files changed, 18 insertions, 4 deletions
diff --git a/nuttx/libc/stdio/lib_sscanf.c b/nuttx/libc/stdio/lib_sscanf.c index 72489647d..3873950bc 100644 --- a/nuttx/libc/stdio/lib_sscanf.c +++ b/nuttx/libc/stdio/lib_sscanf.c @@ -51,11 +51,19 @@ #include <debug.h> /**************************************************************************** - * Definitions + * Pre-processor Definitions ****************************************************************************/ #define MAXLN 128 +#ifndef MIN +# define MIN(a,b) (a < b ? a : b) +#endif + +#ifndef MAX +# define MAX(a,b) (a > b ? a : b) +#endif + /**************************************************************************** * Private Type Declarations ****************************************************************************/ @@ -287,14 +295,19 @@ int vsscanf(FAR const char *buf, FAR const char *fmt, va_list ap) { /* No... Guess a field width using some heuristics */ - width = findwidth(buf, fmt); + int tmpwidth = findwidth(buf, fmt); + width = MIN(sizeof(tmp) - 1, tmpwidth); } /* Copy the string (if we are making an assignment) */ if (!noassign) { - strncpy(tv, buf, width); + if (width > 0) + { + strncpy(tv, buf, width); + } + tv[width] = '\0'; } @@ -425,7 +438,8 @@ int vsscanf(FAR const char *buf, FAR const char *fmt, va_list ap) { /* No... Guess a field width using some heuristics */ - width = findwidth(buf, fmt); + int tmpwidth = findwidth(buf, fmt) + width = MIN(sizeof(tmp) - 1, tmpwidth); } /* Copy the numeric string into a temporary working |