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

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

index 60ed44502154fa52e6ff1860057c3ebe7b6e196a..28833003fb93454748585f16c48ee45929ba718b 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,13 @@
 2024-06-22  Bruno Haible  <bruno@clisp.org>
 
+       vsnprintf: Use vzsnprintf.
+       * lib/stdio.in.h (vsnprintf): Move specification to here.
+       * lib/vsnprintf.c: Don't include <stdlib.h>, <string.h>, vasnprintf.h.
+       Include <stdint.h>.
+       (vsnprintf): Implement based on vzsnprintf.
+       * modules/vsnprintf (Depends-on): Add stdint, vzsnprintf. Remove
+       vasnprintf.
+
        vzsnprintf: New module.
        * lib/stdio.in.h (vzsnprintf): New declaration, based on
        lib/vsnprintf.c.
index d8d27a56c53abc0b499604fdcbbc7bec55ffcffc..8d6cd21fd57292cf2bacdb97988831bdacdd0770 100644 (file)
@@ -1789,6 +1789,11 @@ _GL_CXXALIAS_SYS (vzsnprintf, ptrdiff_t,
 #endif
 
 #if @GNULIB_VSNPRINTF@
+/* Prints formatted output to string STR.  Similar to vsprintf, 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_VSNPRINTF@
 #  if !(defined __cplusplus && defined GNULIB_NAMESPACE)
 #   define vsnprintf rpl_vsnprintf
index e6676a1ffa358d9735f2b61e8d5546b4418cbfbc..1954dcea26e41f27532a83292dd47a6e6955fff2 100644 (file)
@@ -1,6 +1,5 @@
 /* Formatted output to strings.
    Copyright (C) 2004, 2006-2024 Free Software Foundation, Inc.
-   Written by Simon Josefsson and Yoann Vandoorselaere <yoann@prelude-ids.org>.
 
    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 vsprintf, 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
 vsnprintf (char *str, size_t size, const char *format, va_list args)
 {
-  char *output;
-  size_t len;
-  size_t lenbuf = size;
-
-  output = vasnprintf (str, &lenbuf, format, args);
-  len = lenbuf;
-
-  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);
-    }
+  ptrdiff_t ret = vzsnprintf (str, size, format, args);
 
-  if (len > INT_MAX)
+#if PTRDIFF_MAX > INT_MAX
+  if (ret > INT_MAX)
     {
       errno = EOVERFLOW;
       return -1;
     }
+#endif
 
-  return len;
+  return ret;
 }
index 053b10c32a57749b0e5c310a3aba04d15d95f051..de2a86e28589407585b38b14a4fdaf2c26d73807 100644 (file)
@@ -9,8 +9,9 @@ m4/printf.m4
 
 Depends-on:
 stdio
-vasnprintf      [test $ac_cv_func_vsnprintf = no || test $REPLACE_VSNPRINTF = 1]
 errno           [test $ac_cv_func_vsnprintf = no || test $REPLACE_VSNPRINTF = 1]
+stdint          [test $ac_cv_func_vsnprintf = no || test $REPLACE_VSNPRINTF = 1]
+vzsnprintf      [test $ac_cv_func_vsnprintf = no || test $REPLACE_VSNPRINTF = 1]
 
 configure.ac:
 gl_FUNC_VSNPRINTF
@@ -25,4 +26,4 @@ License:
 LGPLv2+
 
 Maintainer:
-Yoann Vandoorselaere
+all