Skip to content

Commit

Permalink
cutils: work around platform differences in strto{l,ul,ll,ull}
Browse files Browse the repository at this point in the history
Linux returns 0 if no conversion was made, while OS X and presumably
the BSDs return EINVAL.  The OS X convention rejects more invalid
inputs, so convert to it and adjust the test case.

Windows returns 1 from strtoul and strtoull (instead of -1) for
negative out-of-range input; fix it up.

Reported-by: Peter Maydell <[email protected]>
Signed-off-by: Paolo Bonzini <[email protected]>
  • Loading branch information
bonzini committed Sep 10, 2015
1 parent 9fd1a94 commit 47d4be1
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 46 deletions.
57 changes: 16 additions & 41 deletions tests/test-cutils.c
Original file line number Diff line number Diff line change
Expand Up @@ -264,9 +264,7 @@ static void test_qemu_strtol_empty(void)

err = qemu_strtol(str, &endptr, 0, &res);

g_assert_cmpint(err, ==, 0);
g_assert_cmpint(res, ==, 0);
g_assert(endptr == str);
g_assert_cmpint(err, ==, -EINVAL);
}

static void test_qemu_strtol_whitespace(void)
Expand All @@ -279,9 +277,7 @@ static void test_qemu_strtol_whitespace(void)

err = qemu_strtol(str, &endptr, 0, &res);

g_assert_cmpint(err, ==, 0);
g_assert_cmpint(res, ==, 0);
g_assert(endptr == str);
g_assert_cmpint(err, ==, -EINVAL);
}

static void test_qemu_strtol_invalid(void)
Expand All @@ -294,9 +290,7 @@ static void test_qemu_strtol_invalid(void)

err = qemu_strtol(str, &endptr, 0, &res);

g_assert_cmpint(err, ==, 0);
g_assert_cmpint(res, ==, 0);
g_assert(endptr == str);
g_assert_cmpint(err, ==, -EINVAL);
}

static void test_qemu_strtol_trailing(void)
Expand Down Expand Up @@ -478,8 +472,7 @@ static void test_qemu_strtol_full_empty(void)

err = qemu_strtol(str, NULL, 0, &res);

g_assert_cmpint(err, ==, 0);
g_assert_cmpint(res, ==, 0);
g_assert_cmpint(err, ==, -EINVAL);
}

static void test_qemu_strtol_full_negative(void)
Expand Down Expand Up @@ -555,9 +548,7 @@ static void test_qemu_strtoul_empty(void)

err = qemu_strtoul(str, &endptr, 0, &res);

g_assert_cmpint(err, ==, 0);
g_assert_cmpint(res, ==, 0);
g_assert(endptr == str);
g_assert_cmpint(err, ==, -EINVAL);
}

static void test_qemu_strtoul_whitespace(void)
Expand All @@ -570,9 +561,7 @@ static void test_qemu_strtoul_whitespace(void)

err = qemu_strtoul(str, &endptr, 0, &res);

g_assert_cmpint(err, ==, 0);
g_assert_cmpint(res, ==, 0);
g_assert(endptr == str);
g_assert_cmpint(err, ==, -EINVAL);
}

static void test_qemu_strtoul_invalid(void)
Expand All @@ -585,8 +574,7 @@ static void test_qemu_strtoul_invalid(void)

err = qemu_strtoul(str, &endptr, 0, &res);

g_assert_cmpint(err, ==, 0);
g_assert(endptr == str);
g_assert_cmpint(err, ==, -EINVAL);
}

static void test_qemu_strtoul_trailing(void)
Expand Down Expand Up @@ -765,8 +753,7 @@ static void test_qemu_strtoul_full_empty(void)

err = qemu_strtoul(str, NULL, 0, &res);

g_assert_cmpint(err, ==, 0);
g_assert_cmpint(res, ==, 0);
g_assert_cmpint(err, ==, -EINVAL);
}
static void test_qemu_strtoul_full_negative(void)
{
Expand Down Expand Up @@ -840,9 +827,7 @@ static void test_qemu_strtoll_empty(void)

err = qemu_strtoll(str, &endptr, 0, &res);

g_assert_cmpint(err, ==, 0);
g_assert_cmpint(res, ==, 0);
g_assert(endptr == str);
g_assert_cmpint(err, ==, -EINVAL);
}

static void test_qemu_strtoll_whitespace(void)
Expand All @@ -855,9 +840,7 @@ static void test_qemu_strtoll_whitespace(void)

err = qemu_strtoll(str, &endptr, 0, &res);

g_assert_cmpint(err, ==, 0);
g_assert_cmpint(res, ==, 0);
g_assert(endptr == str);
g_assert_cmpint(err, ==, -EINVAL);
}

static void test_qemu_strtoll_invalid(void)
Expand All @@ -870,8 +853,7 @@ static void test_qemu_strtoll_invalid(void)

err = qemu_strtoll(str, &endptr, 0, &res);

g_assert_cmpint(err, ==, 0);
g_assert(endptr == str);
g_assert_cmpint(err, ==, -EINVAL);
}

static void test_qemu_strtoll_trailing(void)
Expand Down Expand Up @@ -1050,8 +1032,7 @@ static void test_qemu_strtoll_full_empty(void)

err = qemu_strtoll(str, NULL, 0, &res);

g_assert_cmpint(err, ==, 0);
g_assert_cmpint(res, ==, 0);
g_assert_cmpint(err, ==, -EINVAL);
}

static void test_qemu_strtoll_full_negative(void)
Expand Down Expand Up @@ -1128,9 +1109,7 @@ static void test_qemu_strtoull_empty(void)

err = qemu_strtoull(str, &endptr, 0, &res);

g_assert_cmpint(err, ==, 0);
g_assert_cmpint(res, ==, 0);
g_assert(endptr == str);
g_assert_cmpint(err, ==, -EINVAL);
}

static void test_qemu_strtoull_whitespace(void)
Expand All @@ -1143,9 +1122,7 @@ static void test_qemu_strtoull_whitespace(void)

err = qemu_strtoull(str, &endptr, 0, &res);

g_assert_cmpint(err, ==, 0);
g_assert_cmpint(res, ==, 0);
g_assert(endptr == str);
g_assert_cmpint(err, ==, -EINVAL);
}

static void test_qemu_strtoull_invalid(void)
Expand All @@ -1158,8 +1135,7 @@ static void test_qemu_strtoull_invalid(void)

err = qemu_strtoull(str, &endptr, 0, &res);

g_assert_cmpint(err, ==, 0);
g_assert(endptr == str);
g_assert_cmpint(err, ==, -EINVAL);
}

static void test_qemu_strtoull_trailing(void)
Expand Down Expand Up @@ -1338,8 +1314,7 @@ static void test_qemu_strtoull_full_empty(void)

err = qemu_strtoull(str, NULL, 0, &res);

g_assert_cmpint(err, ==, 0);
g_assert_cmpint(res, ==, 0);
g_assert_cmpint(err, ==, -EINVAL);
}

static void test_qemu_strtoull_full_negative(void)
Expand Down
24 changes: 19 additions & 5 deletions util/cutils.c
Original file line number Diff line number Diff line change
Expand Up @@ -361,9 +361,15 @@ int64_t strtosz(const char *nptr, char **end)
/**
* Helper function for qemu_strto*l() functions.
*/
static int check_strtox_error(const char **next, char *endptr,
static int check_strtox_error(const char *p, char *endptr, const char **next,
int err)
{
/* If no conversion was performed, prefer BSD behavior over glibc
* behavior.
*/
if (err == 0 && endptr == p) {
err = EINVAL;
}
if (!next && *endptr) {
return -EINVAL;
}
Expand Down Expand Up @@ -412,7 +418,7 @@ int qemu_strtol(const char *nptr, const char **endptr, int base,
} else {
errno = 0;
*result = strtol(nptr, &p, base);
err = check_strtox_error(endptr, p, errno);
err = check_strtox_error(nptr, p, endptr, errno);
}
return err;
}
Expand Down Expand Up @@ -443,7 +449,11 @@ int qemu_strtoul(const char *nptr, const char **endptr, int base,
} else {
errno = 0;
*result = strtoul(nptr, &p, base);
err = check_strtox_error(endptr, p, errno);
/* Windows returns 1 for negative out-of-range values. */
if (errno == ERANGE) {
*result = -1;
}
err = check_strtox_error(nptr, p, endptr, errno);
}
return err;
}
Expand All @@ -466,7 +476,7 @@ int qemu_strtoll(const char *nptr, const char **endptr, int base,
} else {
errno = 0;
*result = strtoll(nptr, &p, base);
err = check_strtox_error(endptr, p, errno);
err = check_strtox_error(nptr, p, endptr, errno);
}
return err;
}
Expand All @@ -489,7 +499,11 @@ int qemu_strtoull(const char *nptr, const char **endptr, int base,
} else {
errno = 0;
*result = strtoull(nptr, &p, base);
err = check_strtox_error(endptr, p, errno);
/* Windows returns 1 for negative out-of-range values. */
if (errno == ERANGE) {
*result = -1;
}
err = check_strtox_error(nptr, p, endptr, errno);
}
return err;
}
Expand Down

0 comments on commit 47d4be1

Please sign in to comment.