Skip to contentSkip to conversation

ui/console-vc: Silence warning about sprintf() on OpenBSD

PATCH Applied Thomas Huth 5 participants 6 messages Oct 15, 2024 – Oct 18, 2024

lore
Share & tools
lore
#0 lore raw

PATCH v2 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 g_strdup_printf() is certainly better here, so let's switch to that function instead.

---
v2: Use g_strdup_printf() instead of snprintf()

 ui/console-vc.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
ui/console-vc.c +2 -2
index 8393d532e7..53fcee88f4 100644
@@ -648,7 +648,7 @@ static void vc_putchar(VCChardev *vc, int ch)
     QemuTextConsole *s = vc->console;
     int i;
     int x, y;
-    char response[40];
+    g_autofree char *response = NULL;
 
     switch(vc->state) {
     case TTY_STATE_NORM:
@@ -821,7 +821,7 @@ static void vc_putchar(VCChardev *vc, int ch)
                     break;
                 case 6:
                     /* report cursor position */
-                    sprintf(response, "\033[%d;%dR",
+                    response = g_strdup_printf("\033[%d;%dR",
                            (s->y_base + s->y) % s->total_height + 1,
                             s->x + 1);
                     vc_respond_str(vc, response);
Signature · 1 line
2.47.0
Reviewed-by on v2 1/1
#1 lore raw
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 use snprintf()

Using g_strdup_printf() is certainly better here, so let's switch to that function instead.

Quoted 31 lines

---
v2: Use g_strdup_printf() instead of snprintf()

 ui/console-vc.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
ui/console-vc.c +2 -3
index 8393d532e7..53fcee88f4 100644
@@ -648,7 +648,7 @@ static void vc_putchar(VCChardev *vc, int ch)
     QemuTextConsole *s = vc->console;
     int i;
     int x, y;
-    char response[40];
+    g_autofree char *response = NULL;
 
     switch(vc->state) {
     case TTY_STATE_NORM:
@@ -821,7 +821,7 @@ static void vc_putchar(VCChardev *vc, int ch)
                     break;
                 case 6:
                     /* report cursor position */
-                    sprintf(response, "\033[%d;%dR",
+                    response = g_strdup_printf("\033[%d;%dR",
                            (s->y_base + s->y) % s->total_height + 1,
                             s->x + 1);
                     vc_respond_str(vc, response);
--

2.47.0

Reviewed-by on v2 1/1
#2 lore raw
In reply to Thomas Huth
Quoted 9 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 use snprintf()

Using g_strdup_printf() is certainly better here, so let's switch to that function instead.

Signature · 2 lines
Alex Bennée
Virtualisation Tech Lead @ Linaro
Reviewed-by on v2 1/1
#3 lore raw
In reply to Thomas Huth
Quoted 29 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 use snprintf()

Using g_strdup_printf() is certainly better here, so let's switch to that function instead.

---
v2: Use g_strdup_printf() instead of snprintf()

  ui/console-vc.c | 4 ++--
  1 file changed, 2 insertions(+), 2 deletions(-)
ui/console-vc.c +1 -1
index 8393d532e7..53fcee88f4 100644
@@ -648,7 +648,7 @@ static void vc_putchar(VCChardev *vc, int ch)
      QemuTextConsole *s = vc->console;
      int i;
      int x, y;
-    char response[40];
+    g_autofree char *response = NULL;
 
      switch(vc->state) {
      case TTY_STATE_NORM:

ui/console-vc.c static void vc_putchar(VCChardev *vc, int ch) · line 827

Quoted 8 lines
ui/console-vc.c +1 -1
@@ -821,7 +821,7 @@ static void vc_putchar(VCChardev *vc, int ch)
                      break;
                  case 6:
                      /* report cursor position */
-                    sprintf(response, "\033[%d;%dR",
+                    response = g_strdup_printf("\033[%d;%dR",
                             (s->y_base + s->y) % s->total_height + 1,
                              s->x + 1);
                      vc_respond_str(vc, response);

Could be better by limiting the scope of the variable to this case. But either way,

r~

#4 lore raw
Quoted 31 lines · Richard Henderson
Quoted 30 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 use snprintf()

Using g_strdup_printf() is certainly better here, so let's switch to that function instead.

---
  v2: Use g_strdup_printf() instead of snprintf()

  ui/console-vc.c | 4 ++--

  1 file changed, 2 insertions(+), 2 deletions(-)
ui/console-vc.c +1 -1
index 8393d532e7..53fcee88f4 100644
@@ -648,7 +648,7 @@ static void vc_putchar(VCChardev *vc, int ch)
       QemuTextConsole *s = vc->console;
       int i;
       int x, y;
-    char response[40];
+    g_autofree char *response = NULL;
       switch(vc->state) {

      case TTY_STATE_NORM:

ui/console-vc.c +0 -0
@@ -821,7 +821,7 @@ static void vc_putchar(VCChardev *vc, int ch)
                       break;

                  case 6:

Quoted 8 lines
Quoted 6 lines

                      /* report cursor position */
-                    sprintf(response, "\033[%d;%dR",
+                    response = g_strdup_printf("\033[%d;%dR", (s->y_base + s->y) % s->total_height + 1, s->x + 1);
                      vc_respond_str(vc, response);

Could be better by limiting the scope of the variable to this case.

I tried that first, but then I need curly braces around the whole case block to avoid compiler warnings, which is ugly, too, so I decided to keep it at the top.

Thomas

Reviewed-by on v2 1/1 Applied
#5 lore raw
In reply to Thomas Huth
Quoted 7 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 use snprintf()

Using g_strdup_printf() is certainly better here, so let's switch to that function instead.

Queued for qemu-trivial, thanks!

/mjt