]> Savannah Git Hosting - gnulib.git/commitdiff
c-xvasprintf: Guarantee a non-NULL result.
authorBruno Haible <bruno@clisp.org>
Sat, 22 Jun 2024 10:22:12 +0000 (12:22 +0200)
committerBruno Haible <bruno@clisp.org>
Sat, 22 Jun 2024 10:22:12 +0000 (12:22 +0200)
* lib/c-xvasprintf.h: Clarify the programmer's responsibilities.
(c_xasprintf, c_xvasprintf): Declare as returning non-NULL.
* lib/c-xvasprintf.c: Include <stdlib.h>, <string.h>.
(c_xvasprintf): Call c_vazsprintf instead of c_vasprintf. When some
other error occurs, emit an error message and abort.
* modules/c-xvasprintf (Files): Add m4/strerrorname_np.m4.
(Depends-on): Add extensions, c-vazsprintf. Remove c-vasprintf.
(configure.ac): Invoke gl_OPTIONAL_STRERRORNAME_NP.
* NEWS: Mention the change.

ChangeLog
NEWS
lib/c-xvasprintf.c
lib/c-xvasprintf.h
modules/c-xvasprintf

index f3aec5b9280dce26e6711db1bbd4fb626de01e97..f81ae03de765d3a0be6f110181e0b18c65d202a3 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,16 @@
+2024-06-22  Bruno Haible  <bruno@clisp.org>
+
+       c-xvasprintf: Guarantee a non-NULL result.
+       * lib/c-xvasprintf.h: Clarify the programmer's responsibilities.
+       (c_xasprintf, c_xvasprintf): Declare as returning non-NULL.
+       * lib/c-xvasprintf.c: Include <stdlib.h>, <string.h>.
+       (c_xvasprintf): Call c_vazsprintf instead of c_vasprintf. When some
+       other error occurs, emit an error message and abort.
+       * modules/c-xvasprintf (Files): Add m4/strerrorname_np.m4.
+       (Depends-on): Add extensions, c-vazsprintf. Remove c-vasprintf.
+       (configure.ac): Invoke gl_OPTIONAL_STRERRORNAME_NP.
+       * NEWS: Mention the change.
+
 2024-06-22  Bruno Haible  <bruno@clisp.org>
 
        c-vasprintf: Make return convention consistent with other modules.
diff --git a/NEWS b/NEWS
index 36ffbb64f8cd10d60549457e041d9bca113fefad..4c5d9fc5de3ce34da358ba825941184a9951c484 100644 (file)
--- a/NEWS
+++ b/NEWS
@@ -75,7 +75,7 @@ User visible incompatible changes
 Date        Modules         Changes
 
 2024-06-22  xvasprintf      It is now the programmer's responsibility to pass
-                            a valid format string without %ls, %lc directives
+            c-xvasprintf    a valid format string without %ls, %lc directives
                             and that all widths are >= -INT_MAX and <= INT_MAX.
 
 2024-05-16  putenv          This module is renamed to 'putenv-gnu'.
index 0fda230eb907d2431ecb6ca76d4ac12146c7eff1..b058bde4c542d0465a3e35572ddd358b0b09120f 100644 (file)
@@ -21,6 +21,8 @@
 
 #include <errno.h>
 #include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
 
 #include "c-vasprintf.h"
 #include "xalloc.h"
@@ -30,11 +32,38 @@ c_xvasprintf (const char *format, va_list args)
 {
   char *result;
 
-  if (c_vasprintf (&result, format, args) < 0)
+  if (c_vazsprintf (&result, format, args) < 0)
     {
       if (errno == ENOMEM)
         xalloc_die ();
-      return NULL;
+      else
+        {
+          /* The programmer ought to have ensured that none of the other errors
+             can occur.  */
+          int err = errno;
+          char errbuf[20];
+          const char *errname;
+#if HAVE_WORKING_STRERRORNAME_NP
+          errname = strerrorname_np (err);
+          if (errname == NULL)
+#else
+          if (err == EINVAL)
+            errname = "EINVAL";
+          else if (err == EILSEQ)
+            errname = "EILSEQ";
+          else if (err == EOVERFLOW)
+            errname = "EOVERFLOW";
+          else
+#endif
+            {
+              sprintf (errbuf, "%d", err);
+              errname = errbuf;
+            }
+          fprintf (stderr, "c_vasprintf failed! format=\"%s\", errno=%s\n",
+                   format, errname);
+          fflush (stderr);
+          abort ();
+        }
     }
 
   return result;
index 4cb4578584dc9d9129af4f4405f8111a533f16ae..897ecfdbf23a5eed01f5f5a2b49a16b003b394cd 100644 (file)
@@ -17,7 +17,8 @@
 #ifndef _C_XVASPRINTF_H
 #define _C_XVASPRINTF_H
 
-/* This file uses _GL_ATTRIBUTE_FORMAT, _GL_ATTRIBUTE_MALLOC.  */
+/* This file uses _GL_ATTRIBUTE_FORMAT, _GL_ATTRIBUTE_MALLOC,
+  _GL_ATTRIBUTE_RETURNS_NONNULL.  */
 #if !_GL_CONFIG_H_INCLUDED
  #error "Please include config.h first."
 #endif
 extern "C" {
 #endif
 
-/* Write formatted output to a string dynamically allocated with malloc(),
-   and return it.  Upon [ENOMEM] memory allocation error, call xalloc_die.
-   On some other error
-     - [EOVERFLOW] resulting string length is > INT_MAX,
+/* Prints formatted output to a string dynamically allocated with malloc(),
+   and returns it.  Upon [ENOMEM] memory allocation error, it calls xalloc_die.
+
+   It is the responsibility of the programmer to ensure that
+     - the format string is valid,
+     - the format string does not use %ls or %lc directives, and
+     - all widths in the format string and passed as arguments are >= -INT_MAX
+       and <= INT_MAX,
+   so that other errors
      - [EINVAL] invalid format string,
      - [EILSEQ] error during conversion between wide and multibyte characters,
-   return NULL.
+     - [EOVERFLOW] some specified width is > INT_MAX,
+   cannot occur.
 
    Formatting takes place in the C locale, that is, the decimal point
    used in floating-point formatting directives is always '.'. */
 extern char *c_xasprintf (const char *format, ...)
   _GL_ATTRIBUTE_FORMAT ((_GL_ATTRIBUTE_SPEC_PRINTF_STANDARD, 1, 2))
-  _GL_ATTRIBUTE_MALLOC _GL_ATTRIBUTE_DEALLOC_FREE;
+  _GL_ATTRIBUTE_MALLOC _GL_ATTRIBUTE_DEALLOC_FREE
+  _GL_ATTRIBUTE_RETURNS_NONNULL;
 extern char *c_xvasprintf (const char *format, va_list args)
   _GL_ATTRIBUTE_FORMAT ((_GL_ATTRIBUTE_SPEC_PRINTF_STANDARD, 1, 0))
-  _GL_ATTRIBUTE_MALLOC _GL_ATTRIBUTE_DEALLOC_FREE;
+  _GL_ATTRIBUTE_MALLOC _GL_ATTRIBUTE_DEALLOC_FREE
+  _GL_ATTRIBUTE_RETURNS_NONNULL;
 
 #ifdef __cplusplus
 }
index b952128fb6dd281efc77a97f6cc9feaf1efd7d02..ccbc93d794b6b5dbbea1a6da264fa5affa871a57 100644 (file)
@@ -5,13 +5,16 @@ Files:
 lib/c-xvasprintf.h
 lib/c-xasprintf.c
 lib/c-xvasprintf.c
+m4/strerrorname_np.m4
 
 Depends-on:
+extensions
 stdio
-c-vasprintf
+c-vazsprintf
 xalloc-die
 
 configure.ac:
+gl_OPTIONAL_STRERRORNAME_NP
 
 Makefile.am:
 lib_SOURCES += c-xasprintf.c c-xvasprintf.c
@@ -23,4 +26,4 @@ License:
 GPL
 
 Maintainer:
-Ben Pfaff
+all