]> git.ipfire.org Git - thirdparty/libvirt.git/commitdiff
virPCIDeviceReadID: Fix use of 'virFileReadAll'
authorPeter Krempa <pkrempa@redhat.com>
Fri, 27 Mar 2026 08:37:42 +0000 (09:37 +0100)
committerPeter Krempa <pkrempa@redhat.com>
Fri, 27 Mar 2026 10:44:25 +0000 (11:44 +0100)
Use 'virFileReadAllQuiet' since the function doesn't want to report
errors on other code paths.

The function also assumed that the file which it reads always 7 bytes
isn't true at least in the test suite. This didn't cause a problem
because the test data had strings 6 bytes long so it didn't cause a
write beyond the end of the buffer.

Clear the newline by using strchrnul instead to find it rather than
assuming where it is.

Signed-off-by: Peter Krempa <pkrempa@redhat.com>
Reviewed-by: Pavel Hrdina <phrdina@redhat.com>
src/util/virpci.c

index 2e32ed17ff834c21c412c21e2f6625f5ae57b3a0..d43fa1ef548a3b50b6f212ff2f6979245effb706 100644 (file)
@@ -1760,19 +1760,20 @@ virPCIDeviceReadID(virPCIDevice *dev, const char *id_name)
 {
     g_autofree char *path = NULL;
     g_autofree char *id_str = NULL;
+    int len;
 
     path = virPCIFile(dev->name, id_name);
 
     /* ID string is '0xNNNN\n' ... i.e. 7 bytes */
-    if (virFileReadAll(path, 7, &id_str) < 0)
+    if ((len = virFileReadAllQuiet(path, 7, &id_str)) < 0)
         return NULL;
 
-    /* Check for 0x suffix */
+    /* Check for 0x prefix */
     if (id_str[0] != '0' || id_str[1] != 'x')
         return NULL;
 
-    /* Chop off the newline; we know the string is 7 bytes */
-    id_str[6] = '\0';
+    /* Chop off the newline */
+    virStringTrimOptionalNewline(id_str);
 
     return g_steal_pointer(&id_str);
 }