[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [PULL 06/21] test-cutils: Test more integer corner cases
From: |
Eric Blake |
Subject: |
Re: [PULL 06/21] test-cutils: Test more integer corner cases |
Date: |
Fri, 2 Jun 2023 07:34:45 -0500 |
User-agent: |
NeoMutt/20230517 |
On Thu, Jun 01, 2023 at 05:02:50PM -0500, Eric Blake wrote:
> We have quite a few undertested and underdocumented integer parsing
> corner cases. To ensure that any changes we make in the code are
> intentional rather than accidental semantic changes, it is time to add
> more unit tests of existing behavior.
>
> static void test_qemu_strtol_overflow(void)
> {
> - const char *str = "99999999999999999999999999999999999999999999";
> + const char *str;
> + const char *endptr;
> + long res;
> + int err;
> +
> + /* 1 more than LONG_MAX */
> + str = LONG_MAX == INT_MAX ? "2147483648" : "9223372036854775808";
> + endptr = "somewhere";
> + res = 999;
> + err = qemu_strtol(str, &endptr, 0, &res);
> + g_assert_cmpint(err, ==, -ERANGE);
> + g_assert_cmpint(res, ==, LONG_MAX);
> + g_assert_true(endptr == str + strlen(str));
> +
> + if (LONG_MAX == INT_MAX) {
> + str = "0xffffffff00000001"; /* ULLONG_MAX - UINT_MAX + 1 (not 1) */
> + endptr = "somewhere";
> + res = 999;
> + err = qemu_strtol(str, &endptr, 0, &res);
> + g_assert_cmpint(err, ==, -ERANGE);
> + g_assert_cmpint(res, ==, LONG_MIN);
> + g_assert_true(endptr == str + strlen(str));
> + }
Copy-paste failure, but only visible on platforms like mingw where
long is 32-bits; I had copied this...
>
> static void test_qemu_strtol_underflow(void)
> {
> - const char *str = "-99999999999999999999999999999999999999999999";
> - char f = 'X';
> - const char *endptr = &f;
> - long res = 999;
> + const char *str;
> + const char *endptr;
> + long res;
> int err;
>
> + /* 1 less than LONG_MIN */
> + str = LONG_MIN == INT_MIN ? "-2147483649" : "-9223372036854775809";
> + endptr = "somewhere";
> + res = 999;
> err = qemu_strtol(str, &endptr, 0, &res);
> + g_assert_cmpint(err, ==, -ERANGE);
> + g_assert_cmpint(res, ==, LONG_MIN);
> + g_assert_true(endptr == str + strlen(str));
>
> + if (LONG_MAX == INT_MAX) {
> + str = "-18446744073709551615"; /* -UINT64_MAX (not 1) */
> + endptr = "somewhere";
> + res = 999;
> + err = qemu_strtol(str, &endptr, 0, &res);
> + g_assert_cmpint(err, ==, -ERANGE);
> + g_assert_cmpint(res, ==, LONG_MIN);
> + g_assert_true(endptr == str + strlen(str));
> + }
...from here, but failed to s/MIN/MAX/ when dealing with the changed
sign. Yay for CI catching it.
--
Eric Blake, Principal Software Engineer
Red Hat, Inc. +1-919-301-3266
Virtualization: qemu.org | libvirt.org
- Re: [PULL 09/21] cutils: Adjust signature of parse_uint[_full], (continued)
- [PULL 18/21] cutils: Set value in all integral qemu_strto* error paths, Eric Blake, 2023/06/01
- [PULL 16/21] test-cutils: Add more coverage to qemu_strtosz, Eric Blake, 2023/06/01
- [PULL 21/21] cutils: Improve qemu_strtosz handling of fractions, Eric Blake, 2023/06/01
- [PULL 04/21] test-cutils: Use g_assert_cmpuint where appropriate, Eric Blake, 2023/06/01
- [PULL 15/21] numa: Check for qemu_strtosz_MiB error, Eric Blake, 2023/06/01
- [PULL 20/21] cutils: Improve qemu_strtod* error paths, Eric Blake, 2023/06/01
- [PULL 17/21] cutils: Set value in all qemu_strtosz* error paths, Eric Blake, 2023/06/01
- [PULL 06/21] test-cutils: Test more integer corner cases, Eric Blake, 2023/06/01
- Re: [PULL 06/21] test-cutils: Test more integer corner cases,
Eric Blake <=
- [PULL 14/21] cutils: Allow NULL str in qemu_strtosz, Eric Blake, 2023/06/01
- [PULL 19/21] cutils: Use parse_uint in qemu_strtosz for negative rejection, Eric Blake, 2023/06/01
- [PULL 13/21] test-cutils: Refactor qemu_strtosz tests for less boilerplate, Eric Blake, 2023/06/01
- [PULL 12/21] test-cutils: Prepare for upcoming semantic change in qemu_strtosz, Eric Blake, 2023/06/01
- [PULL 03/21] test-cutils: Avoid g_assert in unit tests, Eric Blake, 2023/06/01
- Re: [PULL 00/21] NBD and miscellaneous patches for 2023-06-01, Richard Henderson, 2023/06/01
- Conclusion of yet another expensive UI folly (was: [PULL 00/21] NBD and miscellaneous patches for 2023-06-01), Markus Armbruster, 2023/06/02