Skip to contentSkip to conversation

block/vvfat: Fix compiler warning with gcc 7

PATCH Applied Max Reitz 4 participants 5 messages Jul 17, 2017 Block layer

lore
Share & tools
lore

Max Reitz

author
#0 lore raw

PATCH 1/1 block/vvfat: Fix compiler warning with gcc 7

gcc 7 complains that the sprintf() might write a null byte beyond the end of the tail buffer. That is wrong, but we can silence it by making i unsigned (it can never be negative anyway, see the if condition right before). For some reason, this allows gcc to suddenly accurately calculate the range of i so we can give the tail[] array the exact size it needs to have (which is 8 bytes) without gcc complaining.

In addition, let us convert the sprintf() to snprintf(), because that is always nicer, and add an assertion about the range of the return value afterwards so we can see that "8 - len" will never be negative and thus "entry->name + MIN(j, 8 - len)" will never be out of bounds.

---
 block/vvfat.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
block/vvfat.c +3 -2
index 6b11596..a9e207f 100644
@@ -549,7 +549,7 @@ static direntry_t *create_short_filename(BDRVVVFATState *s,
     const gchar *p, *last_dot = NULL;
     gunichar c;
     bool lossy_conversion = false;
-    char tail[11];
+    char tail[8];
 
     if (!entry) {
         return NULL;
@@ -614,7 +614,8 @@ static direntry_t *create_short_filename(BDRVVVFATState *s,
     for (i = lossy_conversion ? 1 : 0; i < 999999; i++) {
         direntry_t *entry1;
         if (i > 0) {
-            int len = sprintf(tail, "~%d", i);
+            int len = snprintf(tail, sizeof(tail), "~%u", (unsigned)i);
+            assert(len <= 7);
             memcpy(entry->name + MIN(j, 8 - len), tail, len);
         }
         for (entry1 = array_get(&(s->directory), directory_start);
Signature · 1 line
2.9.4
Reviewed-by on v1 1/1
#1 lore raw
Quoted 12 lines · Max Reitz

gcc 7 complains that the sprintf() might write a null byte beyond the end of the tail buffer. That is wrong, but we can silence it by making i unsigned (it can never be negative anyway, see the if condition right before). For some reason, this allows gcc to suddenly accurately calculate the range of i so we can give the tail[] array the exact size it needs to have (which is 8 bytes) without gcc complaining.

In addition, let us convert the sprintf() to snprintf(), because that is always nicer, and add an assertion about the range of the return value afterwards so we can see that "8 - len" will never be negative and thus "entry->name + MIN(j, 8 - len)" will never be out of bounds.

Quoted 4 lines
---
 block/vvfat.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
Signature · 3 lines
Eric Blake, Principal Software Engineer
Red Hat, Inc.           +1-919-301-3266
Virtualization:  qemu.org | libvirt.org
Applied
#2 lore raw
In reply to Max Reitz

Am 17.07.2017 um 17:12 hat Max Reitz geschrieben:

Quoted 7 lines

gcc 7 complains that the sprintf() might write a null byte beyond the end of the tail buffer. That is wrong, but we can silence it by making i unsigned (it can never be negative anyway, see the if condition right before). For some reason, this allows gcc to suddenly accurately calculate the range of i so we can give the tail[] array the exact size it needs to have (which is 8 bytes) without gcc complaining.

Quoted 6 lines

In addition, let us convert the sprintf() to snprintf(), because that is always nicer, and add an assertion about the range of the return value afterwards so we can see that "8 - len" will never be negative and thus "entry->name + MIN(j, 8 - len)" will never be out of bounds.

Thanks, applied to the block branch.

Kevin

#3 lore raw
In reply to Max Reitz
Quoted 31 lines · Max Reitz

gcc 7 complains that the sprintf() might write a null byte beyond the end of the tail buffer. That is wrong, but we can silence it by making i unsigned (it can never be negative anyway, see the if condition right before). For some reason, this allows gcc to suddenly accurately calculate the range of i so we can give the tail[] array the exact size it needs to have (which is 8 bytes) without gcc complaining.

In addition, let us convert the sprintf() to snprintf(), because that is always nicer, and add an assertion about the range of the return value afterwards so we can see that "8 - len" will never be negative and thus "entry->name + MIN(j, 8 - len)" will never be out of bounds.

---
 block/vvfat.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
block/vvfat.c +1 -1
index 6b11596..a9e207f 100644
@@ -549,7 +549,7 @@ static direntry_t *create_short_filename(BDRVVVFATState *s,
     const gchar *p, *last_dot = NULL;
     gunichar c;
     bool lossy_conversion = false;
-    char tail[11];
+    char tail[8];
 
     if (!entry) {
         return NULL;

block/vvfat.c static direntry_t *create_short_filename(BDRVVVFATState *s, · line 618

Quoted 6 lines
block/vvfat.c +2 -1
@@ -614,7 +614,8 @@ static direntry_t *create_short_filename(BDRVVVFATState *s,
     for (i = lossy_conversion ? 1 : 0; i < 999999; i++) {
         direntry_t *entry1;
         if (i > 0) {
-            int len = sprintf(tail, "~%d", i);
+            int len = snprintf(tail, sizeof(tail), "~%u", (unsigned)i);
+            assert(len <= 7);

As i is on minimum between 0 or 1 and on maximum equal at 999999, does it work if you change the type of i from int to unsigned int? That way, you probably won't need the cast to unsigned in the s(n)printf.

Quoted 4 lines
block/vvfat.c +0 -0
             memcpy(entry->name + MIN(j, 8 - len), tail, len);
         }
         for (entry1 = array_get(&(s->directory), directory_start);
author
#4 lore raw
Quoted 30 lines · Hervé Poussineau
Quoted 29 lines · Max Reitz

gcc 7 complains that the sprintf() might write a null byte beyond the end of the tail buffer. That is wrong, but we can silence it by making i unsigned (it can never be negative anyway, see the if condition right before). For some reason, this allows gcc to suddenly accurately calculate the range of i so we can give the tail[] array the exact size it needs to have (which is 8 bytes) without gcc complaining.

In addition, let us convert the sprintf() to snprintf(), because that is always nicer, and add an assertion about the range of the return value afterwards so we can see that "8 - len" will never be negative and thus "entry->name + MIN(j, 8 - len)" will never be out of bounds.

---
 block/vvfat.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
block/vvfat.c +1 -1
index 6b11596..a9e207f 100644
@@ -549,7 +549,7 @@ static direntry_t
 *create_short_filename(BDRVVVFATState *s,
     const gchar *p, *last_dot = NULL;
     gunichar c;
     bool lossy_conversion = false;
-    char tail[11];
+    char tail[8];
 
Quoted 14 lines

block/vvfat.c static direntry_t · line 619

Quoted 10 lines
block/vvfat.c +0 -0
     if (!entry) {
         return NULL;
block/vvfat.c +2 -1
@@ -614,7 +614,8 @@ static direntry_t
 *create_short_filename(BDRVVVFATState *s,
     for (i = lossy_conversion ? 1 : 0; i < 999999; i++) {
         direntry_t *entry1;
         if (i > 0) {
-            int len = sprintf(tail, "~%d", i);
+            int len = snprintf(tail, sizeof(tail), "~%u", (unsigned)i);
+            assert(len <= 7);

As i is on minimum between 0 or 1 and on maximum equal at 999999, does it work if you change the type of i from int to unsigned int? That way, you probably won't need the cast to unsigned in the s(n)printf.

Hm... It works in a way, but then gcc likes to think tail[] needs to be 9 bytes long (for whatever reason). So... It works in a sense, but not quite as well.

So I'm not quite sure which way is better. :-)

Max