mirror of
https://github.com/xemu-project/xemu.git
synced 2025-01-18 01:43:04 +00:00
cutils: Assert in-range base for string-to-integer conversions
POSIX states that the value of endptr is unspecified if strtol() fails with EINVAL due to an invalid base argument. Since none of the callers to check_strtox_error() initialized endptr, we could end up propagating uninitialized data back to a caller on error. However, passing an out-of-range base is already a sign of poor programming, so let's just assert that base is in range, at which point check_strtox_error() can be tightened to assert that it is receiving an initialized ep that points somewhere within the caller's original string, regardless of whether strto*() succeeded or failed with ERANGE. Reported-by: Vladimir Sementsov-Ogievskiy <vsementsov@virtuozzo.com> Signed-off-by: Eric Blake <eblake@redhat.com> Reviewed-by: Markus Armbruster <armbru@redhat.com> Reviewed-by: Philippe Mathieu-Daudé <philmd@redhat.com> Message-Id: <20181206151856.77503-1-eblake@redhat.com> Signed-off-by: Laurent Vivier <laurent@vivier.eu>
This commit is contained in:
parent
9e722ebc06
commit
53a90b9724
@ -280,6 +280,7 @@ int qemu_strtosz_metric(const char *nptr, char **end, uint64_t *result)
|
||||
static int check_strtox_error(const char *nptr, char *ep,
|
||||
const char **endptr, int libc_errno)
|
||||
{
|
||||
assert(ep >= nptr);
|
||||
if (endptr) {
|
||||
*endptr = ep;
|
||||
}
|
||||
@ -327,6 +328,7 @@ int qemu_strtoi(const char *nptr, const char **endptr, int base,
|
||||
char *ep;
|
||||
long long lresult;
|
||||
|
||||
assert((unsigned) base <= 36 && base != 1);
|
||||
if (!nptr) {
|
||||
if (endptr) {
|
||||
*endptr = nptr;
|
||||
@ -379,6 +381,7 @@ int qemu_strtoui(const char *nptr, const char **endptr, int base,
|
||||
char *ep;
|
||||
long long lresult;
|
||||
|
||||
assert((unsigned) base <= 36 && base != 1);
|
||||
if (!nptr) {
|
||||
if (endptr) {
|
||||
*endptr = nptr;
|
||||
@ -435,6 +438,7 @@ int qemu_strtol(const char *nptr, const char **endptr, int base,
|
||||
{
|
||||
char *ep;
|
||||
|
||||
assert((unsigned) base <= 36 && base != 1);
|
||||
if (!nptr) {
|
||||
if (endptr) {
|
||||
*endptr = nptr;
|
||||
@ -477,6 +481,7 @@ int qemu_strtoul(const char *nptr, const char **endptr, int base,
|
||||
{
|
||||
char *ep;
|
||||
|
||||
assert((unsigned) base <= 36 && base != 1);
|
||||
if (!nptr) {
|
||||
if (endptr) {
|
||||
*endptr = nptr;
|
||||
@ -504,6 +509,7 @@ int qemu_strtoi64(const char *nptr, const char **endptr, int base,
|
||||
{
|
||||
char *ep;
|
||||
|
||||
assert((unsigned) base <= 36 && base != 1);
|
||||
if (!nptr) {
|
||||
if (endptr) {
|
||||
*endptr = nptr;
|
||||
@ -527,6 +533,7 @@ int qemu_strtou64(const char *nptr, const char **endptr, int base,
|
||||
{
|
||||
char *ep;
|
||||
|
||||
assert((unsigned) base <= 36 && base != 1);
|
||||
if (!nptr) {
|
||||
if (endptr) {
|
||||
*endptr = nptr;
|
||||
@ -594,6 +601,7 @@ int parse_uint(const char *s, unsigned long long *value, char **endptr,
|
||||
char *endp = (char *)s;
|
||||
unsigned long long val = 0;
|
||||
|
||||
assert((unsigned) base <= 36 && base != 1);
|
||||
if (!s) {
|
||||
r = -EINVAL;
|
||||
goto out;
|
||||
|
Loading…
x
Reference in New Issue
Block a user