Skip to content
Snippets Groups Projects
Commit a325cb9f authored by Gregory Nutt's avatar Gregory Nutt
Browse files

sscanf(): NuttX libc tried to guess how many characters to parse, extracted...

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
parent a8d7772a
No related branches found
No related tags found
No related merge requests found
......@@ -405,6 +405,10 @@ int vsscanf(FAR const char *buf, FAR const char *fmt, va_list ap)
if (*buf)
{
FAR char *endptr;
int errsave;
long tmplong;
/* Skip over any white space before the integer string */
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);
/* Perform the integer conversion */
/* Preserve the errno value */
buf += width;
if (!noassign)
errsave = get_errno();
set_errno(0);
if (sign)
{
FAR char *endptr;
int errsave;
long tmplong;
/* Preserve the errno value */
tmplong = strtol(tmp, &endptr, base);
}
else
{
tmplong = strtoul(tmp, &endptr, base);
}
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 */
/* Check if the number was successfully converted */
if (tmp == endptr || get_errno() == ERANGE)
{
return count;
}
if (tmp == endptr || get_errno() == ERANGE)
{
return count;
}
/* Move by the actual number of characters converted */
set_errno(errsave);
buf += (endptr - tmp);
set_errno(errsave);
if (!noassign)
{
/* We have to check whether we need to return a long
* or an int.
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment