mirror of
https://github.com/yuzu-emu/unicorn.git
synced 2025-06-19 11:17:57 +00:00
util/cutils: Clean up control flow around qemu_strtol() a bit
Reorder check_strtox_error() to make it obvious that we always store through a non-null @endptr. Transform if (some error) { error case ... err = value for error case; } else { normal case ... err = value for normal case; } return err; to if (some error) { error case ... return value for error case; } normal case ... return value for normal case; Backports commit 4baef2679e029c76707be1e2ed54bf3dd21693fe from qemu
This commit is contained in:
parent
9236950e61
commit
fb962d2e74
|
@ -214,15 +214,20 @@ int64_t qemu_strtosz(const char *nptr, char **end)
|
||||||
static int check_strtox_error(const char *nptr, char *ep,
|
static int check_strtox_error(const char *nptr, char *ep,
|
||||||
const char **endptr, int libc_errno)
|
const char **endptr, int libc_errno)
|
||||||
{
|
{
|
||||||
if (libc_errno == 0 && ep == nptr) {
|
|
||||||
libc_errno = EINVAL;
|
|
||||||
}
|
|
||||||
if (!endptr && *ep) {
|
|
||||||
return -EINVAL;
|
|
||||||
}
|
|
||||||
if (endptr) {
|
if (endptr) {
|
||||||
*endptr = ep;
|
*endptr = ep;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* Turn "no conversion" into an error */
|
||||||
|
if (libc_errno == 0 && ep == nptr) {
|
||||||
|
return -EINVAL;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Fail when we're expected to consume the string, but didn't */
|
||||||
|
if (!endptr && *ep) {
|
||||||
|
return -EINVAL;
|
||||||
|
}
|
||||||
|
|
||||||
return -libc_errno;
|
return -libc_errno;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -254,18 +259,17 @@ int qemu_strtol(const char *nptr, const char **endptr, int base,
|
||||||
long *result)
|
long *result)
|
||||||
{
|
{
|
||||||
char *ep;
|
char *ep;
|
||||||
int err = 0;
|
|
||||||
if (!nptr) {
|
if (!nptr) {
|
||||||
if (endptr) {
|
if (endptr) {
|
||||||
*endptr = nptr;
|
*endptr = nptr;
|
||||||
}
|
}
|
||||||
err = -EINVAL;
|
return -EINVAL;
|
||||||
} else {
|
}
|
||||||
|
|
||||||
errno = 0;
|
errno = 0;
|
||||||
*result = strtol(nptr, &ep, base);
|
*result = strtol(nptr, &ep, base);
|
||||||
err = check_strtox_error(nptr, ep, endptr, errno);
|
return check_strtox_error(nptr, ep, endptr, errno);
|
||||||
}
|
|
||||||
return err;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -297,22 +301,21 @@ int qemu_strtoul(const char *nptr, const char **endptr, int base,
|
||||||
unsigned long *result)
|
unsigned long *result)
|
||||||
{
|
{
|
||||||
char *ep;
|
char *ep;
|
||||||
int err = 0;
|
|
||||||
if (!nptr) {
|
if (!nptr) {
|
||||||
if (endptr) {
|
if (endptr) {
|
||||||
*endptr = nptr;
|
*endptr = nptr;
|
||||||
}
|
}
|
||||||
err = -EINVAL;
|
return -EINVAL;
|
||||||
} else {
|
}
|
||||||
|
|
||||||
errno = 0;
|
errno = 0;
|
||||||
*result = strtoul(nptr, &ep, base);
|
*result = strtoul(nptr, &ep, base);
|
||||||
/* Windows returns 1 for negative out-of-range values. */
|
/* Windows returns 1 for negative out-of-range values. */
|
||||||
if (errno == ERANGE) {
|
if (errno == ERANGE) {
|
||||||
*result = -1;
|
*result = -1;
|
||||||
}
|
}
|
||||||
err = check_strtox_error(nptr, ep, endptr, errno);
|
return check_strtox_error(nptr, ep, endptr, errno);
|
||||||
}
|
|
||||||
return err;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -325,19 +328,18 @@ int qemu_strtoi64(const char *nptr, const char **endptr, int base,
|
||||||
int64_t *result)
|
int64_t *result)
|
||||||
{
|
{
|
||||||
char *ep;
|
char *ep;
|
||||||
int err = 0;
|
|
||||||
if (!nptr) {
|
if (!nptr) {
|
||||||
if (endptr) {
|
if (endptr) {
|
||||||
*endptr = nptr;
|
*endptr = nptr;
|
||||||
}
|
}
|
||||||
err = -EINVAL;
|
return -EINVAL;
|
||||||
} else {
|
}
|
||||||
|
|
||||||
errno = 0;
|
errno = 0;
|
||||||
/* FIXME This assumes int64_t is long long */
|
/* FIXME This assumes int64_t is long long */
|
||||||
*result = strtoll(nptr, &ep, base);
|
*result = strtoll(nptr, &ep, base);
|
||||||
err = check_strtox_error(nptr, ep, endptr, errno);
|
return check_strtox_error(nptr, ep, endptr, errno);
|
||||||
}
|
|
||||||
return err;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -349,13 +351,14 @@ int qemu_strtou64(const char *nptr, const char **endptr, int base,
|
||||||
uint64_t *result)
|
uint64_t *result)
|
||||||
{
|
{
|
||||||
char *ep;
|
char *ep;
|
||||||
int err = 0;
|
|
||||||
if (!nptr) {
|
if (!nptr) {
|
||||||
if (endptr) {
|
if (endptr) {
|
||||||
*endptr = nptr;
|
*endptr = nptr;
|
||||||
}
|
}
|
||||||
err = -EINVAL;
|
return -EINVAL;
|
||||||
} else {
|
}
|
||||||
|
|
||||||
errno = 0;
|
errno = 0;
|
||||||
/* FIXME This assumes uint64_t is unsigned long long */
|
/* FIXME This assumes uint64_t is unsigned long long */
|
||||||
*result = strtoull(nptr, &ep, base);
|
*result = strtoull(nptr, &ep, base);
|
||||||
|
@ -363,8 +366,6 @@ int qemu_strtou64(const char *nptr, const char **endptr, int base,
|
||||||
if (errno == ERANGE) {
|
if (errno == ERANGE) {
|
||||||
*result = -1;
|
*result = -1;
|
||||||
}
|
}
|
||||||
err = check_strtox_error(nptr, ep, endptr, errno);
|
return check_strtox_error(nptr, ep, endptr, errno);
|
||||||
}
|
|
||||||
return err;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue