bug-gnulib
[Top][All Lists]
Advanced

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

c-strtod: handling of out-of-memory


From: Bruno Haible
Subject: c-strtod: handling of out-of-memory
Date: Thu, 22 Jan 2009 12:27:47 +0100
User-agent: KMail/1.9.9

Now, when there is an out-of-memory while c_strtod is called, the handling is
inconsistent:
  - If it occurs inside newlocale() or inside strtod_l() or inside strtod(),
    the function returns 0 with errno set to ENOMEM.
  - If it occurs inside strdup() of the locale string, the function calls
    xalloc_die().

Michael Gold said in
<http://lists.gnu.org/archive/html/bug-gnulib/2009-01/msg00094.html>:
  "c_strtod will also call abort() on these platforms if there's no memory
   to copy the old locale string, which makes it inappropriate for use in
   libraries."

So, to make the function more widely usable and the error handling more
consistent, I propose to remove the use of xmalloc here, and leave the
handling of ENOMEM to the caller.

Here is a proposed patch to that effect:


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

        Make c-strtod, c-strtold usable in libraries.
        * lib/c-strtod.c: Include string.h instead of xalloc.h.
        (C_STRTOD): Call strdup instead of xstrdup.
        * modules/c-strtod (Depends-on): Add strdup-posix, remove xalloc.
        * modules/c-strtold (Depends-on): Likewise.
        * doc/c-strtod.texi: Remove the sentence mentioning xalloc_die.
        * NEWS: Mention the change.
        Reported by Michael Gold <address@hidden>.

--- lib/c-strtod.c.orig 2009-01-22 12:22:36.000000000 +0100
+++ lib/c-strtod.c      2009-01-22 12:11:31.000000000 +0100
@@ -24,8 +24,7 @@
 #include <errno.h>
 #include <locale.h>
 #include <stdlib.h>
-
-#include "xalloc.h"
+#include <string.h>
 
 #if LONG
 # define C_STRTOD c_strtold
@@ -82,7 +81,9 @@
 
   if (saved_locale)
     {
-      saved_locale = xstrdup (saved_locale);
+      saved_locale = strdup (saved_locale);
+      if (saved_locale == NULL)
+       return 0; /* errno is set here */
       setlocale (LC_NUMERIC, "C");
     }
 
--- modules/c-strtod.orig       2009-01-22 12:22:36.000000000 +0100
+++ modules/c-strtod    2009-01-22 12:11:57.000000000 +0100
@@ -8,7 +8,7 @@
 
 Depends-on:
 extensions
-xalloc
+strdup-posix
 
 configure.ac:
 gl_C_STRTOD
--- modules/c-strtold.orig      2009-01-22 12:22:36.000000000 +0100
+++ modules/c-strtold   2009-01-22 12:11:57.000000000 +0100
@@ -9,7 +9,7 @@
 
 Depends-on:
 extensions
-xalloc
+strdup-posix
 
 configure.ac:
 gl_C_STRTOLD
--- doc/c-strtod.texi.orig      2009-01-22 12:22:36.000000000 +0100
+++ doc/c-strtod.texi   2009-01-22 12:22:25.000000000 +0100
@@ -23,6 +23,5 @@
 when the current locale's notion of decimal point is a comma @samp{,},
 and no characters outside the basic character set are accepted.
 
-This function aborts via @code{xalloc_die} if it cannot allocate memory.
-On platforms without @code{strtod_l}, it is not safe for use in
+On platforms without @code{strtod_l}, this function is not safe for use in
 multi-threaded applications since it calls @code{setlocale}.
--- NEWS.orig   2009-01-22 12:22:36.000000000 +0100
+++ NEWS        2009-01-22 12:21:58.000000000 +0100
@@ -6,6 +6,15 @@
 
 Date        Modules         Changes
 
+2009-01-22  c-strtod        This function no longer calls xalloc_die(). If
+            c-strtold       you want to exit the program in case of out-of-
+                            memory, the calling function needs to arrange
+                            for it, like this:
+                                errno = 0;
+                                val = c_strtod (...);
+                                if (val == 0 && errno == ENOMEM)
+                                  xalloc_die ();
+
 2009-01-17  relocatable-prog  In the Makefile.am or Makefile.in, you now also
                             need to set RELOCATABLE_STRIP = :.
 




reply via email to

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