[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [Qemu-devel] [PATCH v1 1/9] cutils: add qemu_strtod() and qemu_strto
From: |
Eric Blake |
Subject: |
Re: [Qemu-devel] [PATCH v1 1/9] cutils: add qemu_strtod() and qemu_strtod_finite() |
Date: |
Thu, 15 Nov 2018 08:23:59 -0600 |
User-agent: |
Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Thunderbird/60.3.0 |
On 11/15/18 8:04 AM, David Hildenbrand wrote:
Let's provide a wrapper for strtod().
Signed-off-by: David Hildenbrand <address@hidden>
---
include/qemu/cutils.h | 2 ++
util/cutils.c | 38 ++++++++++++++++++++++++++++++++++++++
2 files changed, 40 insertions(+)
+
+/**
+ * Convert string @nptr to a finite double.
+ *
+ * Works like qemu_strtoul(), except it stores +/-HUGE_VAL on
+ * overflow/underflow. "NaN" or "inf" are rejcted with -EINVAL.
s/rejcted/rejected/
+ */
+int qemu_strtod_finite(const char *nptr, const char **endptr, double *result)
+{
+ int ret = qemu_strtod(nptr, endptr, result);
On overflow, result is set to HUGE_VAL (aka "inf") with ret set to
-ERANGE. (The C standard uses HUGE_VAL rather than directly requiring
infinity on overflow, in order to cater to museum platforms where the
largest representable double is still finite; but no one develops qemu
on a non-IEEE machine these days so we know that HUGE_VAL == INF).
+
+ if (!ret && !isfinite(*result)) {
+ return -EINVAL;
+ }
This check means that overflow ("1e9999") fails with -ERANGE, while
actual infinity ("inf") fails with -EINVAL, letting the user distinguish
between the two. Still, I wonder if assigning a non-finite value into
result on -ERANGE is the wisest course of action. We'll just have to
see in the next patches that use this.
With the typo fix,
Reviewed-by: Eric Blake <address@hidden>
--
Eric Blake, Principal Software Engineer
Red Hat, Inc. +1-919-301-3266
Virtualization: qemu.org | libvirt.org
[Qemu-devel] [PATCH v1 2/9] cutils: use qemu_strtod_finite() in do_strtosz(), David Hildenbrand, 2018/11/15
[Qemu-devel] [PATCH v1 3/9] qapi: use qemu_strtod_finite() in string-input-visitor, David Hildenbrand, 2018/11/15