On 11/15/18 8:04 AM, David Hildenbrand wrote:
Let's provide a wrapper for strtod().

Signed-off-by: David Hildenbrand <da...@redhat.com>
---
  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 <ebl...@redhat.com>

--
Eric Blake, Principal Software Engineer
Red Hat, Inc.           +1-919-301-3266
Virtualization:  qemu.org | libvirt.org

Reply via email to