cutils: Set value in all integral qemu_strto* error paths
commit3c5f2467984c23aae5a64548dcb15efae18e207e
authorEric Blake <eblake@redhat.com>
Mon, 22 May 2023 19:04:38 +0000 (22 14:04 -0500)
committerEric Blake <eblake@redhat.com>
Fri, 2 Jun 2023 17:29:27 +0000 (2 12:29 -0500)
treed94f3a3f72cf6e614c3a834365dc6c6ce79a96af
parent896fbd90aa3ffd4ad57b35722128fe65a9abc530
cutils: Set value in all integral qemu_strto* error paths

Our goal in writing qemu_strtoi() and friends is to have an interface
harder to abuse than libc's strtol().  Leaving the return value
uninitialized on some but not all error paths does not lend itself
well to this goal; and our documentation wasn't helpful on what to
expect.

Note that the previous patch changed all qemu_strtosz() EINVAL error
paths to slam value to 0 rather than stay uninitialized, even when the
EINVAL eror occurs because of trailing junk.  But for the remaining
integral qemu_strto*, it's easier to return the parsed value than to
force things back to zero, in part because of how check_strtox_error
works; in part because people expect that from libc strto* (while
there is no libc strtosz to compare to), and in part because doing so
creates less churn in the testsuite.

Here, the list of affected callers is much longer ('git grep
"qemu_strto[ui]" "*.c" "**/*.c" | grep -v tests/ |wc -l' outputs 107,
although a few of those are the implementation in in cutils.c), so
touching as little as possible is the wisest course of action.

Signed-off-by: Eric Blake <eblake@redhat.com>
Reviewed-by: Hanna Czenczek <hreitz@redhat.com>
Message-Id: <20230522190441.64278-17-eblake@redhat.com>
tests/unit/test-cutils.c
util/cutils.c