[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [Qemu-devel] [PATCH 1/8] cutils: unsigned int parsing functions
From: |
Eric Blake |
Subject: |
Re: [Qemu-devel] [PATCH 1/8] cutils: unsigned int parsing functions |
Date: |
Wed, 16 Jan 2013 10:10:42 -0700 |
User-agent: |
Mozilla/5.0 (X11; Linux x86_64; rv:17.0) Gecko/20130110 Thunderbird/17.0.2 |
On 01/16/2013 08:24 AM, Eduardo Habkost wrote:
> There are lots of duplicate parsing code using strto*() in QEMU, and
> most of that code is broken in one way or another. Even the visitors
> code have duplicate integer parsing code[1]. This introduces functions
> to help parsing unsigned int values: parse_uint() and parse_uint_full().
>
> Parsing functions for signed ints and floats will be submitted later.
>
> parse_uint_full() has all the checks made by opts_type_uint64() at
> opts-visitor.c:
>
> - Check for NULL (returns -EINVAL)
> - Check for negative numbers (returns -ERANGE)
> - Check for empty string (returns -EINVAL)
> - Check for overflow or other errno values set by strtoll() (returns
> -errno)
> - Check for end of string (reject invalid characters after number)
> (returns -EINVAL)
>
> parse_uint() does everything above except checking for the end of the
> string, so callers can continue parsing the remainder of string after
> the number.
>
> Unit tests included.
>
> [1] string-input-visitor.c:parse_int() could use the same parsing code
> used by opts-visitor.c:opts_type_int(), instead of duplicating that
> logic.
>
> Signed-off-by: Eduardo Habkost <address@hidden>
> ---
> Cc: Laszlo Ersek <address@hidden>
> Cc: Eric Blake <address@hidden>
> ---
> +++ b/tests/test-cutils.c
> + *
> + * Permission is hereby granted, free of charge, to any person obtaining a
> copy
Interesting that you chose a BSD license instead of GPL, but doesn't
affect my review.
Your test case lacks test of octal or hexadecimal input strings; is that
worth adding?
> +++ b/util/cutils.c
> @@ -270,6 +270,82 @@ int64_t strtosz(const char *nptr, char **end)
> return strtosz_suffix(nptr, end, STRTOSZ_DEFSUFFIX_MB);
> }
>
> +/* Try to parse an unsigned integer
> + *
> + * Error checks done by the function:
> + * - NULL pointer will return -EINVAL.
> + * - Empty strings will return -EINVAL.
> + * - Overflow errors or other errno values set by strtoull() will
> + * return -errno (-ERANGE in case of overflow).
> + * - Differently from strtoull(), values starting with a minus sign are
> + * rejected (returning -ERANGE).
Interesting that you chose to reject negative numbers, even though
strtoull() is required to accept them. But you documented it and tested
for it, so I can live with it.
> + errno = 0;
> + val = strtoull(s, &endp, 0);
> + if (errno) {
> + r = -errno;
Why two spaces?
Reviewed-by: Eric Blake <address@hidden>
--
Eric Blake eblake redhat com +1-919-301-3266
Libvirt virtualization library http://libvirt.org
signature.asc
Description: OpenPGP digital signature
- Re: [Qemu-devel] [PATCH 6/8] vl.c: Use parse_uint_full() for NUMA nodeid, (continued)
[Qemu-devel] [PATCH 2/8] vl.c: Fix off-by-one bug when handling "-numa node" argument, Eduardo Habkost, 2013/01/16
[Qemu-devel] [PATCH 3/8] vl.c: Abort on unknown -numa option type, Eduardo Habkost, 2013/01/16
[Qemu-devel] [PATCH 8/8] vl.c: validate -numa "cpus" parameter properly, Eduardo Habkost, 2013/01/16
[Qemu-devel] [PATCH 1/8] cutils: unsigned int parsing functions, Eduardo Habkost, 2013/01/16
- Re: [Qemu-devel] [PATCH 1/8] cutils: unsigned int parsing functions,
Eric Blake <=
[Qemu-devel] [PATCH 5/8] vl.c: numa_add(): Validate nodeid before using it, Eduardo Habkost, 2013/01/16
[Qemu-devel] [PATCH 4/8] vl.c: Check for NUMA node limit inside numa_add(), Eduardo Habkost, 2013/01/16
Message not available