sscanf: Use stroul() vs strol() for %u format. From kfrolov

This commit is contained in:
Gregory Nutt 2014-04-02 09:15:40 -06:00
parent 6201df6463
commit ed294643bd
2 changed files with 32 additions and 14 deletions

View File

@ -7108,3 +7108,7 @@
DBGU (2014-4-1). DBGU (2014-4-1).
* configs/sama5d3-xplained/nsh: Configurations now use the DBGU for * configs/sama5d3-xplained/nsh: Configurations now use the DBGU for
the serial console (instead of USART1) (2014-4-1). the serial console (instead of USART1) (2014-4-1).
* libc/stdio/lib_sccanf.c: Use stroul() vs strol() with %u format
otherwise, range of values is restricted because of sign bit
from kfrolov (2014-4-2).

View File

@ -372,6 +372,7 @@ int vsscanf(FAR const char *buf, FAR const char *fmt, va_list ap)
{ {
FAR long *plong = NULL; FAR long *plong = NULL;
FAR int *pint = NULL; FAR int *pint = NULL;
bool sign;
lvdbg("vsscanf: Performing integer conversion\n"); lvdbg("vsscanf: Performing integer conversion\n");
@ -415,24 +416,30 @@ int vsscanf(FAR const char *buf, FAR const char *fmt, va_list ap)
* specific conversion specification. * specific conversion specification.
*/ */
if (*fmt == 'd' || *fmt == 'u') sign = false;
switch (*fmt)
{ {
default:
case 'd':
sign = true;
case 'u':
base = 10; base = 10;
} break;
else if (*fmt == 'x')
{ case 'x':
base = 16; base = 16;
} break;
else if (*fmt == 'o')
{ case 'o':
base = 8; base = 8;
} break;
else if (*fmt == 'b')
{ case 'b':
base = 2; base = 2;
break;
} }
/* Was a fieldwidth specified? */ /* Was a field width specified? */
if (!width) if (!width)
{ {
@ -464,9 +471,16 @@ int vsscanf(FAR const char *buf, FAR const char *fmt, va_list ap)
errsave = get_errno(); errsave = get_errno();
set_errno(0); set_errno(0);
tmplong = strtol(tmp, &endptr, base); if (sign)
{
tmplong = strtol(tmp, &endptr, base);
}
else
{
tmplong = strtoul(tmp, &endptr, base);
}
/* Number can't be converted */ /* Check if the number was successfully converted */
if (tmp == endptr || get_errno() == ERANGE) if (tmp == endptr || get_errno() == ERANGE)
{ {
@ -580,7 +594,7 @@ int vsscanf(FAR const char *buf, FAR const char *fmt, va_list ap)
set_errno(0); set_errno(0);
dvalue = strtod(tmp, &endptr); dvalue = strtod(tmp, &endptr);
/* Number can't be converted */ /* Check if the number was successfully converted */
if (tmp == endptr || get_errno() == ERANGE) if (tmp == endptr || get_errno() == ERANGE)
{ {