sscanf(): NuttX libc tried to guess how many characters to parse, extracted them into a buffer, then ran strtol() on that buffer. That guess is often wrong. A better approach would be to call strtol() directly on the input data, using the endptr return value to determine how many characters to skip after parsing. From Kosma Moczek

This commit is contained in:
Gregory Nutt 2014-08-18 07:33:17 -06:00
parent a8d7772ad6
commit a325cb9f0d

View File

@ -405,6 +405,10 @@ int vsscanf(FAR const char *buf, FAR const char *fmt, va_list ap)
if (*buf) if (*buf)
{ {
FAR char *endptr;
int errsave;
long tmplong;
/* Skip over any white space before the integer string */ /* Skip over any white space before the integer string */
while (isspace(*buf)) while (isspace(*buf))
@ -459,35 +463,32 @@ int vsscanf(FAR const char *buf, FAR const char *fmt, va_list ap)
lvdbg("vsscanf: tmp[]=\"%s\"\n", tmp); lvdbg("vsscanf: tmp[]=\"%s\"\n", tmp);
/* Perform the integer conversion */ /* Perform the integer conversion */
/* Preserve the errno value */
buf += width; errsave = get_errno();
set_errno(0);
if (sign)
{
tmplong = strtol(tmp, &endptr, base);
}
else
{
tmplong = strtoul(tmp, &endptr, base);
}
/* Check if the number was successfully converted */
if (tmp == endptr || get_errno() == ERANGE)
{
return count;
}
/* Move by the actual number of characters converted */
buf += (endptr - tmp);
set_errno(errsave);
if (!noassign) if (!noassign)
{ {
FAR char *endptr;
int errsave;
long tmplong;
/* Preserve the errno value */
errsave = get_errno();
set_errno(0);
if (sign)
{
tmplong = strtol(tmp, &endptr, base);
}
else
{
tmplong = strtoul(tmp, &endptr, base);
}
/* Check if the number was successfully converted */
if (tmp == endptr || get_errno() == ERANGE)
{
return count;
}
set_errno(errsave);
/* We have to check whether we need to return a long /* We have to check whether we need to return a long
* or an int. * or an int.