From: Markus Armbruster <armbru@redhat.com>
To: Akihiko Odaki <odaki@rsg.ci.i.u-tokyo.ac.jp>
Cc: qemu-devel@nongnu.org,
"Viktor Prutyanov" <viktor.prutyanov@phystech.edu>,
"Alex Williamson" <alex@shazbot.org>,
"Cédric Le Goater" <clg@redhat.com>,
"Michael Roth" <michael.roth@amd.com>,
"Paolo Bonzini" <pbonzini@redhat.com>,
"Marc-André Lureau" <marcandre.lureau@redhat.com>,
"Daniel P. Berrangé" <berrange@redhat.com>,
"Philippe Mathieu-Daudé" <philmd@linaro.org>,
"Keith Busch" <kbusch@kernel.org>,
"Klaus Jensen" <its@irrelevant.dk>,
"Jesper Devantier" <foss@defmacro.it>,
qemu-block@nongnu.org
Subject: Re: [PATCH v3 3/4] tests: Avoid sprintf() with "%.6f"
Date: Wed, 04 Mar 2026 10:59:01 +0100 [thread overview]
Message-ID: <87wlzrkja2.fsf@pond.sub.org> (raw)
In-Reply-To: <20260304-nvme-v3-3-bbb1b7fd2d0b@rsg.ci.i.u-tokyo.ac.jp> (Akihiko Odaki's message of "Wed, 04 Mar 2026 15:28:35 +0900")
Akihiko Odaki <odaki@rsg.ci.i.u-tokyo.ac.jp> writes:
> A string that represents a double can be long if it is an exponentially
> large number.
I'd like us to spell out that this is cleanup, not a bug fix. I suggest
to start with the warning. Something like this:
tests: Clean up double comparisons to avoid compiler warning
To enable -Wformat-overflow=2, we need to clean up a couple of false
positives:
<insert warnings here>
Now explain why they are false:
These buffers cannot actually overflow because the doubles are
between 0 and 31.0/3 inclusive.
Then justify the solution:
However, formatting doubles just to compare them is silly. Compare
them directly instead. To avoid potential rounding trouble, change
the numbers tested to be representable exactly in double.
> Signed-off-by: Akihiko Odaki <odaki@rsg.ci.i.u-tokyo.ac.jp>
> ---
> tests/unit/test-qobject-input-visitor.c | 8 ++------
> tests/unit/test-qobject-output-visitor.c | 7 ++-----
> 2 files changed, 4 insertions(+), 11 deletions(-)
>
> diff --git a/tests/unit/test-qobject-input-visitor.c b/tests/unit/test-qobject-input-visitor.c
> index 84bdcdf702e0..beee11db4e47 100644
> --- a/tests/unit/test-qobject-input-visitor.c
> +++ b/tests/unit/test-qobject-input-visitor.c
> @@ -500,7 +500,7 @@ static void test_visitor_in_list_struct(TestInputVisitorData *data,
> g_string_append_printf(json, "'number': [");
> sep = "";
> for (i = 0; i < 32; i++) {
> - g_string_append_printf(json, "%s%f", sep, (double)i / 3);
> + g_string_append_printf(json, "%s%f", sep, (double)i / FLT_RADIX);
> sep = ", ";
> }
> g_string_append_printf(json, "], ");
> @@ -583,11 +583,7 @@ static void test_visitor_in_list_struct(TestInputVisitorData *data,
>
> i = 0;
> for (num_list = arrs->number; num_list; num_list = num_list->next) {
> - char expected[32], actual[32];
> -
> - sprintf(expected, "%.6f", (double)i / 3);
> - sprintf(actual, "%.6f", num_list->value);
> - g_assert_cmpstr(expected, ==, actual);
> + g_assert_cmpfloat(num_list->value, ==, (double)i / FLT_RADIX);
> i++;
> }
>
> diff --git a/tests/unit/test-qobject-output-visitor.c b/tests/unit/test-qobject-output-visitor.c
> index 407ab9ed505a..3c47b28f6638 100644
> --- a/tests/unit/test-qobject-output-visitor.c
> +++ b/tests/unit/test-qobject-output-visitor.c
> @@ -538,7 +538,7 @@ static void test_visitor_out_list_struct(TestOutputVisitorData *data,
> }
>
> for (i = 31; i >= 0; i--) {
> - QAPI_LIST_PREPEND(arrs->number, (double)i / 3);
> + QAPI_LIST_PREPEND(arrs->number, (double)i / FLT_RADIX);
> }
>
> for (i = 31; i >= 0; i--) {
> @@ -571,12 +571,9 @@ static void test_visitor_out_list_struct(TestOutputVisitorData *data,
> i = 0;
> QLIST_FOREACH_ENTRY(qlist, e) {
> QNum *qvalue = qobject_to(QNum, qlist_entry_obj(e));
> - char expected[32], actual[32];
>
> g_assert(qvalue);
> - sprintf(expected, "%.6f", (double)i / 3);
> - sprintf(actual, "%.6f", qnum_get_double(qvalue));
> - g_assert_cmpstr(actual, ==, expected);
> + g_assert_cmpfloat(qnum_get_double(qvalue), ==, (double)i / FLT_RADIX);
> i++;
> }
With an improved commit message
Reviewed-by: Markus Armbruster <armbru@redhat.com>
next prev parent reply other threads:[~2026-03-04 9:59 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-03-04 6:28 [PATCH v3 0/4] Fix use-after-free and make format overflow more difficult Akihiko Odaki
2026-03-04 6:28 ` [PATCH v3 1/4] contrib/elf2dmp: Grow PDB URL buffer Akihiko Odaki
2026-03-04 6:28 ` [PATCH v3 2/4] vfio/pci: Grow buffer in vfio_pci_host_match() Akihiko Odaki
2026-03-04 6:28 ` [PATCH v3 3/4] tests: Avoid sprintf() with "%.6f" Akihiko Odaki
2026-03-04 9:59 ` Markus Armbruster [this message]
2026-03-04 6:28 ` [PATCH v3 4/4] meson: Add -Wformat-overflow=2 Akihiko Odaki
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=87wlzrkja2.fsf@pond.sub.org \
--to=armbru@redhat.com \
--cc=alex@shazbot.org \
--cc=berrange@redhat.com \
--cc=clg@redhat.com \
--cc=foss@defmacro.it \
--cc=its@irrelevant.dk \
--cc=kbusch@kernel.org \
--cc=marcandre.lureau@redhat.com \
--cc=michael.roth@amd.com \
--cc=odaki@rsg.ci.i.u-tokyo.ac.jp \
--cc=pbonzini@redhat.com \
--cc=philmd@linaro.org \
--cc=qemu-block@nongnu.org \
--cc=qemu-devel@nongnu.org \
--cc=viktor.prutyanov@phystech.edu \
/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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.