]> Savannah Git Hosting - gnulib.git/commitdiff
snprintf: Use vzsnprintf.
authorBruno Haible <bruno@clisp.org>
Sat, 22 Jun 2024 10:15:38 +0000 (12:15 +0200)
committerBruno Haible <bruno@clisp.org>
Sat, 22 Jun 2024 10:15:48 +0000 (12:15 +0200)
* lib/stdio.in.h (snprintf): Move specification to here.
* lib/snprintf.c: Don't include <stdlib.h>, <string.h>, vasnprintf.h.
Include <stdint.h>.
(snprintf): Implement based on vzsnprintf.
* modules/snprintf (Depends-on): Add stdint, vzsnprintf. Remove
vasnprintf.

ChangeLog
lib/snprintf.c
lib/stdio.in.h
modules/snprintf

index 458561b9fed09d483b7f2d9bd881f5f923ddfb5d..68677a44ad81b41703d95af2a527591426876d43 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,13 @@
 2024-06-22  Bruno Haible  <bruno@clisp.org>
 
+       snprintf: Use vzsnprintf.
+       * lib/stdio.in.h (snprintf): Move specification to here.
+       * lib/snprintf.c: Don't include <stdlib.h>, <string.h>, vasnprintf.h.
+       Include <stdint.h>.
+       (snprintf): Implement based on vzsnprintf.
+       * modules/snprintf (Depends-on): Add stdint, vzsnprintf. Remove
+       vasnprintf.
+
        zsnprintf: New module.
        * lib/stdio.in.h (zsnprintf): New declaration, based on
        lib/snprintf.c.
index c1b93562ec50b0efa564dfab05420b8b43f386ba..80f69225ac97922bdcf0866471a73b82d7b77dfc 100644 (file)
@@ -1,6 +1,5 @@
 /* Formatted output to strings.
    Copyright (C) 2004, 2006-2024 Free Software Foundation, Inc.
-   Written by Simon Josefsson and Paul Eggert.
 
    This file is free software: you can redistribute it and/or modify
    it under the terms of the GNU Lesser General Public License as
 #include <errno.h>
 #include <limits.h>
 #include <stdarg.h>
-#include <stdlib.h>
-#include <string.h>
+#include <stdint.h>
 
-#include "vasnprintf.h"
-
-/* Print formatted output to string STR.  Similar to sprintf, but
-   additional length SIZE limit how much is written into STR.  Returns
-   string length of formatted string (which may be larger than SIZE).
-   STR may be NULL, in which case nothing will be written.  On error,
-   return a negative value.  */
 int
 snprintf (char *str, size_t size, const char *format, ...)
 {
-  char *output;
-  size_t len;
-  size_t lenbuf = size;
   va_list args;
+  ptrdiff_t ret;
 
   va_start (args, format);
-  output = vasnprintf (str, &lenbuf, format, args);
-  len = lenbuf;
+  ret = vzsnprintf (str, size, format, args);
   va_end (args);
 
-  if (!output)
-    return -1;
-
-  if (output != str)
-    {
-      if (size)
-        {
-          size_t pruned_len = (len < size ? len : size - 1);
-          memcpy (str, output, pruned_len);
-          str[pruned_len] = '\0';
-        }
-
-      free (output);
-    }
-
-  if (INT_MAX < len)
+#if PTRDIFF_MAX > INT_MAX
+  if (ret > INT_MAX)
     {
       errno = EOVERFLOW;
       return -1;
     }
+#endif
 
-  return len;
+  return ret;
 }
index 4a942a92ed74a43ed2618bbfb219ebac65baa1af..d0479c10ae3758d650c7d6536fe9bd2d84ff834f 100644 (file)
@@ -1453,6 +1453,11 @@ _GL_CXXALIAS_SYS (zsnprintf, ptrdiff_t,
 #endif
 
 #if @GNULIB_SNPRINTF@
+/* Prints formatted output to string STR.  Similar to sprintf, but the
+   additional parameter SIZE limits how much is written into STR.
+   STR may be NULL, in which case nothing will be written.
+   Returns the string length of the formatted string (which may be larger
+   than SIZE).  Upon failure, returns a negative value.  */
 # if @REPLACE_SNPRINTF@
 #  if !(defined __cplusplus && defined GNULIB_NAMESPACE)
 #   define snprintf rpl_snprintf
index 102d629706ec09c92bb1a6442fc9c0bc34fd366a..cd4eb6b2c4910ae63f983451f3c12814a6df231f 100644 (file)
@@ -8,8 +8,9 @@ m4/printf.m4
 
 Depends-on:
 stdio
-vasnprintf      [test $ac_cv_func_snprintf = no || test $REPLACE_SNPRINTF = 1]
 errno           [test $ac_cv_func_snprintf = no || test $REPLACE_SNPRINTF = 1]
+stdint          [test $ac_cv_func_snprintf = no || test $REPLACE_SNPRINTF = 1]
+vzsnprintf      [test $ac_cv_func_snprintf = no || test $REPLACE_SNPRINTF = 1]
 
 configure.ac:
 gl_FUNC_SNPRINTF
@@ -25,4 +26,4 @@ License:
 LGPLv2+
 
 Maintainer:
-Simon Josefsson, Paul Eggert
+all