[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
vasprintf, c-vasprintf: Remind users to consider the return value
From: |
Bruno Haible |
Subject: |
vasprintf, c-vasprintf: Remind users to consider the return value |
Date: |
Mon, 29 Jul 2024 17:39:16 +0200 |
Eric Blake wrote:
> > The only reasonable change that should be done is to mark the
> > declaration of asprintf and vasprintf with attribute
> > __warn_unused_result__. This way, the compiler will warn about
> > the crappy code.
>
> Makes sense; I would be in favor of that change in gnulib
Done:
2024-07-29 Bruno Haible <bruno@clisp.org>
vasprintf, c-vasprintf: Remind users to consider the return value.
* lib/stdio.in.h (aszprintf, vaszprintf, asprintf, vasprintf): Mark with
_GL_ATTRIBUTE_NODISCARD.
* lib/c-vasprintf.h (c_aszprintf, c_vaszprintf, c_asprintf,
c_vasprintf): Likewise.
diff --git a/lib/c-vasprintf.h b/lib/c-vasprintf.h
index 048ecf0f62..628d04fa6f 100644
--- a/lib/c-vasprintf.h
+++ b/lib/c-vasprintf.h
@@ -17,7 +17,7 @@
#ifndef _C_VASPRINTF_H
#define _C_VASPRINTF_H
-/* This file uses _GL_ATTRIBUTE_FORMAT. */
+/* This file uses _GL_ATTRIBUTE_FORMAT, _GL_ATTRIBUTE_NODISCARD. */
#if !_GL_CONFIG_H_INCLUDED
#error "Please include config.h first."
#endif
@@ -47,8 +47,10 @@ extern "C" {
Formatting takes place in the C locale, that is, the decimal point
used in floating-point formatting directives is always '.'. */
+_GL_ATTRIBUTE_NODISCARD
ptrdiff_t c_aszprintf (char **resultp, const char *format, ...)
_GL_ATTRIBUTE_FORMAT ((_GL_ATTRIBUTE_SPEC_PRINTF_STANDARD, 2, 3));
+_GL_ATTRIBUTE_NODISCARD
ptrdiff_t c_vaszprintf (char **resultp, const char *format, va_list args)
_GL_ATTRIBUTE_FORMAT ((_GL_ATTRIBUTE_SPEC_PRINTF_STANDARD, 2, 0));
@@ -59,8 +61,10 @@ ptrdiff_t c_vaszprintf (char **resultp, const char *format,
va_list args)
Formatting takes place in the C locale, that is, the decimal point
used in floating-point formatting directives is always '.'. */
+_GL_ATTRIBUTE_NODISCARD
int c_asprintf (char **resultp, const char *format, ...)
_GL_ATTRIBUTE_FORMAT ((_GL_ATTRIBUTE_SPEC_PRINTF_STANDARD, 2, 3));
+_GL_ATTRIBUTE_NODISCARD
int c_vasprintf (char **resultp, const char *format, va_list args)
_GL_ATTRIBUTE_FORMAT ((_GL_ATTRIBUTE_SPEC_PRINTF_STANDARD, 2, 0));
diff --git a/lib/stdio.in.h b/lib/stdio.in.h
index 38572382d4..4ea4e3cc16 100644
--- a/lib/stdio.in.h
+++ b/lib/stdio.in.h
@@ -64,8 +64,8 @@
#define _@GUARD_PREFIX@_STDIO_H
/* This file uses _GL_ATTRIBUTE_DEALLOC, _GL_ATTRIBUTE_FORMAT,
- _GL_ATTRIBUTE_MALLOC, _GL_ATTRIBUTE_NOTHROW, GNULIB_POSIXCHECK,
- HAVE_RAW_DECL_*. */
+ _GL_ATTRIBUTE_MALLOC, _GL_ATTRIBUTE_NODISCARD, _GL_ATTRIBUTE_NOTHROW,
+ GNULIB_POSIXCHECK, HAVE_RAW_DECL_*. */
#if !_GL_CONFIG_H_INCLUDED
#error "Please include config.h first."
#endif
@@ -1693,12 +1693,14 @@ _GL_WARN_ON_USE (tmpfile, "tmpfile is not usable on
mingw - "
Failure code EOVERFLOW can only occur when a width > INT_MAX is used.
Therefore, if the format string is valid and does not use %ls/%lc
directives nor widths, the only possible failure code is ENOMEM. */
+_GL_ATTRIBUTE_NODISCARD
_GL_FUNCDECL_SYS (aszprintf, ptrdiff_t,
(char **result, const char *format, ...)
_GL_ATTRIBUTE_FORMAT_PRINTF_STANDARD (2, 3)
_GL_ARG_NONNULL ((1, 2)));
_GL_CXXALIAS_SYS (aszprintf, ptrdiff_t,
(char **result, const char *format, ...));
+_GL_ATTRIBUTE_NODISCARD
_GL_FUNCDECL_SYS (vaszprintf, ptrdiff_t,
(char **result, const char *format, va_list args)
_GL_ATTRIBUTE_FORMAT_PRINTF_STANDARD (2, 0)
@@ -1717,6 +1719,7 @@ _GL_CXXALIAS_SYS (vaszprintf, ptrdiff_t,
# define asprintf rpl_asprintf
# endif
# define GNULIB_overrides_asprintf
+_GL_ATTRIBUTE_NODISCARD
_GL_FUNCDECL_RPL (asprintf, int,
(char **result, const char *format, ...)
_GL_ATTRIBUTE_FORMAT_PRINTF_STANDARD (2, 3)
@@ -1725,6 +1728,7 @@ _GL_CXXALIAS_RPL (asprintf, int,
(char **result, const char *format, ...));
# else
# if !@HAVE_VASPRINTF@
+_GL_ATTRIBUTE_NODISCARD
_GL_FUNCDECL_SYS (asprintf, int,
(char **result, const char *format, ...)
_GL_ATTRIBUTE_FORMAT_PRINTF_STANDARD (2, 3)
@@ -1739,6 +1743,7 @@ _GL_CXXALIASWARN (asprintf);
# define vasprintf rpl_vasprintf
# endif
# define GNULIB_overrides_vasprintf 1
+_GL_ATTRIBUTE_NODISCARD
_GL_FUNCDECL_RPL (vasprintf, int,
(char **result, const char *format, va_list args)
_GL_ATTRIBUTE_FORMAT_PRINTF_STANDARD (2, 0)
@@ -1747,6 +1752,7 @@ _GL_CXXALIAS_RPL (vasprintf, int,
(char **result, const char *format, va_list args));
# else
# if !@HAVE_VASPRINTF@
+_GL_ATTRIBUTE_NODISCARD
_GL_FUNCDECL_SYS (vasprintf, int,
(char **result, const char *format, va_list args)
_GL_ATTRIBUTE_FORMAT_PRINTF_STANDARD (2, 0)