Skip to contentSkip to conversation

jazz_led: fix bad snprintf

PATCH Applied Paolo Bonzini 4 participants 11 messages May 3, 2017 – May 5, 2017

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 jazz_led: fix bad snprintf

Detected by GCC 7's -Wformat-truncation. snprintf writes at most 2 bytes here including the terminating NUL, so the result is truncated. In addition, the newline at the end is pointless. Fix the buffer size and the format string.

---
 hw/display/jazz_led.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
hw/display/jazz_led.c +2 -2
index b72fdb1717..3c97d56434 100644
@@ -227,13 +227,13 @@ static void jazz_led_invalidate_display(void *opaque)
 static void jazz_led_text_update(void *opaque, console_ch_t *chardata)
 {
     LedState *s = opaque;
-    char buf[2];
+    char buf[3];
 
     dpy_text_cursor(s->con, -1, -1);
     qemu_console_resize(s->con, 2, 1);
 
     /* TODO: draw the segments */
-    snprintf(buf, 2, "%02hhx\n", s->segments);
+    snprintf(buf, 3, "%02hhx", s->segments);
     console_write_ch(chardata++, ATTR2CHTYPE(buf[0], QEMU_COLOR_BLUE,
                                              QEMU_COLOR_BLACK, 1));
     console_write_ch(chardata++, ATTR2CHTYPE(buf[1], QEMU_COLOR_BLUE,
Signature · 1 line
2.12.2
Reviewed-by on v1 1/1
#1 lore raw
Quoted 18 lines · Paolo Bonzini

Detected by GCC 7's -Wformat-truncation. snprintf writes at most 2 bytes here including the terminating NUL, so the result is truncated. In addition, the newline at the end is pointless. Fix the buffer size and the format string.

---
 hw/display/jazz_led.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
hw/display/jazz_led.c +1 -1
index b72fdb1717..3c97d56434 100644
@@ -227,13 +227,13 @@ static void jazz_led_invalidate_display(void *opaque)
 static void jazz_led_text_update(void *opaque, console_ch_t *chardata)
 {
     LedState *s = opaque;
-    char buf[2];
+    char buf[3];

hw/display/jazz_led.c static void jazz_led_invalidate_display(void *opaque) · line 239

Quoted 10 lines
hw/display/jazz_led.c +1 -1
 
     dpy_text_cursor(s->con, -1, -1);
     qemu_console_resize(s->con, 2, 1);
 
     /* TODO: draw the segments */
-    snprintf(buf, 2, "%02hhx\n", s->segments);
+    snprintf(buf, 3, "%02hhx", s->segments);
     console_write_ch(chardata++, ATTR2CHTYPE(buf[0], QEMU_COLOR_BLUE,
                                              QEMU_COLOR_BLACK, 1));
     console_write_ch(chardata++, ATTR2CHTYPE(buf[1], QEMU_COLOR_BLUE,
Reviewed-by on v1 1/1
#2 lore raw
In reply to Paolo Bonzini
Quoted 18 lines · Paolo Bonzini

Detected by GCC 7's -Wformat-truncation. snprintf writes at most 2 bytes here including the terminating NUL, so the result is truncated. In addition, the newline at the end is pointless. Fix the buffer size and the format string.

---
 hw/display/jazz_led.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
hw/display/jazz_led.c +1 -1
index b72fdb1717..3c97d56434 100644
@@ -227,13 +227,13 @@ static void jazz_led_invalidate_display(void *opaque)
 static void jazz_led_text_update(void *opaque, console_ch_t *chardata)
 {
     LedState *s = opaque;
-    char buf[2];
+    char buf[3];

hw/display/jazz_led.c static void jazz_led_invalidate_display(void *opaque) · line 239

Quoted 10 lines
hw/display/jazz_led.c +1 -1
 
     dpy_text_cursor(s->con, -1, -1);
     qemu_console_resize(s->con, 2, 1);
 
     /* TODO: draw the segments */
-    snprintf(buf, 2, "%02hhx\n", s->segments);
+    snprintf(buf, 3, "%02hhx", s->segments);
     console_write_ch(chardata++, ATTR2CHTYPE(buf[0], QEMU_COLOR_BLUE,
                                              QEMU_COLOR_BLACK, 1));
     console_write_ch(chardata++, ATTR2CHTYPE(buf[1], QEMU_COLOR_BLUE,

Since we're only every interested in the first two characters, the truncation is totally harmless. Thus, your patch cleans doesn't really "fix bad snprintf", it cleans up an unclean one. Consider rewording the commit message accordingly.

Regardless,

#3 lore raw
Quoted 24 lines · Markus Armbruster
Quoted 22 lines · Paolo Bonzini

Detected by GCC 7's -Wformat-truncation. snprintf writes at most 2 bytes here including the terminating NUL, so the result is truncated. In addition, the newline at the end is pointless. Fix the buffer size and the format string.

---
 hw/display/jazz_led.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
hw/display/jazz_led.c +1 -1
index b72fdb1717..3c97d56434 100644
@@ -227,13 +227,13 @@ static void jazz_led_invalidate_display(void *opaque)
 static void jazz_led_text_update(void *opaque, console_ch_t *chardata)
 {
     LedState *s = opaque;
-    char buf[2];
+    char buf[3];
 
     dpy_text_cursor(s->con, -1, -1);
     qemu_console_resize(s->con, 2, 1);
 
Quoted 11 lines

hw/display/jazz_led.c static void jazz_led_invalidate_display(void *opaque) · line 239

Quoted 6 lines
hw/display/jazz_led.c +1 -1
     /* TODO: draw the segments */
-    snprintf(buf, 2, "%02hhx\n", s->segments);
+    snprintf(buf, 3, "%02hhx", s->segments);
     console_write_ch(chardata++, ATTR2CHTYPE(buf[0], QEMU_COLOR_BLUE,
                                              QEMU_COLOR_BLACK, 1));
     console_write_ch(chardata++, ATTR2CHTYPE(buf[1], QEMU_COLOR_BLUE,

Since we're only every interested in the first two characters, the truncation is totally harmless. Thus, your patch cleans doesn't really "fix bad snprintf", it cleans up an unclean one. Consider rewording the commit message accordingly.

My reading is that buf[1] is always '\0' in the current code: "snprintf writes at most 2 bytes here including the terminating NUL, so the result is truncated".

Paolo

Quoted 3 lines

Regardless,

#4 lore raw
Quoted 39 lines · Paolo Bonzini
Quoted 24 lines · Markus Armbruster
Quoted 22 lines · Paolo Bonzini

Detected by GCC 7's -Wformat-truncation. snprintf writes at most 2 bytes here including the terminating NUL, so the result is truncated. In addition, the newline at the end is pointless. Fix the buffer size and the format string.

---
 hw/display/jazz_led.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
hw/display/jazz_led.c +1 -1
index b72fdb1717..3c97d56434 100644
@@ -227,13 +227,13 @@ static void jazz_led_invalidate_display(void *opaque)
 static void jazz_led_text_update(void *opaque, console_ch_t *chardata)
 {
     LedState *s = opaque;
-    char buf[2];
+    char buf[3];
 
     dpy_text_cursor(s->con, -1, -1);
     qemu_console_resize(s->con, 2, 1);
 
Quoted 11 lines

hw/display/jazz_led.c static void jazz_led_invalidate_display(void *opaque) · line 239

Quoted 6 lines
hw/display/jazz_led.c +1 -1
     /* TODO: draw the segments */
-    snprintf(buf, 2, "%02hhx\n", s->segments);
+    snprintf(buf, 3, "%02hhx", s->segments);
     console_write_ch(chardata++, ATTR2CHTYPE(buf[0], QEMU_COLOR_BLUE,
                                              QEMU_COLOR_BLACK, 1));
     console_write_ch(chardata++, ATTR2CHTYPE(buf[1], QEMU_COLOR_BLUE,

Since we're only every interested in the first two characters, the truncation is totally harmless. Thus, your patch cleans doesn't really "fix bad snprintf", it cleans up an unclean one. Consider rewording the commit message accordingly.

Quoted 3 lines

My reading is that buf[1] is always '\0' in the current code: "snprintf writes at most 2 bytes here including the terminating NUL, so the result is truncated".

I agree, from printf(3):

"int snprintf(char *str, size_t size, const char *format, ...);
...
The functions snprintf() and vsnprintf() write at most size bytes (including the terminating null byte ('\0')) to str."

Laurent

#5 lore raw
In reply to Paolo Bonzini
Quoted 37 lines · Paolo Bonzini
Quoted 24 lines · Markus Armbruster
Quoted 22 lines · Paolo Bonzini

Detected by GCC 7's -Wformat-truncation. snprintf writes at most 2 bytes here including the terminating NUL, so the result is truncated. In addition, the newline at the end is pointless. Fix the buffer size and the format string.

---
 hw/display/jazz_led.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
hw/display/jazz_led.c +1 -1
index b72fdb1717..3c97d56434 100644
@@ -227,13 +227,13 @@ static void jazz_led_invalidate_display(void *opaque)
 static void jazz_led_text_update(void *opaque, console_ch_t *chardata)
 {
     LedState *s = opaque;
-    char buf[2];
+    char buf[3];
 
     dpy_text_cursor(s->con, -1, -1);
     qemu_console_resize(s->con, 2, 1);
 
Quoted 11 lines

hw/display/jazz_led.c static void jazz_led_invalidate_display(void *opaque) · line 239

Quoted 6 lines
hw/display/jazz_led.c +1 -1
     /* TODO: draw the segments */
-    snprintf(buf, 2, "%02hhx\n", s->segments);
+    snprintf(buf, 3, "%02hhx", s->segments);
     console_write_ch(chardata++, ATTR2CHTYPE(buf[0], QEMU_COLOR_BLUE,
                                              QEMU_COLOR_BLACK, 1));
     console_write_ch(chardata++, ATTR2CHTYPE(buf[1], QEMU_COLOR_BLUE,

Since we're only every interested in the first two characters, the truncation is totally harmless. Thus, your patch cleans doesn't really "fix bad snprintf", it cleans up an unclean one. Consider rewording the commit message accordingly.

Quoted 3 lines

My reading is that buf[1] is always '\0' in the current code: "snprintf writes at most 2 bytes here including the terminating NUL, so the result is truncated".

You're right; I forgot that snprintf() always adds a NUL. So this *is*
broken: we write NUL instead of the second digit. Mentioning this in the commit message wouldn't hurt.

Quoted 6 lines

Paolo

Quoted 3 lines

Regardless,

#6 lore raw
Quoted 6 lines · Markus Armbruster
Quoted 2 lines

snprintf writes at most 2 bytes here including the terminating NUL, so the result is truncated".

You're right; I forgot that snprintf() always adds a NUL. So this *is*
broken: we write NUL instead of the second digit. Mentioning this in the commit message wouldn't hurt.

Well, it does, see the quote. :)

Paolo

#7 lore raw
Quoted 9 lines · Paolo Bonzini
Quoted 6 lines · Markus Armbruster
Quoted 2 lines

snprintf writes at most 2 bytes here including the terminating NUL, so the result is truncated".

You're right; I forgot that snprintf() always adds a NUL. So this *is*
broken: we write NUL instead of the second digit. Mentioning this in the commit message wouldn't hurt.

Well, it does, see the quote. :)

Yes, snprintf() truncates, but that's *internal* to jazz_led_text_update(). The *external* effect isn't quite truncation, it's writing NUL to the console instead of the second digit. That's truncation only if the console ignores NUL. It might; I don't know. Anyway, no big deal.

#8 lore raw
In reply to Paolo Bonzini
Quoted 4 lines · Paolo Bonzini

Detected by GCC 7's -Wformat-truncation. snprintf writes at most 2 bytes here including the terminating NUL, so the result is truncated. In addition, the newline at the end is pointless. Fix the buffer size and the format string.

Polo, that's quite a bit too bureaucratic,
but where's your s-o-b for this patch? :)

Thanks,

/mjt

#9 lore raw
Quoted 8 lines · Michael Tokarev
Quoted 4 lines · Paolo Bonzini

Detected by GCC 7's -Wformat-truncation. snprintf writes at most 2 bytes here including the terminating NUL, so the result is truncated. In addition, the newline at the end is pointless. Fix the buffer size and the format string.

Polo, that's quite a bit too bureaucratic,
but where's your s-o-b for this patch? :)

Oops, here:

Paolo

Quoted 5 lines

Thanks,

/mjt

#10 lore raw
In reply to Paolo Bonzini
Quoted 4 lines · Paolo Bonzini

Detected by GCC 7's -Wformat-truncation. snprintf writes at most 2 bytes here including the terminating NUL, so the result is truncated. In addition, the newline at the end is pointless. Fix the buffer size and the format string.

Applied to -trivial, thanks!

/mjt