From: "Philippe Mathieu-Daudé" <philmd@linaro.org>
To: Paolo Bonzini <pbonzini@redhat.com>, qemu-devel@nongnu.org
Cc: Akihiko Odaki <akihiko.odaki@daynix.com>
Subject: Re: [PATCH v2] disas/cris: Pass buffer size to format_dec() to avoid overflow warning
Date: Tue, 21 Nov 2023 08:54:03 +0100 [thread overview]
Message-ID: <5c82ad73-aeee-4f46-9559-3b6ea12aadab@linaro.org> (raw)
In-Reply-To: <20231120214520.59431-1-pbonzini@redhat.com>
On 20/11/23 22:45, Paolo Bonzini wrote:
> From: Philippe Mathieu-Daudé <philmd@linaro.org>
>
> Propagate the buffer size to format_dec() and use snprintf().
>
> This should silence this UBSan -Wformat-overflow warning:
"produced when using GCC 12.1.0:"
>
> In file included from /usr/include/stdio.h:906,
> from include/qemu/osdep.h:114,
> from ../disas/cris.c:21:
> In function 'sprintf',
> inlined from 'format_dec' at ../disas/cris.c:1737:3,
> inlined from 'print_with_operands' at ../disas/cris.c:2477:12,
> inlined from 'print_insn_cris_generic.constprop' at ../disas/cris.c:2690:8:
> /usr/include/bits/stdio2.h:30:10: warning: null destination pointer [-Wformat-overflow=]
> 30 | return __builtin___sprintf_chk (__s, __USE_FORTIFY_LEVEL - 1,
> | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> 31 | __glibc_objsize (__s), __fmt,
> | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> 32 | __va_arg_pack ());
> | ~~~~~~~~~~~~~~~~~
>
> Reported-by: Akihiko Odaki <akihiko.odaki@daynix.com>
> Signed-off-by: Philippe Mathieu-Daudé <philmd@linaro.org>
> Message-ID: <20231120132222.82138-1-philmd@linaro.org>
> [Rewritten to fix logic and avoid repeated expression. - Paolo]
> Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
> ---
> disas/cris.c | 26 ++++++++++++++++----------
> 1 file changed, 16 insertions(+), 10 deletions(-)
>
> diff --git a/disas/cris.c b/disas/cris.c
> index 0b0a3fb9165..409a224c5d1 100644
> --- a/disas/cris.c
> +++ b/disas/cris.c
> @@ -1731,10 +1731,10 @@ format_hex (unsigned long number,
> unsigned (== 0). */
>
> static char *
> -format_dec (long number, char *outbuffer, int signedp)
> +format_dec (long number, char *outbuffer, size_t outsize, int signedp)
> {
> last_immediate = number;
> - sprintf (outbuffer, signedp ? "%ld" : "%lu", number);
> + snprintf (outbuffer, outsize, signedp ? "%ld" : "%lu", number);
>
> return outbuffer + strlen (outbuffer);
> }
> @@ -1876,6 +1876,12 @@ print_flags (struct cris_disasm_data *disdata, unsigned int insn, char *cp)
> return cp;
> }
>
> +#define FORMAT_DEC(number, tp, signedp) \
> + format_dec (number, tp, ({ \
> + assert(tp >= temp && tp <= temp + sizeof(temp)); \
> + temp + sizeof(temp) - tp; \
> + }), signedp)
Thank you Paolo for this respin!
Reviewed-by: Philippe Mathieu-Daudé <philmd@linaro.org>
prev parent reply other threads:[~2023-11-21 7:55 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-11-20 21:45 [PATCH v2] disas/cris: Pass buffer size to format_dec() to avoid overflow warning Paolo Bonzini
2023-11-21 5:32 ` Akihiko Odaki
2023-11-21 7:54 ` Philippe Mathieu-Daudé [this message]
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=5c82ad73-aeee-4f46-9559-3b6ea12aadab@linaro.org \
--to=philmd@linaro.org \
--cc=akihiko.odaki@daynix.com \
--cc=pbonzini@redhat.com \
--cc=qemu-devel@nongnu.org \
/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;
as well as URLs for NNTP newsgroup(s).