libpayload: Deduplicate strtol and strtoull

Our strtol() and strtoull() function contain almost exactly the same
code. This is a) bad in general and b) may cause the code to get out of
sync, such as it recently happened with CB:32029.

This patch changes strtol() to be based on strtoull() so that the main
parsing code exists only once, and also adds a strtoll() to round off
the library. Also fix the bounds imposed by strtoul() to be based on the
actual length of a 'long', not hardcoded to 32-bits (which is not
equivalent on all architectures).

Change-Id: I919c65a773cecdb11739c3f22dd0d182ed50c07f
Signed-off-by: Julius Werner <jwerner@chromium.org>
Reviewed-on: https://review.coreboot.org/c/coreboot/+/32086
Tested-by: build bot (Jenkins) <no-reply@coreboot.org>
Reviewed-by: Patrick Georgi <pgeorgi@google.com>
This commit is contained in:
Julius Werner 2019-03-27 14:47:09 -07:00 committed by Patrick Georgi
parent 18c1b6b240
commit 0de2fa0a62
2 changed files with 26 additions and 40 deletions

View File

@ -186,6 +186,7 @@ static inline void *xmemalign_work(size_t align, size_t size, const char *file,
* @{ * @{
*/ */
long int strtol(const char *s, char **nptr, int base); long int strtol(const char *s, char **nptr, int base);
long long int strtoll(const char *s, char **nptr, int base);
unsigned long int strtoul(const char *s, char **nptr, int base); unsigned long int strtoul(const char *s, char **nptr, int base);
unsigned long long int strtoull(const char *s, char **nptr, int base); unsigned long long int strtoull(const char *s, char **nptr, int base);
long atol(const char *nptr); long atol(const char *nptr);

View File

@ -33,6 +33,7 @@
#include <string.h> #include <string.h>
#include <ctype.h> #include <ctype.h>
#include <inttypes.h> #include <inttypes.h>
#include <limits.h>
#include <errno.h> #include <errno.h>
/** /**
@ -419,59 +420,43 @@ static int _offset(char ch, int base)
* @return A signed integer representation of the string * @return A signed integer representation of the string
*/ */
long int strtol(const char *ptr, char **endptr, int base) long long int strtoll(const char *orig_ptr, char **endptr, int base)
{ {
int ret = 0; const char *ptr = orig_ptr;
int negative = 1; int is_negative = 0;
if (endptr != NULL)
*endptr = (char *) ptr;
/* Purge whitespace */ /* Purge whitespace */
for( ; *ptr && isspace(*ptr); ptr++); for( ; *ptr && isspace(*ptr); ptr++);
if (ptr[0] == '-') { if (ptr[0] == '-') {
negative = -1; is_negative = 1;
ptr++; ptr++;
} }
if (!*ptr) unsigned long long uval = strtoull(ptr, endptr, base);
return 0;
/* Determine the base */ /* If the whole string is unparseable, endptr should point to start. */
if (endptr && *endptr == ptr)
*endptr = (char *)orig_ptr;
if (base == 0) { if (uval > (unsigned long long)LLONG_MAX + !!is_negative)
if (ptr[0] == '0' && (ptr[1] == 'x' || ptr[1] == 'X')) uval = (unsigned long long)LLONG_MAX + !!is_negative;
base = 16;
else if (ptr[0] == '0') {
base = 8;
ptr++;
}
else
base = 10;
}
/* Base 16 allows the 0x on front - so skip over it */ if (is_negative)
return -uval;
else
return uval;
}
if (base == 16) { long int strtol(const char *ptr, char **endptr, int base)
if (ptr[0] == '0' && (ptr[1] == 'x' || ptr[1] == 'X')) {
ptr += 2; long long int val = strtoll(ptr, endptr, base);
} if (val > LONG_MAX)
return LONG_MAX;
/* If the first character isn't valid, then don't if (val < LONG_MIN)
* bother */ return LONG_MIN;
return val;
if (!*ptr || !_valid(*ptr, base))
return 0;
for( ; *ptr && _valid(*ptr, base); ptr++)
ret = (ret * base) + _offset(*ptr, base);
if (endptr != NULL)
*endptr = (char *) ptr;
return ret * negative;
} }
long atol(const char *nptr) long atol(const char *nptr)
@ -534,7 +519,7 @@ unsigned long long int strtoull(const char *ptr, char **endptr, int base)
unsigned long int strtoul(const char *ptr, char **endptr, int base) unsigned long int strtoul(const char *ptr, char **endptr, int base)
{ {
unsigned long long val = strtoull(ptr, endptr, base); unsigned long long val = strtoull(ptr, endptr, base);
if (val > UINT32_MAX) return UINT32_MAX; if (val > ULONG_MAX) return ULONG_MAX;
return val; return val;
} }