]> xenbits.xensource.com Git - people/andrewcoop/hwloc.git/commitdiff
linux: check the return value of fread() for sysfs files
authorBrice Goglin <Brice.Goglin@inria.fr>
Fri, 14 Feb 2014 08:15:24 +0000 (09:15 +0100)
committerBrice Goglin <Brice.Goglin@inria.fr>
Fri, 14 Feb 2014 08:17:02 +0000 (09:17 +0100)
There's very few chances the kernel will report garbage here.
Just don't strtoul unless we read at least something. At worse, we'll get 0.

This fixes warning when the libc declares fread() as __attribute_warn_unused_result__

src/topology-linux.c
src/topology-pci.c

index 8d18d1dd39b274eb22bbc2fbc00f3b099f9972be..fd7dd30fc266be9e8bb081d6ebff4cb8b11cb70c 100644 (file)
@@ -4679,6 +4679,7 @@ hwloc_look_linuxfs_pci(struct hwloc_backend *backend)
     unsigned os_index;
     char path[64];
     char value[16];
+    size_t read;
     FILE *file;
 
     if (sscanf(dirent->d_name, "%04x:%02x:%02x.%01x", &domain, &bus, &dev, &func) != 4)
@@ -4707,37 +4708,42 @@ hwloc_look_linuxfs_pci(struct hwloc_backend *backend)
     snprintf(path, sizeof(path), "/sys/bus/pci/devices/%s/vendor", dirent->d_name);
     file = hwloc_fopen(path, "r", root_fd);
     if (file) {
-      fread(value, sizeof(value), 1, file);
+      read = fread(value, sizeof(value), 1, file);
       fclose(file);
-      attr->vendor_id = strtoul(value, NULL, 16);
+      if (read)
+        attr->vendor_id = strtoul(value, NULL, 16);
     }
     snprintf(path, sizeof(path), "/sys/bus/pci/devices/%s/device", dirent->d_name);
     file = hwloc_fopen(path, "r", root_fd);
     if (file) {
-      fread(value, sizeof(value), 1, file);
+      read = fread(value, sizeof(value), 1, file);
       fclose(file);
-      attr->device_id = strtoul(value, NULL, 16);
+      if (read)
+        attr->device_id = strtoul(value, NULL, 16);
     }
     snprintf(path, sizeof(path), "/sys/bus/pci/devices/%s/class", dirent->d_name);
     file = hwloc_fopen(path, "r", root_fd);
     if (file) {
-      fread(value, sizeof(value), 1, file);
+      read = fread(value, sizeof(value), 1, file);
       fclose(file);
-      attr->class_id = strtoul(value, NULL, 16) >> 8;
+      if (read)
+        attr->class_id = strtoul(value, NULL, 16) >> 8;
     }
     snprintf(path, sizeof(path), "/sys/bus/pci/devices/%s/subsystem_vendor", dirent->d_name);
     file = hwloc_fopen(path, "r", root_fd);
     if (file) {
-      fread(value, sizeof(value), 1, file);
+      read = fread(value, sizeof(value), 1, file);
       fclose(file);
-      attr->subvendor_id = strtoul(value, NULL, 16);
+      if (read)
+        attr->subvendor_id = strtoul(value, NULL, 16);
     }
     snprintf(path, sizeof(path), "/sys/bus/pci/devices/%s/subsystem_device", dirent->d_name);
     file = hwloc_fopen(path, "r", root_fd);
     if (file) {
-      fread(value, sizeof(value), 1, file);
+      read = fread(value, sizeof(value), 1, file);
       fclose(file);
-      attr->subdevice_id = strtoul(value, NULL, 16);
+      if (read)
+        attr->subdevice_id = strtoul(value, NULL, 16);
     }
 
     snprintf(path, sizeof(path), "/sys/bus/pci/devices/%s/config", dirent->d_name);
@@ -4749,7 +4755,8 @@ hwloc_look_linuxfs_pci(struct hwloc_backend *backend)
 
       /* initialize the config space in case we fail to read it (missing permissions, etc). */
       memset(config_space_cache, 0xff, CONFIG_SPACE_CACHESIZE);
-      (void) fread(config_space_cache, 1, CONFIG_SPACE_CACHESIZE, file);
+      read = fread(config_space_cache, 1, CONFIG_SPACE_CACHESIZE, file);
+      (void) read; /* we initialized config_space_cache in case we don't read enough, ignore the read length */
       fclose(file);
 
       /* is this a bridge? */
index a185d8635502ba32776b61101a665f7f393a60f8..aa366c762b2cbe74dea290de33462a610be74d4e 100644 (file)
@@ -249,21 +249,26 @@ hwloc_look_pci(struct hwloc_backend *backend)
       char path[64];
       char value[16];
       FILE *file;
+      size_t read;
+
       snprintf(path, sizeof(path), "/sys/bus/pci/devices/%04x:%02x:%02x.%01x/vendor",
               domain, pcidev->bus, pcidev->dev, pcidev->func);
       file = fopen(path, "r");
       if (file) {
-       fread(value, sizeof(value), 1, file);
+       read = fread(value, sizeof(value), 1, file);
        fclose(file);
-       obj->attr->pcidev.vendor_id = strtoul(value, NULL, 16);
+       if (read)
+         obj->attr->pcidev.vendor_id = strtoul(value, NULL, 16);
       }
+
       snprintf(path, sizeof(path), "/sys/bus/pci/devices/%04x:%02x:%02x.%01x/device",
               domain, pcidev->bus, pcidev->dev, pcidev->func);
       file = fopen(path, "r");
       if (file) {
-       fread(value, sizeof(value), 1, file);
+       read = fread(value, sizeof(value), 1, file);
        fclose(file);
-       obj->attr->pcidev.device_id = strtoul(value, NULL, 16);
+       if (read)
+         obj->attr->pcidev.device_id = strtoul(value, NULL, 16);
       }
 #endif
     }