[systemd-commits] 2 commits - src/udev

Zbigniew Jędrzejewski-Szmek zbyszek at kemper.freedesktop.org
Fri Apr 5 14:40:10 PDT 2013


 src/udev/udev-builtin-usb_id.c |   34 +++++++----------
 src/udev/udevadm-hwdb.c        |   78 +++++++++++++++++++----------------------
 2 files changed, 51 insertions(+), 61 deletions(-)

New commits:
commit 8c62ecf1a99ab4a3f69cb81be38715c504ef5723
Author: Zbigniew Jędrzejewski-Szmek <zbyszek at in.waw.pl>
Date:   Fri Apr 5 17:39:46 2013 -0400

    udevadm: do not free node on success
    
    A fix for ff03aed06a422.

diff --git a/src/udev/udevadm-hwdb.c b/src/udev/udevadm-hwdb.c
index 1e80b0d..b18b28a 100644
--- a/src/udev/udevadm-hwdb.c
+++ b/src/udev/udevadm-hwdb.c
@@ -84,14 +84,12 @@ static int trie_children_cmp(const void *v1, const void *v2) {
 
 static int node_add_child(struct trie *trie, struct trie_node *node, struct trie_node *node_child, uint8_t c) {
         struct trie_child_entry *child;
-        int err = 0;
 
         /* extend array, add new entry, sort for bisection */
         child = realloc(node->children, (node->children_count + 1) * sizeof(struct trie_child_entry));
-        if (!child) {
-                err = -ENOMEM;
-                goto out;
-        }
+        if (!child)
+                return -ENOMEM;
+
         node->children = child;
         trie->children_count++;
         node->children[node->children_count].c = c;
@@ -99,8 +97,8 @@ static int node_add_child(struct trie *trie, struct trie_node *node, struct trie
         node->children_count++;
         qsort(node->children, node->children_count, sizeof(struct trie_child_entry), trie_children_cmp);
         trie->nodes_count++;
-out:
-        return err;
+
+        return 0;
 }
 
 static struct trie_node *node_lookup(const struct trie_node *node, uint8_t c) {
@@ -176,53 +174,51 @@ static int trie_insert(struct trie *trie, struct trie_node *node, const char *se
                        const char *key, const char *value) {
         size_t i = 0;
         int err = 0;
-        struct trie_node _cleanup_free_ *child = NULL;
 
         for (;;) {
                 size_t p;
                 uint8_t c;
+                struct trie_node *child;
 
                 for (p = 0; (c = trie->strings->buf[node->prefix_off + p]); p++) {
-                        char *s;
+                        char _cleanup_free_ *s = NULL;
                         ssize_t off;
+                        struct trie_node _cleanup_free_ *new_child = NULL;
 
                         if (c == search[i + p])
                                 continue;
 
                         /* split node */
-                        child = calloc(sizeof(struct trie_node), 1);
-                        if (!child) {
-                                err = -ENOMEM;
-                                goto out;
-                        }
+                        new_child = calloc(sizeof(struct trie_node), 1);
+                        if (!new_child)
+                                return -ENOMEM;
 
                         /* move values from parent to child */
-                        child->prefix_off = node->prefix_off + p+1;
-                        child->children = node->children;
-                        child->children_count = node->children_count;
-                        child->values = node->values;
-                        child->values_count = node->values_count;
+                        new_child->prefix_off = node->prefix_off + p+1;
+                        new_child->children = node->children;
+                        new_child->children_count = node->children_count;
+                        new_child->values = node->values;
+                        new_child->values_count = node->values_count;
 
                         /* update parent; use strdup() because the source gets realloc()d */
                         s = strndup(trie->strings->buf + node->prefix_off, p);
-                        if (!s) {
-                                err = -ENOMEM;
-                                goto out;
-                        }
+                        if (!s)
+                                return -ENOMEM;
+
                         off = strbuf_add_string(trie->strings, s, p);
-                        free(s);
-                        if (off < 0) {
-                                err = off;
-                                goto out;
-                        }
+                        if (off < 0)
+                                return off;
+
                         node->prefix_off = off;
                         node->children = NULL;
                         node->children_count = 0;
                         node->values = NULL;
                         node->values_count = 0;
-                        err = node_add_child(trie, node, child, c);
+                        err = node_add_child(trie, node, new_child, c);
                         if (err)
-                                goto out;
+                                return err;
+
+                        new_child = NULL; /* avoid cleanup */
                         break;
                 }
                 i += p;
@@ -237,28 +233,28 @@ static int trie_insert(struct trie *trie, struct trie_node *node, const char *se
 
                         /* new child */
                         child = calloc(sizeof(struct trie_node), 1);
-                        if (!child) {
-                                err = -ENOMEM;
-                                goto out;
-                        }
+                        if (!child)
+                                return -ENOMEM;
+
                         off = strbuf_add_string(trie->strings, search + i+1, strlen(search + i+1));
                         if (off < 0) {
-                                err = off;
-                                goto out;
+                                free(child);
+                                return off;
                         }
+
                         child->prefix_off = off;
                         err = node_add_child(trie, node, child, c);
-                        if (err)
-                                goto out;
+                        if (err) {
+                                free(child);
+                                return err;
+                        }
+
                         return trie_node_add_value(trie, child, key, value);
                 }
 
                 node = child;
-                child = NULL; /* avoid cleanup */
                 i++;
         }
-out:
-        return err;
 }
 
 struct trie_f {

commit bad490b0518e4fba1ac04022e0f7f7fa5c65dc76
Author: Zbigniew Jędrzejewski-Szmek <zbyszek at in.waw.pl>
Date:   Fri Apr 5 17:40:31 2013 -0400

    udev-builtin-usb_id: avoid comparison of unsigned and ssize_t
    
    For some reason this shows up on i686 only:
    src/udev/udev-builtin-usb_id.c:192:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]

diff --git a/src/udev/udev-builtin-usb_id.c b/src/udev/udev-builtin-usb_id.c
index dcb2468..269aa4e 100644
--- a/src/udev/udev-builtin-usb_id.c
+++ b/src/udev/udev-builtin-usb_id.c
@@ -151,11 +151,12 @@ static void set_scsi_type(char *to, const char *from, size_t len)
 
 static int dev_if_packed_info(struct udev_device *dev, char *ifs_str, size_t len)
 {
-        char *filename = NULL;
-        int fd;
+        char _cleanup_free_ *filename = NULL;
+        int _cleanup_close_ fd = -1;
         ssize_t size;
         unsigned char buf[18 + 65535];
-        unsigned int pos, strpos;
+        int pos = 0;
+        unsigned strpos = 0;
         struct usb_interface_descriptor {
                 u_int8_t        bLength;
                 u_int8_t        bDescriptorType;
@@ -167,27 +168,20 @@ static int dev_if_packed_info(struct udev_device *dev, char *ifs_str, size_t len
                 u_int8_t        bInterfaceProtocol;
                 u_int8_t        iInterface;
         } __attribute__((packed));
-        int err = 0;
 
-        if (asprintf(&filename, "%s/descriptors", udev_device_get_syspath(dev)) < 0) {
-                err = -1;
-                goto out;
-        }
+        if (asprintf(&filename, "%s/descriptors", udev_device_get_syspath(dev)) < 0)
+                return log_oom();
+
         fd = open(filename, O_RDONLY|O_CLOEXEC);
         if (fd < 0) {
                 fprintf(stderr, "error opening USB device 'descriptors' file\n");
-                err = -1;
-                goto out;
+                return -errno;
         }
+
         size = read(fd, buf, sizeof(buf));
-        close(fd);
-        if (size < 18 || size == sizeof(buf)) {
-                err = -1;
-                goto out;
-        }
+        if (size < 18 || size == sizeof(buf))
+                return -EIO;
 
-        pos = 0;
-        strpos = 0;
         ifs_str[0] = '\0';
         while (pos < size && strpos+7 < len-2) {
                 struct usb_interface_descriptor *desc;
@@ -213,13 +207,13 @@ static int dev_if_packed_info(struct udev_device *dev, char *ifs_str, size_t len
                 memcpy(&ifs_str[strpos], if_str, 8),
                 strpos += 7;
         }
+
         if (strpos > 0) {
                 ifs_str[strpos++] = ':';
                 ifs_str[strpos++] = '\0';
         }
-out:
-        free(filename);
-        return err;
+
+        return 0;
 }
 
 /*



More information about the systemd-commits mailing list