loader: Fix compiler warnings about truncating the PCI ID path.
authorEric Anholt <eric@anholt.net>
Sat, 10 Feb 2018 10:45:18 +0000 (10:45 +0000)
committerEric Anholt <eric@anholt.net>
Wed, 21 Feb 2018 04:23:57 +0000 (20:23 -0800)
My build was producing:

../src/loader/loader.c:121:67: warning: ā€˜%1uā€™ directive output may be truncated writing between 1 and 3 bytes into a region of size 2 [-Wformat-truncation=]

and we can avoid this careful calculation by just using asprintf (as we do
elsewhere in the file).

Reviewed-by: Eric Engestrom <eric.engestrom@imgtec.com>
Reviewed-by: Emil Velikov <emil.velikov@collabora.com>
Reviewed-by: Ian Romanick <ian.d.romanick@intel.com>
src/loader/loader.c

index 913b3dcac032b3c9cd0ab068874627913f2b45bc..92b4c5204b1995ca577d99ac7ce13e3f4a4e0496 100644 (file)
@@ -110,17 +110,16 @@ static char *loader_get_dri_config_device_id(void)
 
 static char *drm_construct_id_path_tag(drmDevicePtr device)
 {
-#define PCI_ID_PATH_TAG_LENGTH sizeof("pci-xxxx_xx_xx_x")
    char *tag = NULL;
 
    if (device->bustype == DRM_BUS_PCI) {
-        tag = calloc(PCI_ID_PATH_TAG_LENGTH, sizeof(char));
-        if (tag == NULL)
-            return NULL;
-
-        snprintf(tag, PCI_ID_PATH_TAG_LENGTH, "pci-%04x_%02x_%02x_%1u",
-                 device->businfo.pci->domain, device->businfo.pci->bus,
-                 device->businfo.pci->dev, device->businfo.pci->func);
+      if (asprintf(&tag, "pci-%04x_%02x_%02x_%1u",
+                   device->businfo.pci->domain,
+                   device->businfo.pci->bus,
+                   device->businfo.pci->dev,
+                   device->businfo.pci->func) < 0) {
+         return NULL;
+      }
    }
    return tag;
 }