* [PATCH v2] i386: Fix GCC warning with snprintf when HAX is enabled
@ 2020-03-01 19:17 Julio Faracco
2020-03-02 4:15 ` Richard Henderson
2020-03-02 4:16 ` Richard Henderson
0 siblings, 2 replies; 3+ messages in thread
From: Julio Faracco @ 2020-03-01 19:17 UTC (permalink / raw)
To: qemu-devel; +Cc: Paolo Bonzini, Eduardo Habkost, Richard Henderson
When HAX is enabled (--enable-hax), GCC 9.2.1 reports issues with
snprintf(). This commit is checking if snprintf returns an error. This
is a simple way to avoid this warnings. An `assert()` boundary checks
were added before snprintf too.
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>
---
v1-v2: Add assert() as Richard Henderson suggested.
---
target/i386/hax-posix.c | 15 ++++++++++++---
1 file changed, 12 insertions(+), 3 deletions(-)
diff --git a/target/i386/hax-posix.c b/target/i386/hax-posix.c
index a5426a6dac..dc777751d7 100644
--- a/target/i386/hax-posix.c
+++ b/target/i386/hax-posix.c
@@ -121,7 +121,12 @@ static char *hax_vm_devfs_string(int vm_id)
return NULL;
}
- snprintf(name, sizeof HAX_VM_DEVFS, "/dev/hax_vm/vm%02d", vm_id);
+ assert(vm_id < 0);
+
+ if (snprintf(name, sizeof HAX_VM_DEVFS, "/dev/hax_vm/vm%02d",
+ vm_id) < 0)
+ return NULL;
+
return name;
}
@@ -140,8 +145,12 @@ 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);
+ assert(vm_id < 0 || vcpu_id < 0);
+
+ if (snprintf(name, sizeof HAX_VCPU_DEVFS, "/dev/hax_vm%02d/vcpu%02d",
+ vm_id, vcpu_id) < 0)
+ return NULL;
+
return name;
}
--
2.24.1
^ permalink raw reply related [flat|nested] 3+ messages in thread
* Re: [PATCH v2] i386: Fix GCC warning with snprintf when HAX is enabled
2020-03-01 19:17 [PATCH v2] i386: Fix GCC warning with snprintf when HAX is enabled Julio Faracco
@ 2020-03-02 4:15 ` Richard Henderson
2020-03-02 4:16 ` Richard Henderson
1 sibling, 0 replies; 3+ messages in thread
From: Richard Henderson @ 2020-03-02 4:15 UTC (permalink / raw)
To: Julio Faracco, qemu-devel
Cc: Paolo Bonzini, Eduardo Habkost, Richard Henderson
On 3/1/20 11:17 AM, Julio Faracco wrote:
> - snprintf(name, sizeof HAX_VM_DEVFS, "/dev/hax_vm/vm%02d", vm_id);
> + assert(vm_id < 0);
> +
> + if (snprintf(name, sizeof HAX_VM_DEVFS, "/dev/hax_vm/vm%02d",
> + vm_id) < 0)
> + return NULL;
> +
No, you don't need the IF around the snprinf.
r~
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [PATCH v2] i386: Fix GCC warning with snprintf when HAX is enabled
2020-03-01 19:17 [PATCH v2] i386: Fix GCC warning with snprintf when HAX is enabled Julio Faracco
2020-03-02 4:15 ` Richard Henderson
@ 2020-03-02 4:16 ` Richard Henderson
1 sibling, 0 replies; 3+ messages in thread
From: Richard Henderson @ 2020-03-02 4:16 UTC (permalink / raw)
To: Julio Faracco, qemu-devel
Cc: Paolo Bonzini, Eduardo Habkost, Richard Henderson
On 3/1/20 11:17 AM, Julio Faracco wrote:
> - snprintf(name, sizeof HAX_VM_DEVFS, "/dev/hax_vm/vm%02d", vm_id);
> + assert(vm_id < 0);
> +
> + if (snprintf(name, sizeof HAX_VM_DEVFS, "/dev/hax_vm/vm%02d",
> + vm_id) < 0)
> + return NULL;
> +
And the assert is opposite. You didn't even smoke test.
r~
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2020-03-02 4:17 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2020-03-01 19:17 [PATCH v2] i386: Fix GCC warning with snprintf when HAX is enabled Julio Faracco
2020-03-02 4:15 ` Richard Henderson
2020-03-02 4:16 ` Richard Henderson
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).