Skip to contentSkip to conversation

target/i386/hax-posix: fix two 'format-truncation' compile warnings

PATCH In review [email protected] 3 participants 4 messages Feb 24, 2020 – Mar 3, 2020

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 target/i386/hax-posix: fix two 'format-truncation' compile warnings

From: Pan Nengyuan <[email protected]>

Fix compile warnings:
/mnt/sdb/qemu-new/qemu_test/qemu/target/i386/hax-posix.c:124:56: error: ‘%02d’ directive output may be truncated writing between 2 and 11 bytes into a region of size 3 [-Werror=format-truncation=]

     snprintf(name, sizeof HAX_VM_DEVFS, "/dev/hax_vm/vm%02d", vm_id);
                                                        ^~~~
/mnt/sdb/qemu-new/qemu_test/qemu/target/i386/hax-posix.c:124:41: note: directive argument in the range [-2147483648, 64]
     snprintf(name, sizeof HAX_VM_DEVFS, "/dev/hax_vm/vm%02d", vm_id);
                                         ^~~~~~~~~~~~~~~~~~~~
In file included from /usr/include/stdio.h:873,
                 from /mnt/sdb/qemu-new/qemu_test/qemu/include/qemu/osdep.h:99,
                 from /mnt/sdb/qemu-new/qemu_test/qemu/target/i386/hax-posix.c:14:
/usr/include/bits/stdio2.h:67:10: note: ‘__builtin___snprintf_chk’ output between 17 and 26 bytes into a destination of size 17
   return __builtin___snprintf_chk (__s, __n, __USE_FORTIFY_LEVEL - 1,
          ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        __bos (__s), __fmt, __va_arg_pack ());
        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/mnt/sdb/qemu-new/qemu_test/qemu/target/i386/hax-posix.c: In function ‘hax_vcpu_devfs_string’:
/mnt/sdb/qemu-new/qemu_test/qemu/target/i386/hax-posix.c:143:55: error: ‘%02d’ directive output may be truncated writing between 2 and 11 bytes into a region of size 10 [-Werror=format-truncation=]
     snprintf(name, sizeof HAX_VCPU_DEVFS, "/dev/hax_vm%02d/vcpu%02d",
                                                       ^~~~
/mnt/sdb/qemu-new/qemu_test/qemu/target/i386/hax-posix.c:143:43: note: directive argument in the range [-2147483648, 64]
     snprintf(name, sizeof HAX_VCPU_DEVFS, "/dev/hax_vm%02d/vcpu%02d",
                                           ^~~~~~~~~~~~~~~~~~~~~~~~~~
/mnt/sdb/qemu-new/qemu_test/qemu/target/i386/hax-posix.c:143:43: note: directive argument in the range [-2147483648, 64]
In file included from /usr/include/stdio.h:873,
                 from /mnt/sdb/qemu-new/qemu_test/qemu/include/qemu/osdep.h:99,
                 from /mnt/sdb/qemu-new/qemu_test/qemu/target/i386/hax-posix.c:14:
/usr/include/bits/stdio2.h:67:10: note: ‘__builtin___snprintf_chk’ output between 21 and 39 bytes into a destination of size 21
   return __builtin___snprintf_chk (__s, __n, __USE_FORTIFY_LEVEL - 1,
          ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        __bos (__s), __fmt, __va_arg_pack ());

We know that we have checked the vm_id and vcpu_id in the first(less than 0x40), it will never be truncated in snprintf(). Thus, this patch add an assertion to clear this false-positive warning.

---
 target/i386/hax-posix.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
target/i386/hax-posix.c +5 -3
index a5426a6dac..197d5bc0f9 100644
@@ -121,7 +121,8 @@ static char *hax_vm_devfs_string(int vm_id)
         return NULL;
     }
 
-    snprintf(name, sizeof HAX_VM_DEVFS, "/dev/hax_vm/vm%02d", vm_id);
+    int len = snprintf(name, sizeof HAX_VM_DEVFS, "/dev/hax_vm/vm%02d", vm_id);
+    assert(len < sizeof HAX_VM_DEVFS);
     return name;
 }
 
@@ -140,8 +141,9 @@ static char *hax_vcpu_devfs_string(int vm_id, int vcpu_id)
         return NULL;
     }
 
-    snprintf(name, sizeof HAX_VCPU_DEVFS, "/dev/hax_vm%02d/vcpu%02d",
-             vm_id, vcpu_id);
+    int len = snprintf(name, sizeof HAX_VCPU_DEVFS, "/dev/hax_vm%02d/vcpu%02d",
+                       vm_id, vcpu_id);
+    assert(len < sizeof HAX_VCPU_DEVFS);
     return name;
 }
 
Signature · 1 line
2.18.2
#1 lore raw

Le 24/02/2020 à 07:51, [email protected] a écrit :

Quoted 59 lines

From: Pan Nengyuan <[email protected]>

Fix compile warnings:
/mnt/sdb/qemu-new/qemu_test/qemu/target/i386/hax-posix.c:124:56: error: ‘%02d’ directive output may be truncated writing between 2 and 11 bytes into a region of size 3 [-Werror=format-truncation=]

     snprintf(name, sizeof HAX_VM_DEVFS, "/dev/hax_vm/vm%02d", vm_id);
                                                        ^~~~
/mnt/sdb/qemu-new/qemu_test/qemu/target/i386/hax-posix.c:124:41: note: directive argument in the range [-2147483648, 64]
     snprintf(name, sizeof HAX_VM_DEVFS, "/dev/hax_vm/vm%02d", vm_id);
                                         ^~~~~~~~~~~~~~~~~~~~
In file included from /usr/include/stdio.h:873,
                 from /mnt/sdb/qemu-new/qemu_test/qemu/include/qemu/osdep.h:99,
                 from /mnt/sdb/qemu-new/qemu_test/qemu/target/i386/hax-posix.c:14:
/usr/include/bits/stdio2.h:67:10: note: ‘__builtin___snprintf_chk’ output between 17 and 26 bytes into a destination of size 17
   return __builtin___snprintf_chk (__s, __n, __USE_FORTIFY_LEVEL - 1,
          ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        __bos (__s), __fmt, __va_arg_pack ());
        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/mnt/sdb/qemu-new/qemu_test/qemu/target/i386/hax-posix.c: In function ‘hax_vcpu_devfs_string’:
/mnt/sdb/qemu-new/qemu_test/qemu/target/i386/hax-posix.c:143:55: error: ‘%02d’ directive output may be truncated writing between 2 and 11 bytes into a region of size 10 [-Werror=format-truncation=]
     snprintf(name, sizeof HAX_VCPU_DEVFS, "/dev/hax_vm%02d/vcpu%02d",
                                                       ^~~~
/mnt/sdb/qemu-new/qemu_test/qemu/target/i386/hax-posix.c:143:43: note: directive argument in the range [-2147483648, 64]
     snprintf(name, sizeof HAX_VCPU_DEVFS, "/dev/hax_vm%02d/vcpu%02d",
                                           ^~~~~~~~~~~~~~~~~~~~~~~~~~
/mnt/sdb/qemu-new/qemu_test/qemu/target/i386/hax-posix.c:143:43: note: directive argument in the range [-2147483648, 64]
In file included from /usr/include/stdio.h:873,
                 from /mnt/sdb/qemu-new/qemu_test/qemu/include/qemu/osdep.h:99,
                 from /mnt/sdb/qemu-new/qemu_test/qemu/target/i386/hax-posix.c:14:
/usr/include/bits/stdio2.h:67:10: note: ‘__builtin___snprintf_chk’ output between 21 and 39 bytes into a destination of size 21
   return __builtin___snprintf_chk (__s, __n, __USE_FORTIFY_LEVEL - 1,
          ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        __bos (__s), __fmt, __va_arg_pack ());

We know that we have checked the vm_id and vcpu_id in the first(less than 0x40), it will never be truncated in snprintf(). Thus, this patch add an assertion to clear this false-positive warning.

---
 target/i386/hax-posix.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
target/i386/hax-posix.c +2 -1
index a5426a6dac..197d5bc0f9 100644
@@ -121,7 +121,8 @@ static char *hax_vm_devfs_string(int vm_id)
         return NULL;
     }
 
-    snprintf(name, sizeof HAX_VM_DEVFS, "/dev/hax_vm/vm%02d", vm_id);
+    int len = snprintf(name, sizeof HAX_VM_DEVFS, "/dev/hax_vm/vm%02d", vm_id);
+    assert(len < sizeof HAX_VM_DEVFS);
     return name;
 }
 
@@ -140,8 +141,9 @@ static char *hax_vcpu_devfs_string(int vm_id, int vcpu_id)
         return NULL;

target/i386/hax-posix.c static char *hax_vcpu_devfs_string(int vm_id, int vcpu_id) · line 149

Quoted 10 lines
target/i386/hax-posix.c +3 -2
     }
 
-    snprintf(name, sizeof HAX_VCPU_DEVFS, "/dev/hax_vm%02d/vcpu%02d",
-             vm_id, vcpu_id);
+    int len = snprintf(name, sizeof HAX_VCPU_DEVFS, "/dev/hax_vm%02d/vcpu%02d",
+                       vm_id, vcpu_id);
+    assert(len < sizeof HAX_VCPU_DEVFS);
     return name;
 }
 

You should check instead that vm_id and vcpu_id are >= 0 where they are checked to be <= MAX_VM_ID and MAX_VCPU_ID, this will avoid the overflow of "%02d" and should remove the compile warning.

Thanks,
Laurent

#2 lore raw
In reply to [email protected]
Quoted 17 lines · On 24/02/20 07:51, [email protected] wrote:
target/i386/hax-posix.c +2 -1
index a5426a6dac..197d5bc0f9 100644
@@ -121,7 +121,8 @@ static char *hax_vm_devfs_string(int vm_id)
         return NULL;
     }
 
-    snprintf(name, sizeof HAX_VM_DEVFS, "/dev/hax_vm/vm%02d", vm_id);
+    int len = snprintf(name, sizeof HAX_VM_DEVFS, "/dev/hax_vm/vm%02d", vm_id);
+    assert(len < sizeof HAX_VM_DEVFS);
     return name;
 }
 
@@ -140,8 +141,9 @@ static char *hax_vcpu_devfs_string(int vm_id, int vcpu_id)
         return NULL;

target/i386/hax-posix.c static char *hax_vcpu_devfs_string(int vm_id, int vcpu_id) · line 149

Quoted 10 lines
target/i386/hax-posix.c +3 -2
     }
 
-    snprintf(name, sizeof HAX_VCPU_DEVFS, "/dev/hax_vm%02d/vcpu%02d",
-             vm_id, vcpu_id);
+    int len = snprintf(name, sizeof HAX_VCPU_DEVFS, "/dev/hax_vm%02d/vcpu%02d",
+                       vm_id, vcpu_id);
+    assert(len < sizeof HAX_VCPU_DEVFS);
     return name;
 }
 

Julio Faracco has posted a fix for the same bug. The best change is actually to switch to g_strdup_printf.

Paolo

#3 lore raw
Quoted 19 lines · Paolo Bonzini
Quoted 18 lines · On 24/02/20 07:51, [email protected] wrote:
target/i386/hax-posix.c +2 -1
index a5426a6dac..197d5bc0f9 100644
@@ -121,7 +121,8 @@ static char *hax_vm_devfs_string(int vm_id)
         return NULL;
     }
 
-    snprintf(name, sizeof HAX_VM_DEVFS, "/dev/hax_vm/vm%02d", vm_id);
+    int len = snprintf(name, sizeof HAX_VM_DEVFS, "/dev/hax_vm/vm%02d", vm_id);
+    assert(len < sizeof HAX_VM_DEVFS);
     return name;
 }
 
@@ -140,8 +141,9 @@ static char *hax_vcpu_devfs_string(int vm_id, int vcpu_id)
         return NULL;
     }
 
Quoted 12 lines

target/i386/hax-posix.c static char *hax_vcpu_devfs_string(int vm_id, int vcpu_id) · line 149

Quoted 9 lines
target/i386/hax-posix.c +3 -2
-    snprintf(name, sizeof HAX_VCPU_DEVFS, "/dev/hax_vm%02d/vcpu%02d",
-             vm_id, vcpu_id);
+    int len = snprintf(name, sizeof HAX_VCPU_DEVFS, "/dev/hax_vm%02d/vcpu%02d",
+                       vm_id, vcpu_id);
+    assert(len < sizeof HAX_VCPU_DEVFS);
     return name;
 }
 

Julio Faracco has posted a fix for the same bug. The best change is actually to switch to g_strdup_printf.

Okay, Thanks.

Quoted 3 lines

Paolo