From b0afb8fe0ff1d87165af9df61501197a06240dda Mon Sep 17 00:00:00 2001 From: Lorenz Kästle <12514511+RincewindsHat@users.noreply.github.com> Date: Sun, 28 Dec 2025 12:13:40 +0100 Subject: Sync with Gnulib stable-202507 code (a8ac9f9ce5) --- gl/snprintf.c | 41 ++++++++--------------------------------- 1 file changed, 8 insertions(+), 33 deletions(-) (limited to 'gl/snprintf.c') diff --git a/gl/snprintf.c b/gl/snprintf.c index c1b93562..edeee083 100644 --- a/gl/snprintf.c +++ b/gl/snprintf.c @@ -1,6 +1,5 @@ /* Formatted output to strings. - Copyright (C) 2004, 2006-2024 Free Software Foundation, Inc. - Written by Simon Josefsson and Paul Eggert. + Copyright (C) 2004, 2006-2025 Free Software Foundation, Inc. This file is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as @@ -23,49 +22,25 @@ #include #include #include -#include -#include +#include -#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 = vsnzprintf (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; } -- cgit v1.2.3-74-g34f1