chiark / gitweb /
[PATCH] check for strlen()==0 before accessing strlen()-1
[elogind.git] / udev_rules.c
index 2581dc2a78decf91901399653a29ea76d967aa5a..c07abb9be064b6897c150e095510a5c7ffa196eb 100644 (file)
@@ -287,8 +287,18 @@ static void apply_format(struct udevice *udev, char *string, size_t maxsize,
                                break;
                        }
                        if (find_sysfs_attribute(class_dev, sysfs_device, attr, temp2, sizeof(temp2)) != 0) {
-                               dbg("sysfs attribute '%s' not found", attr);
-                               break;
+                               struct sysfs_device *parent_device;
+
+                               dbg("sysfs attribute '%s' not found, walk up the physical devices", attr);
+                               parent_device = sysfs_get_device_parent(sysfs_device);
+                               while (parent_device) {
+                                       dbg("looking at '%s'", parent_device->path);
+                                       if (find_sysfs_attribute(NULL, parent_device, attr, temp2, sizeof(temp2)) == 0)
+                                               break;
+                                       parent_device = sysfs_get_device_parent(parent_device);
+                               }
+                               if (!parent_device)
+                                       break;
                        }
                        /* strip trailing whitespace of sysfs value */
                        i = strlen(temp2);
@@ -453,34 +463,12 @@ static int execute_program(struct udevice *udev, const char *path, char *value,
        return retval;
 }
 
-static int compare_sysfs_attribute(struct sysfs_class_device *class_dev, struct sysfs_device *sysfs_device, struct key_pair *pair)
-{
-       char value[VALUE_SIZE];
-       int i;
-
-       if (find_sysfs_attribute(class_dev, sysfs_device, pair->name, value, sizeof(value)) != 0)
-               return -1;
-
-       /* strip trailing whitespace of value, if not asked to match for it */
-       if (!isspace(pair->value[strlen(pair->value)-1])) {
-               i = strlen(value);
-               while (i > 0 && isspace(value[i-1]))
-                       value[--i] = '\0';
-               dbg("removed %i trailing whitespace chars from '%s'", strlen(value)-i, value);
-       }
-
-       dbg("compare attribute '%s' value '%s' with '%s'", pair->name, value, pair->value);
-       if (strcmp_pattern(pair->value, value) != 0)
-               return -1;
-
-       dbg("found matching attribute '%s' with value '%s'", pair->name, pair->value);
-       return 0;
-}
-
 static int match_rule(struct udevice *udev, struct udev_rule *rule,
                      struct sysfs_class_device *class_dev, struct sysfs_device *sysfs_device)
 {
-       if (rule->kernel[0] != '\0') {
+       struct sysfs_device *parent_device = sysfs_device;
+
+       if (rule->kernel_operation != KEY_OP_UNSET) {
                dbg("check for " KEY_KERNEL " rule->kernel='%s' class_dev->name='%s'",
                    rule->kernel, class_dev->name);
                if (strcmp_pattern(rule->kernel, class_dev->name) != 0) {
@@ -495,7 +483,7 @@ static int match_rule(struct udevice *udev, struct udev_rule *rule,
                dbg(KEY_KERNEL " key is true");
        }
 
-       if (rule->subsystem[0] != '\0') {
+       if (rule->subsystem_operation != KEY_OP_UNSET) {
                dbg("check for " KEY_SUBSYSTEM " rule->subsystem='%s' class_dev->name='%s'",
                    rule->subsystem, class_dev->name);
                if (strcmp_pattern(rule->subsystem, udev->subsystem) != 0) {
@@ -540,14 +528,14 @@ static int match_rule(struct udevice *udev, struct udev_rule *rule,
        /* walk up the chain of physical devices and find a match */
        while (1) {
                /* check for matching driver */
-               if (rule->driver[0] != '\0') {
-                       if (sysfs_device == NULL) {
+               if (rule->driver_operation != KEY_OP_UNSET) {
+                       if (parent_device == NULL) {
                                dbg("device has no sysfs_device");
                                goto exit;
                        }
                        dbg("check for " KEY_DRIVER " rule->driver='%s' sysfs_device->driver_name='%s'",
-                           rule->driver, sysfs_device->driver_name);
-                       if (strcmp_pattern(rule->driver, sysfs_device->driver_name) != 0) {
+                           rule->driver, parent_device->driver_name);
+                       if (strcmp_pattern(rule->driver, parent_device->driver_name) != 0) {
                                dbg(KEY_DRIVER " is not matching");
                                if (rule->driver_operation != KEY_OP_NOMATCH)
                                        goto try_parent;
@@ -560,14 +548,14 @@ static int match_rule(struct udevice *udev, struct udev_rule *rule,
                }
 
                /* check for matching bus value */
-               if (rule->bus[0] != '\0') {
-                       if (sysfs_device == NULL) {
+               if (rule->bus_operation != KEY_OP_UNSET) {
+                       if (parent_device == NULL) {
                                dbg("device has no sysfs_device");
                                goto exit;
                        }
                        dbg("check for " KEY_BUS " rule->bus='%s' sysfs_device->bus='%s'",
-                           rule->bus, sysfs_device->bus);
-                       if (strcmp_pattern(rule->bus, sysfs_device->bus) != 0) {
+                           rule->bus, parent_device->bus);
+                       if (strcmp_pattern(rule->bus, parent_device->bus) != 0) {
                                dbg(KEY_BUS " is not matching");
                                if (rule->bus_operation != KEY_OP_NOMATCH)
                                        goto try_parent;
@@ -580,13 +568,13 @@ static int match_rule(struct udevice *udev, struct udev_rule *rule,
                }
 
                /* check for matching bus id */
-               if (rule->id[0] != '\0') {
-                       if (sysfs_device == NULL) {
+               if (rule->id_operation != KEY_OP_UNSET) {
+                       if (parent_device == NULL) {
                                dbg("device has no sysfs_device");
                                goto exit;
                        }
                        dbg("check " KEY_ID);
-                       if (strcmp_pattern(rule->id, sysfs_device->bus_id) != 0) {
+                       if (strcmp_pattern(rule->id, parent_device->bus_id) != 0) {
                                dbg(KEY_ID " is not matching");
                                if (rule->id_operation != KEY_OP_NOMATCH)
                                        goto try_parent;
@@ -605,9 +593,24 @@ static int match_rule(struct udevice *udev, struct udev_rule *rule,
                        dbg("check " KEY_SYSFS " pairs");
                        for (i = 0; i < rule->sysfs_pair_count; i++) {
                                struct key_pair *pair;
+                               char value[VALUE_SIZE];
+                               size_t len;
 
                                pair = &rule->sysfs_pair[i];
-                               if (compare_sysfs_attribute(class_dev, sysfs_device, pair) != 0) {
+                               if (find_sysfs_attribute(class_dev, parent_device, pair->name, value, sizeof(value)) != 0)
+                                       goto try_parent;
+
+                               /* strip trailing whitespace of value, if not asked to match for it */
+                               len = strlen(pair->value);
+                               if (len && !isspace(pair->value[len-1])) {
+                                       len = strlen(value);
+                                       while (len > 0 && isspace(value[len-1]))
+                                               value[--len] = '\0';
+                                       dbg("removed %i trailing whitespace chars from '%s'", strlen(value)-len, value);
+                               }
+
+                               dbg("compare attribute '%s' value '%s' with '%s'", pair->name, value, pair->value);
+                               if (strcmp_pattern(pair->value, value) != 0) {
                                        dbg(KEY_SYSFS "{'%s'} is not matching", pair->name);
                                        if (pair->operation != KEY_OP_NOMATCH)
                                                goto try_parent;
@@ -624,15 +627,15 @@ static int match_rule(struct udevice *udev, struct udev_rule *rule,
                break;
 try_parent:
                dbg("try parent sysfs device");
-               sysfs_device = sysfs_get_device_parent(sysfs_device);
-               if (sysfs_device == NULL)
+               parent_device = sysfs_get_device_parent(parent_device);
+               if (parent_device == NULL)
                        goto exit;
-               dbg("look at sysfs_device->path='%s'", sysfs_device->path);
-               dbg("look at sysfs_device->bus_id='%s'", sysfs_device->bus_id);
+               dbg("look at sysfs_device->path='%s'", parent_device->path);
+               dbg("look at sysfs_device->bus_id='%s'", parent_device->bus_id);
        }
 
        /* execute external program */
-       if (rule->program[0] != '\0') {
+       if (rule->program_operation != KEY_OP_UNSET) {
                char program[PATH_SIZE];
 
                dbg("check " KEY_PROGRAM);
@@ -651,7 +654,7 @@ try_parent:
        }
 
        /* check for matching result of external program */
-       if (rule->result[0] != '\0') {
+       if (rule->result_operation != KEY_OP_UNSET) {
                dbg("check for " KEY_RESULT " rule->result='%s', udev->program_result='%s'",
                   rule->result, udev->program_result);
                if (strcmp_pattern(rule->result, udev->program_result) != 0) {