From: Andrew Burgess <aburgess@redhat.com>
To: Simon Marchi <simon.marchi@efficios.com>,
gdb-patches@sourceware.org, binutils@sourceware.org
Cc: Simon Marchi <simon.marchi@efficios.com>
Subject: Re: [PATCH 01/13] gdbsupport: remove uses of vsprintf
Date: Mon, 17 Aug 2026 16:47:55 +0100 [thread overview]
Message-ID: <87pkzg3g0k.fsf@redhat.com> (raw)
In-Reply-To: <20260817151646.152571-2-simon.marchi@efficios.com>
Simon Marchi <simon.marchi@efficios.com> writes:
> When building on macOS, I get:
>
> CXX common-utils.o
> /Users/smarchi/src/binutils-gdb/gdbsupport/common-utils.cc:106:3: error: 'vsprintf' is deprecated: This function is provided for compatibility reasons only. Due to security concerns inherent in the design of sprintf(3), it is highly recommended that you use vsnprintf(3) instead. [-Werror,-Wdeprecated-declarations]
> 106 | vsprintf (&str[0], fmt, vp);
> | ^
> /Users/smarchi/src/binutils-gdb/gdbsupport/common-utils.cc:128:3: error: 'vsprintf' is deprecated: This function is provided for compatibility reasons only. Due to security concerns inherent in the design of sprintf(3), it is highly recommended that you use vsnprintf(3) instead. [-Werror,-Wdeprecated-declarations]
> 128 | vsprintf (&str[0], fmt, args);
> | ^
> /Users/smarchi/src/binutils-gdb/gdbsupport/common-utils.cc:166:3: error: 'vsprintf' is deprecated: This function is provided for compatibility reasons only. Due to security concerns inherent in the design of sprintf(3), it is highly recommended that you use vsnprintf(3) instead. [-Werror,-Wdeprecated-declarations]
> 166 | vsprintf (&str[curr_size], fmt, args);
> | ^
>
> We know that those calls should be safe because we computed the size that
> fmt+args take just before, and allocated that many bytes. But I also
> don't see a real downside in switching those calls to use vsnprintf and
> double check that everything went right.
>
> Change the type of the existing "size" variable in "string_vprintf" to
> "int", since that's what vsnprintf returns.
Approved-By: Andrew Burgess <aburgess@redhat.com>
Thanks,
Andrew
>
> Change-Id: I589d9a170fdd15cc31b44b76689c6d8c324e340a
> ---
> gdbsupport/common-utils.cc | 18 +++++++++---------
> 1 file changed, 9 insertions(+), 9 deletions(-)
>
> diff --git a/gdbsupport/common-utils.cc b/gdbsupport/common-utils.cc
> index 3ae3afcc380b..f31699be13a1 100644
> --- a/gdbsupport/common-utils.cc
> +++ b/gdbsupport/common-utils.cc
> @@ -92,10 +92,9 @@ std::string
> string_printf (const char* fmt, ...)
> {
> va_list vp;
> - int size;
>
> va_start (vp, fmt);
> - size = vsnprintf (NULL, 0, fmt, vp);
> + int size = vsnprintf (NULL, 0, fmt, vp);
> va_end (vp);
>
> std::string str (size, '\0');
> @@ -103,7 +102,8 @@ string_printf (const char* fmt, ...)
> /* C++11 and later guarantee std::string uses contiguous memory and
> always includes the terminating '\0'. */
> va_start (vp, fmt);
> - vsprintf (&str[0], fmt, vp);
> + int ret = vsnprintf (&str[0], size + 1, fmt, vp);
> + gdb_assert (ret == size);
> va_end (vp);
>
> return str;
> @@ -115,17 +115,17 @@ std::string
> string_vprintf (const char* fmt, va_list args)
> {
> va_list vp;
> - size_t size;
>
> va_copy (vp, args);
> - size = vsnprintf (NULL, 0, fmt, vp);
> + int size = vsnprintf (NULL, 0, fmt, vp);
> va_end (vp);
>
> std::string str (size, '\0');
>
> /* C++11 and later guarantee std::string uses contiguous memory and
> always includes the terminating '\0'. */
> - vsprintf (&str[0], fmt, args);
> + int ret = vsnprintf (&str[0], size + 1, fmt, args);
> + gdb_assert (ret == size);
>
> return str;
> }
> @@ -152,10 +152,9 @@ std::string &
> string_vappendf (std::string &str, const char *fmt, va_list args)
> {
> va_list vp;
> - int grow_size;
>
> va_copy (vp, args);
> - grow_size = vsnprintf (NULL, 0, fmt, vp);
> + int grow_size = vsnprintf (NULL, 0, fmt, vp);
> va_end (vp);
>
> size_t curr_size = str.size ();
> @@ -163,7 +162,8 @@ string_vappendf (std::string &str, const char *fmt, va_list args)
>
> /* C++11 and later guarantee std::string uses contiguous memory and
> always includes the terminating '\0'. */
> - vsprintf (&str[curr_size], fmt, args);
> + int ret = vsnprintf (&str[curr_size], grow_size + 1, fmt, args);
> + gdb_assert (ret == grow_size);
>
> return str;
> }
> --
> 2.55.0
next prev parent reply other threads:[~2026-08-17 15:48 UTC|newest]
Thread overview: 35+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-08-17 15:16 [PATCH 00/13] Fix various warnings when building on macOS Simon Marchi
2026-08-17 15:16 ` [PATCH 01/13] gdbsupport: remove uses of vsprintf Simon Marchi
2026-08-17 15:47 ` Andrew Burgess [this message]
2026-08-17 15:16 ` [PATCH 02/13] gdbsupport: remove uses of sprintf Simon Marchi
2026-08-17 15:49 ` Andrew Burgess
2026-08-17 15:16 ` [PATCH 03/13] opcodes/z80: remove use " Simon Marchi
2026-08-18 6:40 ` Jan Beulich
2026-08-18 16:48 ` Simon Marchi
2026-08-17 15:16 ` [PATCH 04/13] sim/ppc: make defines.h sed command portable Simon Marchi
2026-08-17 15:36 ` Andrew Burgess
2026-08-17 15:16 ` [PATCH 05/13] sim/m32r: fix unused variable warning on non-Linux hosts Simon Marchi
2026-08-17 15:36 ` Andrew Burgess
2026-08-17 15:16 ` [PATCH 06/13] sim/m32r: fix unused function warnings " Simon Marchi
2026-08-17 15:37 ` Andrew Burgess
2026-08-17 15:16 ` [PATCH 07/13] gdb/csky: remove uses of sprintf Simon Marchi
2026-08-17 16:26 ` Andrew Burgess
2026-08-17 17:03 ` Simon Marchi
2026-08-17 20:50 ` Tom Tromey
2026-08-18 18:26 ` Simon Marchi
2026-08-17 15:16 ` [PATCH 08/13] gdb/dwarf2: " Simon Marchi
2026-08-17 16:35 ` Andrew Burgess
2026-08-17 15:16 ` [PATCH 09/13] gdb/elfread: remove use " Simon Marchi
2026-08-17 16:38 ` Andrew Burgess
2026-08-17 15:16 ` [PATCH 10/13] gdbsupport: add xstrcpy Simon Marchi
2026-08-17 16:45 ` Andrew Burgess
2026-08-17 17:30 ` Simon Marchi
2026-08-17 15:16 ` [PATCH 11/13] gdb/remote-fileio: remove uses of sprintf Simon Marchi
2026-08-17 16:53 ` Andrew Burgess
2026-08-17 15:16 ` [PATCH 12/13] gdb/remote: " Simon Marchi
2026-08-17 16:51 ` Andrew Burgess
2026-08-17 17:34 ` Simon Marchi
2026-08-17 15:16 ` [PATCH 13/13] gdb/tracepoint: " Simon Marchi
2026-08-17 16:51 ` Andrew Burgess
2026-08-17 20:52 ` [PATCH 00/13] Fix various warnings when building on macOS Tom Tromey
2026-08-18 18:10 ` Simon Marchi
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=87pkzg3g0k.fsf@redhat.com \
--to=aburgess@redhat.com \
--cc=binutils@sourceware.org \
--cc=gdb-patches@sourceware.org \
--cc=simon.marchi@efficios.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox