From: Bruno Haible Date: Sat, 22 Jun 2024 10:21:51 +0000 (+0200) Subject: c-vasprintf: Make return convention consistent with other modules. X-Git-Url: https://gitweb.git.savannah.gnu.org/gitweb/?a=commitdiff_plain;h=82c0381e34d872590dfd3260c94e79d01da104a3;p=gnulib.git c-vasprintf: Make return convention consistent with other modules. * lib/c-vasprintf.h (c_asprintf, c_vasprintf): Add specification. * lib/c-asprintf.c: Replaced with code based on lib/asprintf.c. * lib/c-vasprintf.c: Replaced with code based on lib/vasprintf.c. * modules/c-vasprintf (Depends-on): Add stdint. (License): Change to LGPLv2+. --- diff --git a/ChangeLog b/ChangeLog index 7a2dd7375c..f3aec5b928 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,12 @@ 2024-06-22 Bruno Haible + c-vasprintf: Make return convention consistent with other modules. + * lib/c-vasprintf.h (c_asprintf, c_vasprintf): Add specification. + * lib/c-asprintf.c: Replaced with code based on lib/asprintf.c. + * lib/c-vasprintf.c: Replaced with code based on lib/vasprintf.c. + * modules/c-vasprintf (Depends-on): Add stdint. + (License): Change to LGPLv2+. + c-vazsprintf: New module. * lib/c-vasprintf.h: Change license to LGPLv2+. Include . diff --git a/lib/c-asprintf.c b/lib/c-asprintf.c index c6f7719531..f626c5207b 100644 --- a/lib/c-asprintf.c +++ b/lib/c-asprintf.c @@ -3,16 +3,16 @@ Inc. This file is free software: you can redistribute it and/or modify - it under the terms of the GNU General Public License as published - by the Free Software Foundation, either version 3 of the License, - or (at your option) any later version. + it under the terms of the GNU Lesser General Public License as + published by the Free Software Foundation; either version 2.1 of the + License, or (at your option) any later version. This file is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. + GNU Lesser General Public License for more details. - You should have received a copy of the GNU General Public License + You should have received a copy of the GNU Lesser General Public License along with this program. If not, see . */ #include diff --git a/lib/c-vasprintf.c b/lib/c-vasprintf.c index 5adaec93f2..81d45ce0ac 100644 --- a/lib/c-vasprintf.c +++ b/lib/c-vasprintf.c @@ -2,16 +2,16 @@ Copyright (C) 1999, 2002, 2006-2024 Free Software Foundation, Inc. This file is free software: you can redistribute it and/or modify - it under the terms of the GNU General Public License as published - by the Free Software Foundation, either version 3 of the License, - or (at your option) any later version. + it under the terms of the GNU Lesser General Public License as + published by the Free Software Foundation; either version 2.1 of the + License, or (at your option) any later version. This file is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. + GNU Lesser General Public License for more details. - You should have received a copy of the GNU General Public License + You should have received a copy of the GNU Lesser General Public License along with this program. If not, see . */ #include @@ -21,6 +21,7 @@ #include #include +#include #include #include "c-vasnprintf.h" @@ -33,12 +34,21 @@ c_vasprintf (char **resultp, const char *format, va_list args) if (result == NULL) return -1; +#if PTRDIFF_MAX > INT_MAX if (length > INT_MAX) { free (result); - errno = EOVERFLOW; + errno = (length > PTRDIFF_MAX ? ENOMEM : EOVERFLOW); return -1; } +#else + if (length > PTRDIFF_MAX) + { + free (result); + errno = ENOMEM; + return -1; + } +#endif *resultp = result; /* Return the number of resulting bytes, excluding the trailing NUL. */ diff --git a/lib/c-vasprintf.h b/lib/c-vasprintf.h index 7eae757e3a..bcd40bd5e0 100644 --- a/lib/c-vasprintf.h +++ b/lib/c-vasprintf.h @@ -52,9 +52,13 @@ ptrdiff_t c_azsprintf (char **resultp, const char *format, ...) ptrdiff_t c_vazsprintf (char **resultp, const char *format, va_list args) _GL_ATTRIBUTE_FORMAT ((_GL_ATTRIBUTE_SPEC_PRINTF_STANDARD, 2, 0)); -/* asprintf() and vasprintf(), but formatting takes place in the C locale, that - is, the decimal point used in floating-point formatting directives is always - '.'. */ +/* Prints formatted output to a string dynamically allocated with malloc(). + If the memory allocation succeeds, it stores the address of the string in + *RESULT and returns the number of resulting bytes, excluding the trailing + NUL. Upon memory allocation error, or some other error, it returns -1. + + Formatting takes place in the C locale, that is, the decimal point + used in floating-point formatting directives is always '.'. */ int c_asprintf (char **resultp, const char *format, ...) _GL_ATTRIBUTE_FORMAT ((_GL_ATTRIBUTE_SPEC_PRINTF_STANDARD, 2, 3)); int c_vasprintf (char **resultp, const char *format, va_list args) diff --git a/modules/c-vasprintf b/modules/c-vasprintf index 868dc16d10..8b012daabe 100644 --- a/modules/c-vasprintf +++ b/modules/c-vasprintf @@ -7,6 +7,7 @@ lib/c-asprintf.c lib/c-vasprintf.c Depends-on: +stdint stdio c-vasnprintf @@ -19,7 +20,7 @@ Include: "c-vasprintf.h" License: -GPL +LGPLv2+ Maintainer: Ben Pfaff