qemu with hax to log dma reads & writes jcs.org/2018/11/12/vfio

i386: Fix GCC warning with snprintf when HAX is enabled

When HAX is enabled (--enable-hax), GCC 9.2.1 reports issues with
snprintf(). Replacing old snprintf() by g_strdup_printf() fixes the
problem with boundary checks of vm_id and vcpu_id and finally the
warnings produced by GCC.

For more details, one example of warning:
CC i386-softmmu/target/i386/hax-posix.o
qemu/target/i386/hax-posix.c: In function ‘hax_host_open_vm’:
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=]
124 | snprintf(name, sizeof HAX_VM_DEVFS, "/dev/hax_vm/vm%02d", vm_id);
| ^~~~
qemu/target/i386/hax-posix.c:124:41: note: directive argument in the range
[-2147483648, 64]
124 | snprintf(name, sizeof HAX_VM_DEVFS, "/dev/hax_vm/vm%02d", vm_id);
| ^~~~~~~~~~~~~~~~~~~~
In file included from /usr/include/stdio.h:867,
from qemu/include/qemu/osdep.h:99,
from 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
67 | return __builtin___snprintf_chk (__s, __n, __USE_FORTIFY_LEVEL - 1,
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
68 | __bos (__s), __fmt, __va_arg_pack ());
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Signed-off-by: Julio Faracco <jcfaracco@gmail.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>

authored by

Julio Faracco and committed by
Paolo Bonzini
acb9f95a 64a7b8de

+4 -62
+2 -31
target/i386/hax-posix.c
··· 108 108 109 109 static char *hax_vm_devfs_string(int vm_id) 110 110 { 111 - char *name; 112 - 113 - if (vm_id > MAX_VM_ID) { 114 - fprintf(stderr, "Too big VM id\n"); 115 - return NULL; 116 - } 117 - 118 - #define HAX_VM_DEVFS "/dev/hax_vm/vmxx" 119 - name = g_strdup(HAX_VM_DEVFS); 120 - if (!name) { 121 - return NULL; 122 - } 123 - 124 - snprintf(name, sizeof HAX_VM_DEVFS, "/dev/hax_vm/vm%02d", vm_id); 125 - return name; 111 + return g_strdup_printf("/dev/hax_vm/vm%02d", vm_id); 126 112 } 127 113 128 114 static char *hax_vcpu_devfs_string(int vm_id, int vcpu_id) 129 115 { 130 - char *name; 131 - 132 - if (vm_id > MAX_VM_ID || vcpu_id > MAX_VCPU_ID) { 133 - fprintf(stderr, "Too big vm id %x or vcpu id %x\n", vm_id, vcpu_id); 134 - return NULL; 135 - } 136 - 137 - #define HAX_VCPU_DEVFS "/dev/hax_vmxx/vcpuxx" 138 - name = g_strdup(HAX_VCPU_DEVFS); 139 - if (!name) { 140 - return NULL; 141 - } 142 - 143 - snprintf(name, sizeof HAX_VCPU_DEVFS, "/dev/hax_vm%02d/vcpu%02d", 144 - vm_id, vcpu_id); 145 - return name; 116 + return g_strdup_printf("/dev/hax_vm%02d/vcpu%02d", vm_id, vcpu_id); 146 117 } 147 118 148 119 int hax_host_create_vm(struct hax_state *hax, int *vmid)
+2 -31
target/i386/hax-windows.c
··· 185 185 186 186 static char *hax_vm_devfs_string(int vm_id) 187 187 { 188 - char *name; 189 - 190 - if (vm_id > MAX_VM_ID) { 191 - fprintf(stderr, "Too big VM id\n"); 192 - return NULL; 193 - } 194 - 195 - #define HAX_VM_DEVFS "\\\\.\\hax_vmxx" 196 - name = g_strdup(HAX_VM_DEVFS); 197 - if (!name) { 198 - return NULL; 199 - } 200 - 201 - snprintf(name, sizeof HAX_VM_DEVFS, "\\\\.\\hax_vm%02d", vm_id); 202 - return name; 188 + return g_strdup_printf("/dev/hax_vm/vm%02d", vm_id); 203 189 } 204 190 205 191 static char *hax_vcpu_devfs_string(int vm_id, int vcpu_id) 206 192 { 207 - char *name; 208 - 209 - if (vm_id > MAX_VM_ID || vcpu_id > MAX_VCPU_ID) { 210 - fprintf(stderr, "Too big vm id %x or vcpu id %x\n", vm_id, vcpu_id); 211 - return NULL; 212 - } 213 - 214 - #define HAX_VCPU_DEVFS "\\\\.\\hax_vmxx_vcpuxx" 215 - name = g_strdup(HAX_VCPU_DEVFS); 216 - if (!name) { 217 - return NULL; 218 - } 219 - 220 - snprintf(name, sizeof HAX_VCPU_DEVFS, "\\\\.\\hax_vm%02d_vcpu%02d", 221 - vm_id, vcpu_id); 222 - return name; 193 + return g_strdup_printf("/dev/hax_vm%02d/vcpu%02d", vm_id, vcpu_id); 223 194 } 224 195 225 196 int hax_host_create_vm(struct hax_state *hax, int *vmid)