* [PATCH] ui/console-vc: Silence warning about sprintf() on OpenBSD
@ 2024-10-14 15:10 Thomas Huth
2024-10-14 15:15 ` Daniel P. Berrangé
2024-10-15 6:34 ` Marc-André Lureau
0 siblings, 2 replies; 7+ messages in thread
From: Thomas Huth @ 2024-10-14 15:10 UTC (permalink / raw)
To: Marc-André Lureau, qemu-devel; +Cc: Brad Smith, qemu-trivial
The linker on OpenBSD complains:
ld: warning: console-vc.c:824 (../src/ui/console-vc.c:824)([...]):
warning: sprintf() is often misused, please use snprintf()
Using snprintf() is certainly better here, so let's switch to that
function instead.
Signed-off-by: Thomas Huth <thuth@redhat.com>
---
ui/console-vc.c | 6 +++---
1 file changed, 3 insertions(+), 3 deletions(-)
diff --git a/ui/console-vc.c b/ui/console-vc.c
index 8393d532e7..336a1520eb 100644
--- a/ui/console-vc.c
+++ b/ui/console-vc.c
@@ -821,9 +821,9 @@ static void vc_putchar(VCChardev *vc, int ch)
break;
case 6:
/* report cursor position */
- sprintf(response, "\033[%d;%dR",
- (s->y_base + s->y) % s->total_height + 1,
- s->x + 1);
+ snprintf(response, sizeof(response), "\033[%d;%dR",
+ (s->y_base + s->y) % s->total_height + 1,
+ s->x + 1);
vc_respond_str(vc, response);
break;
}
--
2.46.1
^ permalink raw reply related [flat|nested] 7+ messages in thread
* Re: [PATCH] ui/console-vc: Silence warning about sprintf() on OpenBSD
2024-10-14 15:10 [PATCH] ui/console-vc: Silence warning about sprintf() on OpenBSD Thomas Huth
@ 2024-10-14 15:15 ` Daniel P. Berrangé
2024-10-14 19:50 ` Michael Tokarev
2024-10-15 6:34 ` Marc-André Lureau
1 sibling, 1 reply; 7+ messages in thread
From: Daniel P. Berrangé @ 2024-10-14 15:15 UTC (permalink / raw)
To: Thomas Huth; +Cc: Marc-André Lureau, qemu-devel, Brad Smith, qemu-trivial
On Mon, Oct 14, 2024 at 05:10:23PM +0200, Thomas Huth wrote:
> The linker on OpenBSD complains:
>
> ld: warning: console-vc.c:824 (../src/ui/console-vc.c:824)([...]):
> warning: sprintf() is often misused, please use snprintf()
>
> Using snprintf() is certainly better here, so let's switch to that
> function instead.
>
> Signed-off-by: Thomas Huth <thuth@redhat.com>
> ---
> ui/console-vc.c | 6 +++---
> 1 file changed, 3 insertions(+), 3 deletions(-)
>
> diff --git a/ui/console-vc.c b/ui/console-vc.c
> index 8393d532e7..336a1520eb 100644
> --- a/ui/console-vc.c
> +++ b/ui/console-vc.c
> @@ -821,9 +821,9 @@ static void vc_putchar(VCChardev *vc, int ch)
> break;
> case 6:
> /* report cursor position */
> - sprintf(response, "\033[%d;%dR",
> - (s->y_base + s->y) % s->total_height + 1,
> - s->x + 1);
> + snprintf(response, sizeof(response), "\033[%d;%dR",
> + (s->y_base + s->y) % s->total_height + 1,
> + s->x + 1);
> vc_respond_str(vc, response);
These two lines are the only place in the code that uses the
char response[40];
so even better than switching to snprintf, how about just taking
buffer size out of the picture:
g_autofree *response =
g_strdup_printf("\033[%d;%dR",
(s->y_base + s->y) % s->total_height + 1,
s->x + 1);
vc_respond_str(vc, response);
With regards,
Daniel
--
|: https://berrange.com -o- https://www.flickr.com/photos/dberrange :|
|: https://libvirt.org -o- https://fstop138.berrange.com :|
|: https://entangle-photo.org -o- https://www.instagram.com/dberrange :|
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [PATCH] ui/console-vc: Silence warning about sprintf() on OpenBSD
2024-10-14 15:15 ` Daniel P. Berrangé
@ 2024-10-14 19:50 ` Michael Tokarev
2024-10-15 8:08 ` Alex Bennée
2024-10-15 8:14 ` Daniel P. Berrangé
0 siblings, 2 replies; 7+ messages in thread
From: Michael Tokarev @ 2024-10-14 19:50 UTC (permalink / raw)
To: Daniel P. Berrangé, Thomas Huth
Cc: Marc-André Lureau, qemu-devel, Brad Smith, qemu-trivial
On 14.10.2024 18:15, Daniel P. Berrangé wrote:
> These two lines are the only place in the code that uses the
>
> char response[40];
>
> so even better than switching to snprintf, how about just taking
> buffer size out of the picture:
>
> g_autofree *response =
> g_strdup_printf("\033[%d;%dR",
> (s->y_base + s->y) % s->total_height + 1,
> s->x + 1);
> vc_respond_str(vc, response);
What's the reason to perform memory allocation in trivial places
like this? If we're worrying about possible buffer size issue,
maybe asprintf() is a better alternative for such small things?
Fragmenting heap memory for no reason seems too much overkill.
But I'm old-scool, so.. :)
/mjt
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [PATCH] ui/console-vc: Silence warning about sprintf() on OpenBSD
2024-10-14 19:50 ` Michael Tokarev
@ 2024-10-15 8:08 ` Alex Bennée
2024-10-15 8:14 ` Daniel P. Berrangé
1 sibling, 0 replies; 7+ messages in thread
From: Alex Bennée @ 2024-10-15 8:08 UTC (permalink / raw)
To: Michael Tokarev
Cc: Daniel P. Berrangé, Thomas Huth, Marc-André Lureau,
qemu-devel, Brad Smith, qemu-trivial
Michael Tokarev <mjt@tls.msk.ru> writes:
> On 14.10.2024 18:15, Daniel P. Berrangé wrote:
>
>> These two lines are the only place in the code that uses the
>> char response[40];
>> so even better than switching to snprintf, how about just taking
>> buffer size out of the picture:
>> g_autofree *response =
>> g_strdup_printf("\033[%d;%dR",
>> (s->y_base + s->y) % s->total_height + 1,
>> s->x + 1);
>> vc_respond_str(vc, response);
>
> What's the reason to perform memory allocation in trivial places
> like this? If we're worrying about possible buffer size issue,
> maybe asprintf() is a better alternative for such small things?
> Fragmenting heap memory for no reason seems too much overkill.
> But I'm old-scool, so.. :)
I doubt the allocate/free pair will cause much fragmentation but it
doesn't look like we are in any hot path here. Anyway snprintf is
certainly better than sprintf so:
Reviewed-by: Alex Bennée <alex.bennee@linaro.org>
>
> /mjt
--
Alex Bennée
Virtualisation Tech Lead @ Linaro
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [PATCH] ui/console-vc: Silence warning about sprintf() on OpenBSD
2024-10-14 19:50 ` Michael Tokarev
2024-10-15 8:08 ` Alex Bennée
@ 2024-10-15 8:14 ` Daniel P. Berrangé
2024-10-15 11:28 ` Thomas Huth
1 sibling, 1 reply; 7+ messages in thread
From: Daniel P. Berrangé @ 2024-10-15 8:14 UTC (permalink / raw)
To: Michael Tokarev
Cc: Thomas Huth, Marc-André Lureau, qemu-devel, Brad Smith,
qemu-trivial
On Mon, Oct 14, 2024 at 10:50:44PM +0300, Michael Tokarev wrote:
> On 14.10.2024 18:15, Daniel P. Berrangé wrote:
>
> > These two lines are the only place in the code that uses the
> >
> > char response[40];
> >
> > so even better than switching to snprintf, how about just taking
> > buffer size out of the picture:
> >
> > g_autofree *response =
> > g_strdup_printf("\033[%d;%dR",
> > (s->y_base + s->y) % s->total_height + 1,
> > s->x + 1);
> > vc_respond_str(vc, response);
>
> What's the reason to perform memory allocation in trivial places
> like this? If we're worrying about possible buffer size issue,
> maybe asprintf() is a better alternative for such small things?
> Fragmenting heap memory for no reason seems too much overkill.
> But I'm old-scool, so.. :)
This is not a performance sensitive path, and using g_strdup_printf
makes it robust against any futher changes in the future. In the
context of all the memory allocation QEMU does, I can't see this
making any difference to heap fragmentation whatsoever.
snprintf with fixed buffers should only be used where there's a
demonstratable performance win, and the return value actually
checked with an assert() to prove we're not overflowing.
With regards,
Daniel
--
|: https://berrange.com -o- https://www.flickr.com/photos/dberrange :|
|: https://libvirt.org -o- https://fstop138.berrange.com :|
|: https://entangle-photo.org -o- https://www.instagram.com/dberrange :|
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [PATCH] ui/console-vc: Silence warning about sprintf() on OpenBSD
2024-10-15 8:14 ` Daniel P. Berrangé
@ 2024-10-15 11:28 ` Thomas Huth
0 siblings, 0 replies; 7+ messages in thread
From: Thomas Huth @ 2024-10-15 11:28 UTC (permalink / raw)
To: Daniel P. Berrangé, Michael Tokarev
Cc: Marc-André Lureau, qemu-devel, Brad Smith, qemu-trivial
On 15/10/2024 10.14, Daniel P. Berrangé wrote:
> On Mon, Oct 14, 2024 at 10:50:44PM +0300, Michael Tokarev wrote:
>> On 14.10.2024 18:15, Daniel P. Berrangé wrote:
>>
>>> These two lines are the only place in the code that uses the
>>>
>>> char response[40];
>>>
>>> so even better than switching to snprintf, how about just taking
>>> buffer size out of the picture:
>>>
>>> g_autofree *response =
>>> g_strdup_printf("\033[%d;%dR",
>>> (s->y_base + s->y) % s->total_height + 1,
>>> s->x + 1);
>>> vc_respond_str(vc, response);
>>
>> What's the reason to perform memory allocation in trivial places
>> like this? If we're worrying about possible buffer size issue,
>> maybe asprintf() is a better alternative for such small things?
>> Fragmenting heap memory for no reason seems too much overkill.
>> But I'm old-scool, so.. :)
>
> This is not a performance sensitive path, and using g_strdup_printf
> makes it robust against any futher changes in the future. In the
> context of all the memory allocation QEMU does, I can't see this
> making any difference to heap fragmentation whatsoever.
>
> snprintf with fixed buffers should only be used where there's a
> demonstratable performance win, and the return value actually
> checked with an assert() to prove we're not overflowing.
While I'm obviously old-schooled, too (since I used snprintf here in the
first place), I agree with Daniel that the few cycles of improved
performance likely aren't justified in this case here, so g_strdup_printf()
is a better choice here indeed. I just sent a v2 with that change.
Thomas
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [PATCH] ui/console-vc: Silence warning about sprintf() on OpenBSD
2024-10-14 15:10 [PATCH] ui/console-vc: Silence warning about sprintf() on OpenBSD Thomas Huth
2024-10-14 15:15 ` Daniel P. Berrangé
@ 2024-10-15 6:34 ` Marc-André Lureau
1 sibling, 0 replies; 7+ messages in thread
From: Marc-André Lureau @ 2024-10-15 6:34 UTC (permalink / raw)
To: Thomas Huth; +Cc: qemu-devel, Brad Smith, qemu-trivial
On Mon, Oct 14, 2024 at 7:10 PM Thomas Huth <thuth@redhat.com> wrote:
>
> The linker on OpenBSD complains:
>
> ld: warning: console-vc.c:824 (../src/ui/console-vc.c:824)([...]):
> warning: sprintf() is often misused, please use snprintf()
>
> Using snprintf() is certainly better here, so let's switch to that
> function instead.
>
> Signed-off-by: Thomas Huth <thuth@redhat.com>
Reviewed-by: Marc-André Lureau <marcandre.lureau@redhat.com>
> ---
> ui/console-vc.c | 6 +++---
> 1 file changed, 3 insertions(+), 3 deletions(-)
>
> diff --git a/ui/console-vc.c b/ui/console-vc.c
> index 8393d532e7..336a1520eb 100644
> --- a/ui/console-vc.c
> +++ b/ui/console-vc.c
> @@ -821,9 +821,9 @@ static void vc_putchar(VCChardev *vc, int ch)
> break;
> case 6:
> /* report cursor position */
> - sprintf(response, "\033[%d;%dR",
> - (s->y_base + s->y) % s->total_height + 1,
> - s->x + 1);
> + snprintf(response, sizeof(response), "\033[%d;%dR",
> + (s->y_base + s->y) % s->total_height + 1,
> + s->x + 1);
> vc_respond_str(vc, response);
> break;
> }
> --
> 2.46.1
>
^ permalink raw reply [flat|nested] 7+ messages in thread
end of thread, other threads:[~2024-10-15 11:29 UTC | newest]
Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-10-14 15:10 [PATCH] ui/console-vc: Silence warning about sprintf() on OpenBSD Thomas Huth
2024-10-14 15:15 ` Daniel P. Berrangé
2024-10-14 19:50 ` Michael Tokarev
2024-10-15 8:08 ` Alex Bennée
2024-10-15 8:14 ` Daniel P. Berrangé
2024-10-15 11:28 ` Thomas Huth
2024-10-15 6:34 ` Marc-André Lureau
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).