ui/console-vc: Silence warning about sprintf() on OpenBSD
PATCH 1/1 ui/console-vc: Silence warning about sprintf() on OpenBSD
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.
--- ui/console-vc.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)
ui/console-vc.c +3 -3
@@ -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 ; }
Signature · 1 line
2.46.1
Quoted 18 lines · Thomas Huth
The linker on OpenBSD complains:
ld: warning: console-vc.c:824 (../src/ui/console-vc.c:824)([...]):
warning:sprintf()is often misused, please usesnprintf()Using
snprintf()is certainly better here, so let's switch to that function instead.Signed-off-by Thomas Huth <[email protected]>--- ui/console-vc.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)ui/console-vc.c +0
-0
ui/console-vc.c static void vc_putchar(VCChardev *vc, int ch) · line 827
Quoted 10 lines
ui/console-vc.c +3
-3@@ -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
Signature · 3 lines
|: 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 :|
Quoted 4 lines · Daniel P. Berrangé
These two lines are the only place in the code that uses the
char response[40];
Quoted 8 lines
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
Alex Bennée
Reviewed-byQuoted 12 lines · Michael Tokarev
Quoted 9 lines · Daniel P. Berrangé
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);
Quoted 5 lines
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:
Quoted 2 lines
/mjt
Signature · 2 lines
Alex Bennée Virtualisation Tech Lead @ Linaro
Quoted 15 lines · Michael Tokarev
Quoted 4 lines · Daniel P. Berrangé
These two lines are the only place in the code that uses the
char response[40];Quoted 8 lines
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);
Quoted 5 lines
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
Signature · 3 lines
|: 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 :|
Quoted 22 lines · Daniel P. Berrangé
Quoted 15 lines · Michael Tokarev
Quoted 4 lines · Daniel P. Berrangé
These two lines are the only place in the code that uses the
char response[40];Quoted 8 lines
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);Quoted 5 lines
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.. :)
Quoted 8 lines
This is not a performance sensitive path, and using
g_strdup_printfmakes 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
Quoted 10 lines · Thomas Huth
The linker on OpenBSD complains:
ld: warning: console-vc.c:824 (../src/ui/console-vc.c:824)([...]):
warning:sprintf()is often misused, please usesnprintf()Using
snprintf()is certainly better here, so let's switch to that function instead.Signed-off-by Thomas Huth <[email protected]>
Quoted 24 lines
--- ui/console-vc.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)ui/console-vc.c +3
-4@@ -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