bug-gnulib
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

getloadavg: check for c_strtod errors


From: Bruno Haible
Subject: getloadavg: check for c_strtod errors
Date: Wed, 21 Jan 2009 12:22:08 +0100
User-agent: KMail/1.9.9

Hi Jim,

getloadavg uses c_strtod, but does not completely chcek against all error
conditions (such as EINVAL or ENOMEM). Here's a proposed patch to that effect.

The strtod documentation in POSIX says:
  "Since 0 is returned on error and is also a valid return on success, an
   application wishing to check for error situations should set errno to 0,
   then call strtod(), strtof(), or strtold(), then check errno."
This applies also to c_strtod, since it call strtod().

The test for 'ptr == endptr' is still necessary, though, since at least
on glibc systems,  strtod ("foo", &endptr)  returns 0 with errno = 0.

OK to commit?


2009-01-21  Bruno Haible  <address@hidden>

        * lib/getloadavg.c (getloadavg): Check c_strtod result against error
        conditions other than overflow.

--- lib/getloadavg.c.orig       2009-01-21 12:17:55.000000000 +0100
+++ lib/getloadavg.c    2009-01-21 11:56:50.000000000 +0100
@@ -1,8 +1,8 @@
 /* Get the system load averages.
 
    Copyright (C) 1985, 1986, 1987, 1988, 1989, 1991, 1992, 1993, 1994,
-   1995, 1997, 1999, 2000, 2003, 2004, 2005, 2006, 2007, 2008 Free Software
-   Foundation, Inc.
+   1995, 1997, 1999, 2000, 2003, 2004, 2005, 2006, 2007, 2008, 2009 Free
+   Software Foundation, Inc.
 
    NOTE: The canonical source of this file is maintained with gnulib.
    Bugs can be reported to address@hidden
@@ -621,8 +621,11 @@
   for (elem = 0; elem < nelem; elem++)
     {
       char *endptr;
-      double d = c_strtod (ptr, &endptr);
-      if (ptr == endptr)
+      double d;
+
+      errno = 0;
+      d = c_strtod (ptr, &endptr);
+      if (ptr == endptr || (d == 0 && errno != 0))
        {
          if (elem == 0)
            return -1;




reply via email to

[Prev in Thread] Current Thread [Next in Thread]