From: Peter Krempa Date: Fri, 27 Mar 2026 08:37:42 +0000 (+0100) Subject: virPCIDeviceReadID: Fix use of 'virFileReadAll' X-Git-Tag: v12.2.0-rc2~4 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=4fc09da299865327676362dfd44b69127810be9a;p=thirdparty%2Flibvirt.git virPCIDeviceReadID: Fix use of 'virFileReadAll' 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 Reviewed-by: Pavel Hrdina --- diff --git a/src/util/virpci.c b/src/util/virpci.c index 2e32ed17ff..d43fa1ef54 100644 --- a/src/util/virpci.c +++ b/src/util/virpci.c @@ -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); }