[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[PATCH v3 03/19] test-cutils: Test integral qemu_strto* value on failure
From: |
Eric Blake |
Subject: |
[PATCH v3 03/19] test-cutils: Test integral qemu_strto* value on failures |
Date: |
Mon, 22 May 2023 14:04:25 -0500 |
We are inconsistent on the contents of *value after a strto* parse
failure. I found the following behaviors:
- parse_uint() and parse_uint_full(), which document that *value is
slammed to 0 on all EINVAL failures and 0 or UINT_MAX on ERANGE
failures, and has unit tests for that (note that parse_uint requires
non-NULL endptr, and does not fail with EINVAL for trailing junk)
- qemu_strtosz(), which leaves *value untouched on all failures (both
EINVAL and ERANGE), and has unit tests but not documentation for
that
- qemu_strtoi() and other integral friends, which document *value on
ERANGE failures but is unspecified on EINVAL (other than implicitly
by comparison to libc strto*); there, *value is untouched for NULL
string, slammed to 0 on no conversion, and left at the prefix value
on NULL endptr; unit tests do not consistently check the value
- qemu_strtod(), which documents *value on ERANGE failures but is
unspecified on EINVAL; there, *value is untouched for NULL string,
slammed to 0.0 for no conversion, and left at the prefix value on
NULL endptr; there are no unit tests (other than indirectly through
qemu_strtosz)
- qemu_strtod_finite(), which documents *value on ERANGE failures but
is unspecified on EINVAL; there, *value is left at the prefix for
'inf' or 'nan' and untouched in all other cases; there are no unit
tests (other than indirectly through qemu_strtosz)
Upcoming patches will change behaviors for consistency, but it's best
to first have more unit test coverage to see the impact of those
changes.
Signed-off-by: Eric Blake <eblake@redhat.com>
Reviewed-by: Hanna Czenczek <hreitz@redhat.com>
---
tests/unit/test-cutils.c | 58 +++++++++++++++++++++++++++++++++++-----
1 file changed, 51 insertions(+), 7 deletions(-)
diff --git a/tests/unit/test-cutils.c b/tests/unit/test-cutils.c
index 38bd3990207..1eeaf21ae22 100644
--- a/tests/unit/test-cutils.c
+++ b/tests/unit/test-cutils.c
@@ -248,6 +248,7 @@ static void test_qemu_strtoi_null(void)
err = qemu_strtoi(NULL, &endptr, 0, &res);
g_assert_cmpint(err, ==, -EINVAL);
+ g_assert_cmpint(res, ==, 999);
g_assert_null(endptr);
}
@@ -262,6 +263,7 @@ static void test_qemu_strtoi_empty(void)
err = qemu_strtoi(str, &endptr, 0, &res);
g_assert_cmpint(err, ==, -EINVAL);
+ g_assert_cmpint(res, ==, 0);
g_assert_true(endptr == str);
}
@@ -276,6 +278,7 @@ static void test_qemu_strtoi_whitespace(void)
err = qemu_strtoi(str, &endptr, 0, &res);
g_assert_cmpint(err, ==, -EINVAL);
+ g_assert_cmpint(res, ==, 0);
g_assert_true(endptr == str);
}
@@ -290,6 +293,7 @@ static void test_qemu_strtoi_invalid(void)
err = qemu_strtoi(str, &endptr, 0, &res);
g_assert_cmpint(err, ==, -EINVAL);
+ g_assert_cmpint(res, ==, 0);
g_assert_true(endptr == str);
}
@@ -473,6 +477,7 @@ static void test_qemu_strtoi_full_null(void)
err = qemu_strtoi(NULL, &endptr, 0, &res);
g_assert_cmpint(err, ==, -EINVAL);
+ g_assert_cmpint(res, ==, 999);
g_assert_null(endptr);
}
@@ -485,6 +490,7 @@ static void test_qemu_strtoi_full_empty(void)
err = qemu_strtoi(str, NULL, 0, &res);
g_assert_cmpint(err, ==, -EINVAL);
+ g_assert_cmpint(res, ==, 0);
}
static void test_qemu_strtoi_full_negative(void)
@@ -502,18 +508,19 @@ static void test_qemu_strtoi_full_negative(void)
static void test_qemu_strtoi_full_trailing(void)
{
const char *str = "123xxx";
- int res;
+ int res = 999;
int err;
err = qemu_strtoi(str, NULL, 0, &res);
g_assert_cmpint(err, ==, -EINVAL);
+ g_assert_cmpint(res, ==, 123);
}
static void test_qemu_strtoi_full_max(void)
{
char *str = g_strdup_printf("%d", INT_MAX);
- int res;
+ int res = 999;
int err;
err = qemu_strtoi(str, NULL, 0, &res);
@@ -548,6 +555,7 @@ static void test_qemu_strtoui_null(void)
err = qemu_strtoui(NULL, &endptr, 0, &res);
g_assert_cmpint(err, ==, -EINVAL);
+ g_assert_cmpuint(res, ==, 999);
g_assert_null(endptr);
}
@@ -562,6 +570,7 @@ static void test_qemu_strtoui_empty(void)
err = qemu_strtoui(str, &endptr, 0, &res);
g_assert_cmpint(err, ==, -EINVAL);
+ g_assert_cmpuint(res, ==, 0);
g_assert_true(endptr == str);
}
@@ -576,6 +585,7 @@ static void test_qemu_strtoui_whitespace(void)
err = qemu_strtoui(str, &endptr, 0, &res);
g_assert_cmpint(err, ==, -EINVAL);
+ g_assert_cmpuint(res, ==, 0);
g_assert_true(endptr == str);
}
@@ -590,6 +600,7 @@ static void test_qemu_strtoui_invalid(void)
err = qemu_strtoui(str, &endptr, 0, &res);
g_assert_cmpint(err, ==, -EINVAL);
+ g_assert_cmpuint(res, ==, 0);
g_assert_true(endptr == str);
}
@@ -771,6 +782,7 @@ static void test_qemu_strtoui_full_null(void)
err = qemu_strtoui(NULL, NULL, 0, &res);
g_assert_cmpint(err, ==, -EINVAL);
+ g_assert_cmpuint(res, ==, 999);
}
static void test_qemu_strtoui_full_empty(void)
@@ -782,7 +794,9 @@ static void test_qemu_strtoui_full_empty(void)
err = qemu_strtoui(str, NULL, 0, &res);
g_assert_cmpint(err, ==, -EINVAL);
+ g_assert_cmpuint(res, ==, 0);
}
+
static void test_qemu_strtoui_full_negative(void)
{
const char *str = " \t -321";
@@ -797,12 +811,13 @@ static void test_qemu_strtoui_full_negative(void)
static void test_qemu_strtoui_full_trailing(void)
{
const char *str = "123xxx";
- unsigned int res;
+ unsigned int res = 999;
int err;
err = qemu_strtoui(str, NULL, 0, &res);
g_assert_cmpint(err, ==, -EINVAL);
+ g_assert_cmpuint(res, ==, 123);
}
static void test_qemu_strtoui_full_max(void)
@@ -843,6 +858,7 @@ static void test_qemu_strtol_null(void)
err = qemu_strtol(NULL, &endptr, 0, &res);
g_assert_cmpint(err, ==, -EINVAL);
+ g_assert_cmpint(res, ==, 999);
g_assert_null(endptr);
}
@@ -857,6 +873,7 @@ static void test_qemu_strtol_empty(void)
err = qemu_strtol(str, &endptr, 0, &res);
g_assert_cmpint(err, ==, -EINVAL);
+ g_assert_cmpint(res, ==, 0);
g_assert_true(endptr == str);
}
@@ -871,6 +888,7 @@ static void test_qemu_strtol_whitespace(void)
err = qemu_strtol(str, &endptr, 0, &res);
g_assert_cmpint(err, ==, -EINVAL);
+ g_assert_cmpint(res, ==, 0);
g_assert_true(endptr == str);
}
@@ -885,6 +903,7 @@ static void test_qemu_strtol_invalid(void)
err = qemu_strtol(str, &endptr, 0, &res);
g_assert_cmpint(err, ==, -EINVAL);
+ g_assert_cmpint(res, ==, 0);
g_assert_true(endptr == str);
}
@@ -1066,6 +1085,7 @@ static void test_qemu_strtol_full_null(void)
err = qemu_strtol(NULL, &endptr, 0, &res);
g_assert_cmpint(err, ==, -EINVAL);
+ g_assert_cmpint(res, ==, 999);
g_assert_null(endptr);
}
@@ -1078,6 +1098,7 @@ static void test_qemu_strtol_full_empty(void)
err = qemu_strtol(str, NULL, 0, &res);
g_assert_cmpint(err, ==, -EINVAL);
+ g_assert_cmpint(res, ==, 0);
}
static void test_qemu_strtol_full_negative(void)
@@ -1095,18 +1116,19 @@ static void test_qemu_strtol_full_negative(void)
static void test_qemu_strtol_full_trailing(void)
{
const char *str = "123xxx";
- long res;
+ long res = 999;
int err;
err = qemu_strtol(str, NULL, 0, &res);
g_assert_cmpint(err, ==, -EINVAL);
+ g_assert_cmpint(res, ==, 123);
}
static void test_qemu_strtol_full_max(void)
{
char *str = g_strdup_printf("%ld", LONG_MAX);
- long res;
+ long res = 999;
int err;
err = qemu_strtol(str, NULL, 0, &res);
@@ -1141,6 +1163,7 @@ static void test_qemu_strtoul_null(void)
err = qemu_strtoul(NULL, &endptr, 0, &res);
g_assert_cmpint(err, ==, -EINVAL);
+ g_assert_cmpuint(res, ==, 999);
g_assert_null(endptr);
}
@@ -1155,6 +1178,7 @@ static void test_qemu_strtoul_empty(void)
err = qemu_strtoul(str, &endptr, 0, &res);
g_assert_cmpint(err, ==, -EINVAL);
+ g_assert_cmpuint(res, ==, 0);
g_assert_true(endptr == str);
}
@@ -1169,6 +1193,7 @@ static void test_qemu_strtoul_whitespace(void)
err = qemu_strtoul(str, &endptr, 0, &res);
g_assert_cmpint(err, ==, -EINVAL);
+ g_assert_cmpuint(res, ==, 0);
g_assert_true(endptr == str);
}
@@ -1183,6 +1208,7 @@ static void test_qemu_strtoul_invalid(void)
err = qemu_strtoul(str, &endptr, 0, &res);
g_assert_cmpint(err, ==, -EINVAL);
+ g_assert_cmpuint(res, ==, 0);
g_assert_true(endptr == str);
}
@@ -1362,6 +1388,7 @@ static void test_qemu_strtoul_full_null(void)
err = qemu_strtoul(NULL, NULL, 0, &res);
g_assert_cmpint(err, ==, -EINVAL);
+ g_assert_cmpuint(res, ==, 999);
}
static void test_qemu_strtoul_full_empty(void)
@@ -1373,7 +1400,9 @@ static void test_qemu_strtoul_full_empty(void)
err = qemu_strtoul(str, NULL, 0, &res);
g_assert_cmpint(err, ==, -EINVAL);
+ g_assert_cmpuint(res, ==, 0);
}
+
static void test_qemu_strtoul_full_negative(void)
{
const char *str = " \t -321";
@@ -1388,12 +1417,13 @@ static void test_qemu_strtoul_full_negative(void)
static void test_qemu_strtoul_full_trailing(void)
{
const char *str = "123xxx";
- unsigned long res;
+ unsigned long res = 999;
int err;
err = qemu_strtoul(str, NULL, 0, &res);
g_assert_cmpint(err, ==, -EINVAL);
+ g_assert_cmpuint(res, ==, 123);
}
static void test_qemu_strtoul_full_max(void)
@@ -1434,6 +1464,7 @@ static void test_qemu_strtoi64_null(void)
err = qemu_strtoi64(NULL, &endptr, 0, &res);
g_assert_cmpint(err, ==, -EINVAL);
+ g_assert_cmpint(res, ==, 999);
g_assert_null(endptr);
}
@@ -1448,6 +1479,7 @@ static void test_qemu_strtoi64_empty(void)
err = qemu_strtoi64(str, &endptr, 0, &res);
g_assert_cmpint(err, ==, -EINVAL);
+ g_assert_cmpint(res, ==, 0);
g_assert_true(endptr == str);
}
@@ -1462,6 +1494,7 @@ static void test_qemu_strtoi64_whitespace(void)
err = qemu_strtoi64(str, &endptr, 0, &res);
g_assert_cmpint(err, ==, -EINVAL);
+ g_assert_cmpint(res, ==, 0);
g_assert_true(endptr == str);
}
@@ -1476,6 +1509,7 @@ static void test_qemu_strtoi64_invalid(void)
err = qemu_strtoi64(str, &endptr, 0, &res);
g_assert_cmpint(err, ==, -EINVAL);
+ g_assert_cmpint(res, ==, 0);
g_assert_true(endptr == str);
}
@@ -1655,6 +1689,7 @@ static void test_qemu_strtoi64_full_null(void)
err = qemu_strtoi64(NULL, NULL, 0, &res);
g_assert_cmpint(err, ==, -EINVAL);
+ g_assert_cmpint(res, ==, 999);
}
static void test_qemu_strtoi64_full_empty(void)
@@ -1666,6 +1701,7 @@ static void test_qemu_strtoi64_full_empty(void)
err = qemu_strtoi64(str, NULL, 0, &res);
g_assert_cmpint(err, ==, -EINVAL);
+ g_assert_cmpint(res, ==, 0);
}
static void test_qemu_strtoi64_full_negative(void)
@@ -1689,13 +1725,14 @@ static void test_qemu_strtoi64_full_trailing(void)
err = qemu_strtoi64(str, NULL, 0, &res);
g_assert_cmpint(err, ==, -EINVAL);
+ g_assert_cmpint(res, ==, 123);
}
static void test_qemu_strtoi64_full_max(void)
{
char *str = g_strdup_printf("%lld", LLONG_MAX);
- int64_t res;
+ int64_t res = 999;
int err;
err = qemu_strtoi64(str, NULL, 0, &res);
@@ -1730,6 +1767,7 @@ static void test_qemu_strtou64_null(void)
err = qemu_strtou64(NULL, &endptr, 0, &res);
g_assert_cmpint(err, ==, -EINVAL);
+ g_assert_cmpuint(res, ==, 999);
g_assert_null(endptr);
}
@@ -1744,6 +1782,7 @@ static void test_qemu_strtou64_empty(void)
err = qemu_strtou64(str, &endptr, 0, &res);
g_assert_cmpint(err, ==, -EINVAL);
+ g_assert_cmpuint(res, ==, 0);
g_assert_true(endptr == str);
}
@@ -1758,6 +1797,7 @@ static void test_qemu_strtou64_whitespace(void)
err = qemu_strtou64(str, &endptr, 0, &res);
g_assert_cmpint(err, ==, -EINVAL);
+ g_assert_cmpuint(res, ==, 0);
g_assert_true(endptr == str);
}
@@ -1772,6 +1812,7 @@ static void test_qemu_strtou64_invalid(void)
err = qemu_strtou64(str, &endptr, 0, &res);
g_assert_cmpint(err, ==, -EINVAL);
+ g_assert_cmpuint(res, ==, 0);
g_assert_true(endptr == str);
}
@@ -1951,6 +1992,7 @@ static void test_qemu_strtou64_full_null(void)
err = qemu_strtou64(NULL, NULL, 0, &res);
g_assert_cmpint(err, ==, -EINVAL);
+ g_assert_cmpuint(res, ==, 999);
}
static void test_qemu_strtou64_full_empty(void)
@@ -1962,6 +2004,7 @@ static void test_qemu_strtou64_full_empty(void)
err = qemu_strtou64(str, NULL, 0, &res);
g_assert_cmpint(err, ==, -EINVAL);
+ g_assert_cmpuint(res, ==, 0);
}
static void test_qemu_strtou64_full_negative(void)
@@ -1985,6 +2028,7 @@ static void test_qemu_strtou64_full_trailing(void)
err = qemu_strtou64(str, NULL, 0, &res);
g_assert_cmpint(err, ==, -EINVAL);
+ g_assert_cmpuint(res, ==, 18446744073709551614ULL);
}
static void test_qemu_strtou64_full_max(void)
--
2.40.1
- [PATCH v3 00/19] Fix qemu_strtosz() read-out-of-bounds, Eric Blake, 2023/05/22
- [PATCH v3 04/19] test-cutils: Test more integer corner cases, Eric Blake, 2023/05/22
- [PATCH v3 06/19] cutils: Document differences between parse_uint and qemu_strtou64, Eric Blake, 2023/05/22
- [PATCH v3 01/19] test-cutils: Avoid g_assert in unit tests, Eric Blake, 2023/05/22
- [PATCH v3 02/19] test-cutils: Use g_assert_cmpuint where appropriate, Eric Blake, 2023/05/22
- [PATCH v3 07/19] cutils: Adjust signature of parse_uint[_full], Eric Blake, 2023/05/22
- [PATCH v3 14/19] test-cutils: Add more coverage to qemu_strtosz, Eric Blake, 2023/05/22
- [PATCH v3 03/19] test-cutils: Test integral qemu_strto* value on failures,
Eric Blake <=
- [PATCH v3 05/19] cutils: Fix wraparound parsing in qemu_strtoui, Eric Blake, 2023/05/22
- [PATCH v3 08/19] cutils: Allow NULL endptr in parse_uint(), Eric Blake, 2023/05/22
- [PATCH v3 09/19] test-cutils: Add coverage of qemu_strtod, Eric Blake, 2023/05/22
- [PATCH v3 16/19] cutils: Set value in all integral qemu_strto* error paths, Eric Blake, 2023/05/22
- [PATCH v3 10/19] test-cutils: Prepare for upcoming semantic change in qemu_strtosz, Eric Blake, 2023/05/22
- [PATCH v3 12/19] cutils: Allow NULL str in qemu_strtosz, Eric Blake, 2023/05/22
- [PATCH v3 11/19] test-cutils: Refactor qemu_strtosz tests for less boilerplate, Eric Blake, 2023/05/22
- [PATCH v3 13/19] numa: Check for qemu_strtosz_MiB error, Eric Blake, 2023/05/22
- [PATCH v3 17/19] cutils: Use parse_uint in qemu_strtosz for negative rejection, Eric Blake, 2023/05/22