提交 4baef267 编写于 作者: M Markus Armbruster

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;
Signed-off-by: NMarkus Armbruster <armbru@redhat.com>
Reviewed-by: NEric Blake <eblake@redhat.com>
Message-Id: <1487708048-2131-9-git-send-email-armbru@redhat.com>
上级 717adf96
...@@ -265,15 +265,20 @@ int64_t qemu_strtosz(const char *nptr, char **end) ...@@ -265,15 +265,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 (endptr) {
*endptr = ep;
}
/* Turn "no conversion" into an error */
if (libc_errno == 0 && ep == nptr) { if (libc_errno == 0 && ep == nptr) {
libc_errno = EINVAL; return -EINVAL;
} }
/* Fail when we're expected to consume the string, but didn't */
if (!endptr && *ep) { if (!endptr && *ep) {
return -EINVAL; return -EINVAL;
} }
if (endptr) {
*endptr = ep;
}
return -libc_errno; return -libc_errno;
} }
...@@ -305,18 +310,17 @@ int qemu_strtol(const char *nptr, const char **endptr, int base, ...@@ -305,18 +310,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;
*result = strtol(nptr, &ep, base);
err = check_strtox_error(nptr, ep, endptr, errno);
} }
return err;
errno = 0;
*result = strtol(nptr, &ep, base);
return check_strtox_error(nptr, ep, endptr, errno);
} }
/** /**
...@@ -348,22 +352,21 @@ int qemu_strtoul(const char *nptr, const char **endptr, int base, ...@@ -348,22 +352,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;
*result = strtoul(nptr, &ep, base); errno = 0;
/* Windows returns 1 for negative out-of-range values. */ *result = strtoul(nptr, &ep, base);
if (errno == ERANGE) { /* Windows returns 1 for negative out-of-range values. */
*result = -1; if (errno == ERANGE) {
} *result = -1;
err = check_strtox_error(nptr, ep, endptr, errno);
} }
return err; return check_strtox_error(nptr, ep, endptr, errno);
} }
/** /**
...@@ -376,19 +379,18 @@ int qemu_strtoi64(const char *nptr, const char **endptr, int base, ...@@ -376,19 +379,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;
/* FIXME This assumes int64_t is long long */
*result = strtoll(nptr, &ep, base);
err = check_strtox_error(nptr, ep, endptr, errno);
} }
return err;
errno = 0;
/* FIXME This assumes int64_t is long long */
*result = strtoll(nptr, &ep, base);
return check_strtox_error(nptr, ep, endptr, errno);
} }
/** /**
...@@ -400,23 +402,22 @@ int qemu_strtou64(const char *nptr, const char **endptr, int base, ...@@ -400,23 +402,22 @@ 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;
/* FIXME This assumes uint64_t is unsigned long long */ errno = 0;
*result = strtoull(nptr, &ep, base); /* FIXME This assumes uint64_t is unsigned long long */
/* Windows returns 1 for negative out-of-range values. */ *result = strtoull(nptr, &ep, base);
if (errno == ERANGE) { /* Windows returns 1 for negative out-of-range values. */
*result = -1; if (errno == ERANGE) {
} *result = -1;
err = check_strtox_error(nptr, ep, endptr, errno);
} }
return err; return check_strtox_error(nptr, ep, endptr, errno);
} }
/** /**
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册