Skip to contentSkip to conversation

qga: Fix ubsan warning

PATCH Superseded Thomas Huth 3 participants 5 messages Jul 28, 2025 – Jul 29, 2025

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

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
index 9e8a934b9a6..caf7c3ca22b 100644
@@ -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
#1 lore raw
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 #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 +0 -0
index 9e8a934b9a6..caf7c3ca22b 100644

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 :|
#2 lore raw
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 #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 +0 -1
index 9e8a934b9a6..caf7c3ca22b 100644
@@ -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

#3 lore raw
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 #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 +0 -1
index 9e8a934b9a6..caf7c3ca22b 100644
@@ -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 :|
#4 lore raw
In reply to Thomas Huth

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-zero

offset 5 to null pointer

Quoted 1 line
Quoted 1 line

#0 0x55ea7b89450c in build_guest_fsinfo_for_pci_dev

..../qga/commands-linux.c:452:15

Quoted 8 lines
Quoted 8 lines

Add a check to avoid incrementing the NULL pointer here.

---
  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
index 9e8a934b9a6..caf7c3ca22b 100644
@@ -449,7 +449,9 @@ static bool build_guest_fsinfo_for_pci_dev(char

const *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 -1
          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

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