[PATCH libdrm 1/2] Avoid hardcoded strlens in drmParseSubsystemType().

Eric Engestrom eric.engestrom at intel.com
Fri Nov 16 07:43:05 UTC 2018


On Thursday, 2018-11-15 21:18:42 -0800, Eric Anholt wrote:
> Having people count characters is error-prone, when we could just have
> a computer do it.

Reviewed-by: Eric Engestrom <eric.engestrom at intel.com>

> ---
>  xf86drm.c | 31 ++++++++++++++++---------------
>  1 file changed, 16 insertions(+), 15 deletions(-)
> 
> diff --git a/xf86drm.c b/xf86drm.c
> index 10df682b7870..60fbc49b3d35 100644
> --- a/xf86drm.c
> +++ b/xf86drm.c
> @@ -59,6 +59,8 @@
>  #endif
>  #include <math.h>
>  
> +#define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
> +
>  /* Not all systems have MAP_FAILED defined */
>  #ifndef MAP_FAILED
>  #define MAP_FAILED ((void *)-1)
> @@ -2984,6 +2986,16 @@ static int drmParseSubsystemType(int maj, int min)
>      char path[PATH_MAX + 1];
>      char link[PATH_MAX + 1] = "";
>      char *name;
> +    struct {
> +        const char *name;
> +        int bus_type;
> +    } bus_types[] = {
> +        { "/pci", DRM_BUS_PCI },
> +        { "/usb", DRM_BUS_USB },
> +        { "/platform", DRM_BUS_PLATFORM },
> +        { "/host1x", DRM_BUS_HOST1X },
> +        { "/virtio", DRM_BUS_VIRTIO },
> +    };
>  
>      snprintf(path, PATH_MAX, "/sys/dev/char/%d:%d/device/subsystem",
>               maj, min);
> @@ -2995,20 +3007,10 @@ static int drmParseSubsystemType(int maj, int min)
>      if (!name)
>          return -EINVAL;
>  
> -    if (strncmp(name, "/pci", 4) == 0)
> -        return DRM_BUS_PCI;
> -
> -    if (strncmp(name, "/usb", 4) == 0)
> -        return DRM_BUS_USB;
> -
> -    if (strncmp(name, "/platform", 9) == 0)
> -        return DRM_BUS_PLATFORM;
> -
> -    if (strncmp(name, "/host1x", 7) == 0)
> -        return DRM_BUS_HOST1X;
> -
> -    if (strncmp(name, "/virtio", 7) == 0)
> -        return DRM_BUS_VIRTIO;
> +    for (unsigned i = 0; i < ARRAY_SIZE(bus_types); i++) {
> +        if (strncmp(name, bus_types[i].name, strlen(bus_types[i].name)) == 0)
> +            return bus_types[i].bus_type;
> +    }
>  
>      return -EINVAL;
>  #elif defined(__OpenBSD__)
> @@ -3149,7 +3151,6 @@ static int parse_separate_sysfs_files(int maj, int min,
>                                        drmPciDeviceInfoPtr device,
>                                        bool ignore_revision)
>  {
> -#define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
>      static const char *attrs[] = {
>        "revision", /* Older kernels are missing the file, so check for it first */
>        "vendor",
> -- 
> 2.19.1
> 
> _______________________________________________
> dri-devel mailing list
> dri-devel at lists.freedesktop.org
> https://lists.freedesktop.org/mailman/listinfo/dri-devel


More information about the dri-devel mailing list