qga: Fix ubsan warning
PATCH 1/1 qga: Fix ubsan warning
From: Thomas Huth <[email protected]>
When compiling QEMU with --enable-ubsan there is a undefined behavior warning when running "make check":
.../qga/commands-linux.c:452:15: runtime error: applying non-zero offset 5 to null pointer #0 0x55ea7b89450c in build_guest_fsinfo_for_pci_dev ..../qga/commands-linux.c:452:15
Add a check to avoid incrementing the NULL pointer here.
--- qga/commands-linux.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)
qga/commands-linux.c +3 -1
@@ -449,7 +449,9 @@ static bool build_guest_fsinfo_for_pci_dev(char const *syspath, has_ata = true; }else { p =strstr (syspath,"/host" ); - q = p +5 ; +if (p) { + q = p +5 ; + } }if (p &&sscanf (q,"%u" , &host) ==1 ) { has_host = true;
Signature · 1 line
2.50.1
Quoted 20 lines · Thomas Huth
From: Thomas Huth <[email protected]>
When compiling QEMU with --enable-ubsan there is a undefined behavior warning when running "make check":
.../qga/commands-linux.c:452:15: runtime error: applying non-zero offset 5 to null pointer #00x55ea7b89450cinbuild_guest_fsinfo_for_pci_dev..../qga/commands-linux.c:452:15Add a check to avoid incrementing the NULL pointer here.
Signed-off-by Thomas Huth <[email protected]>--- qga/commands-linux.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)qga/commands-linux.c +0
-0
qga/commands-linux.c static bool build_guest_fsinfo_for_pci_dev(char const *syspath, · line 456
Quoted 9 lines
qga/commands-linux.c +3
-1@@ -449,7 +449,9 @@ static bool build_guest_fsinfo_for_pci_dev(char const *syspath, has_ata = true; }else { p =strstr (syspath,"/host" ); - q = p +5 ; +if (p) { + q = p +5 ; + } }if (p &&sscanf (q,"%u" , &host) ==1 ) {
q is always non-NULL if p is non-NULL, so this is safe, but I would be more happy with this changing to 'q && sscanf' to eliminate the indirection.
With regards,
Daniel
Signature · 3 lines
|: https://berrange.com -o- https://www.flickr.com/photos/dberrange :| |: https://libvirt.org -o- https://fstop138.berrange.com :| |: https://entangle-photo.org -o- https://www.instagram.com/dberrange :|
Quoted 25 lines · Daniel P. Berrangé
Quoted 24 lines · Thomas Huth
From: Thomas Huth <[email protected]>
When compiling QEMU with --enable-ubsan there is a undefined behavior warning when running "make check":
.../qga/commands-linux.c:452:15: runtime error: applying non-zero offset 5 to null pointer #00x55ea7b89450cinbuild_guest_fsinfo_for_pci_dev..../qga/commands-linux.c:452:15Add a check to avoid incrementing the NULL pointer here.
Signed-off-by Thomas Huth <[email protected]>--- qga/commands-linux.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)qga/commands-linux.c +0
-1@@ -449,7 +449,9 @@ static bool build_guest_fsinfo_for_pci_dev(char const *syspath, has_ata = true; }else { p =strstr (syspath,"/host" ); - q = p +5 ;
Quoted 8 lines
qga/commands-linux.c static bool build_guest_fsinfo_for_pci_dev(char const *syspath, · line 456
Quoted 5 lines
qga/commands-linux.c +3
-0+if (p) { + q = p +5 ; + } }if (p &&sscanf (q,"%u" , &host) ==1 ) {q is always non-NULL if p is non-NULL, so this is safe, but I would be more happy with this changing to 'q && sscanf' to eliminate the indirection.
If we agree to do a bigger change here, I'd rather drop the "q" pointer completely and use a new integer variable instead, something like:
int offset;
...
p = strstr(syspath, "/ata");
if (p) {
offset = 4;
has_ata = true;
} else {
offset = 5;
p = strstr(syspath, "/host");
}
if (p && sscanf(p + offset, "%u", &host) == 1) {
...
}WDYT?
Thomas
Quoted 52 lines · Thomas Huth
Quoted 25 lines · Daniel P. Berrangé
Quoted 24 lines · Thomas Huth
From: Thomas Huth <[email protected]>
When compiling QEMU with --enable-ubsan there is a undefined behavior warning when running "make check":
.../qga/commands-linux.c:452:15: runtime error: applying non-zero offset 5 to null pointer #00x55ea7b89450cinbuild_guest_fsinfo_for_pci_dev..../qga/commands-linux.c:452:15Add a check to avoid incrementing the NULL pointer here.
Signed-off-by Thomas Huth <[email protected]>--- qga/commands-linux.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)qga/commands-linux.c +0
-1@@ -449,7 +449,9 @@ static bool build_guest_fsinfo_for_pci_dev(char const *syspath, has_ata = true; }else { p =strstr (syspath,"/host" ); - q = p +5 ;Quoted 8 lines
qga/commands-linux.c static bool build_guest_fsinfo_for_pci_dev(char const *syspath, · line 456
Quoted 5 lines
qga/commands-linux.c +3
-0+if (p) { + q = p +5 ; + } }if (p &&sscanf (q,"%u" , &host) ==1 ) {q is always non-NULL if p is non-NULL, so this is safe, but I would be more happy with this changing to 'q && sscanf' to eliminate the indirection.
If we agree to do a bigger change here, I'd rather drop the "q" pointer completely and use a new integer variable instead, something like:
int offset; ... p = strstr(syspath, "/ata"); if (p) { offset = 4; has_ata = true; } else { offset = 5; p = strstr(syspath, "/host"); } if (p && sscanf(p + offset, "%u", &host) == 1) { ... }
Quoted 1 line
WDYT?
Works for me.
With regards,
Daniel
Signature · 3 lines
|: https://berrange.com -o- https://www.flickr.com/photos/dberrange :| |: https://libvirt.org -o- https://fstop138.berrange.com :| |: https://entangle-photo.org -o- https://www.instagram.com/dberrange :|
LGTM
Quoted 61 lines · Thomas Huth
Quoted 7 lines · Daniel P. Berrangé
Quoted 6 lines · Thomas Huth
From: Thomas Huth <[email protected]>
When compiling QEMU with --enable-ubsan there is a undefined behavior warning when running "make check":
.../qga/commands-linux.c:452:15: runtime error: applying non-zerooffset 5 to null pointer
Quoted 1 line
Quoted 1 line
#0
0x55ea7b89450cinbuild_guest_fsinfo_for_pci_dev
..../qga/commands-linux.c:452:15Quoted 8 lines
Quoted 8 lines
Add a check to avoid incrementing the NULL pointer here.
Signed-off-by Thomas Huth <[email protected]>--- qga/commands-linux.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)Quoted 5 lines
qga/commands-linux.c static bool build_guest_fsinfo_for_pci_dev(char
Quoted 5 lines
qga/commands-linux.c +0
-0@@ -449,7 +449,9 @@ static bool build_guest_fsinfo_for_pci_dev(charconst *syspath,
Quoted 11 lines
qga/commands-linux.c static bool build_guest_fsinfo_for_pci_dev(char const *syspath, · line 456
Quoted 9 lines
qga/commands-linux.c +3
-1has_ata = true; }else { p =strstr (syspath,"/host" ); - q = p +5 ; +if (p) { + q = p +5 ; + } }if (p &&sscanf (q,"%u" , &host) ==1 ) {q is always non-NULL if p is non-NULL, so this is safe, but I would be
more
Quoted 1 line
happy with this changing to 'q && sscanf' to eliminate the indirection.
If we agree to do a bigger change here, I'd rather drop the "q" pointer completely and use a new integer variable instead, something like:
int offset; ... p = strstr(syspath, "/ata"); if (p) { offset = 4; has_ata = true; } else { offset = 5; p = strstr(syspath, "/host"); } if (p && sscanf(p + offset, "%u", &host) == 1) { ... }WDYT?
Thomas