[Libguestfs] [PATCH nbdkit v2] server: utils: Make nbdkit_parse_size to reject negative values
Eric Blake
eblake at redhat.com
Fri Feb 8 14:43:14 UTC 2019
On 2/8/19 12:10 AM, Mykola Ivanets wrote:
> From: Nikolay Ivanets <stenavin at gmail.com>
Grammar in the subject: s/ to//
>
> nbdkit_parse_size() uses strtoumax() function to parse input strings
> which states:
>
> 1. Some more tests were added to cover described behaviour.
>
> 2. Input strings where grouped into a set which lead to
> valid/invalid/negative/overflow result.
>
> 3. Some strings with a leading '+' sign were added.
> ---
> errno = 0;
> - size = strtoumax (str, &end, 10);
> - if (errno || str == end) {
> + size = strtoimax (str, &end, 10);
> + if (str == end) {
> nbdkit_error ("could not parse size string (%s)", str);
> return -1;
> }
> + if (size < 0) {
> + nbdkit_error ("size cannot be negative (%s)", str);
> + return -1;
> + }
> + if (errno) {
> + nbdkit_error ("size (%s) exceeds maximum value", str);
> + return -1;
> + }
On underflow, strtoimax returns INTMAX_MIN and sets ERANGE; which favors
a message about negative values over a message about overflow. Swapping
the errno message first would also work, but I'm fine with your approach.
Thanks; pushed!
--
Eric Blake, Principal Software Engineer
Red Hat, Inc. +1-919-301-3226
Virtualization: qemu.org | libvirt.org
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 488 bytes
Desc: OpenPGP digital signature
URL: <http://listman.redhat.com/archives/libguestfs/attachments/20190208/881de6e9/attachment.sig>
More information about the Libguestfs
mailing list