sscanf() bug fixes from David Sidrane

This commit is contained in:
Gregory Nutt
2014-02-18 14:14:07 -06:00
parent 662622b5dc
commit 927ec9c792
+18 -4
View File
@@ -51,11 +51,19 @@
#include <debug.h> #include <debug.h>
/**************************************************************************** /****************************************************************************
* Definitions * Pre-processor Definitions
****************************************************************************/ ****************************************************************************/
#define MAXLN 128 #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 * 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 */ /* 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) */ /* Copy the string (if we are making an assignment) */
if (!noassign) if (!noassign)
{ {
strncpy(tv, buf, width); if (width > 0)
{
strncpy(tv, buf, width);
}
tv[width] = '\0'; 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 */ /* 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 /* Copy the numeric string into a temporary working