Avoid hardcoded strlens in drmParseSubsystemType().

Having people count characters is error-prone, when we could just have
a computer do it.

Reviewed-by: Eric Engestrom <eric.engestrom@intel.com>
main
Eric Anholt 2018-11-15 17:48:53 -08:00
parent e642f480b8
commit 9b28c5aea3
1 changed files with 16 additions and 15 deletions

View File

@ -59,6 +59,8 @@
#endif #endif
#include <math.h> #include <math.h>
#define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
/* Not all systems have MAP_FAILED defined */ /* Not all systems have MAP_FAILED defined */
#ifndef MAP_FAILED #ifndef MAP_FAILED
#define MAP_FAILED ((void *)-1) #define MAP_FAILED ((void *)-1)
@ -2984,6 +2986,16 @@ static int drmParseSubsystemType(int maj, int min)
char path[PATH_MAX + 1]; char path[PATH_MAX + 1];
char link[PATH_MAX + 1] = ""; char link[PATH_MAX + 1] = "";
char *name; 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", snprintf(path, PATH_MAX, "/sys/dev/char/%d:%d/device/subsystem",
maj, min); maj, min);
@ -2995,20 +3007,10 @@ static int drmParseSubsystemType(int maj, int min)
if (!name) if (!name)
return -EINVAL; return -EINVAL;
if (strncmp(name, "/pci", 4) == 0) for (unsigned i = 0; i < ARRAY_SIZE(bus_types); i++) {
return DRM_BUS_PCI; if (strncmp(name, bus_types[i].name, strlen(bus_types[i].name)) == 0)
return bus_types[i].bus_type;
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;
return -EINVAL; return -EINVAL;
#elif defined(__OpenBSD__) #elif defined(__OpenBSD__)
@ -3149,7 +3151,6 @@ static int parse_separate_sysfs_files(int maj, int min,
drmPciDeviceInfoPtr device, drmPciDeviceInfoPtr device,
bool ignore_revision) bool ignore_revision)
{ {
#define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
static const char *attrs[] = { static const char *attrs[] = {
"revision", /* Older kernels are missing the file, so check for it first */ "revision", /* Older kernels are missing the file, so check for it first */
"vendor", "vendor",