cleanup various MALLOC/REALLOC callers
authorMike Snitzer <snitzer@redhat.com>
Wed, 29 Apr 2009 19:25:42 +0000 (15:25 -0400)
committerMike Snitzer <snitzer@redhat.com>
Wed, 29 Apr 2009 19:25:42 +0000 (15:25 -0400)
- alloc_hwe and alloc_mpe should've been used
- MALLOC and REALLOC returned pointer must be checked before use

Signed-off-by: Mike Snitzer <snitzer@redhat.com>
libmultipath/config.c
libmultipath/config.h
libmultipath/dict.c
libmultipath/parser.c
libmultipath/prio.c
libmultipath/uevent.c
libmultipath/uxsock.c

index 05dbcd2..4f62fc5 100644 (file)
@@ -221,7 +221,7 @@ alloc_mpe (void)
        return mpe;
 }
 
-static struct hwentry *
+struct hwentry *
 alloc_hwe (void)
 {
        struct hwentry * hwe = (struct hwentry *)
index 0d96433..ca4f292 100644 (file)
@@ -99,6 +99,7 @@ struct hwentry * find_hwe (vector hwtable, char * vendor, char * product, char *
 struct mpentry * find_mpe (char * wwid);
 char * get_mpe_wwid (char * alias);
 
+struct hwentry * alloc_hwe (void);
 struct mpentry * alloc_mpe (void);
 
 void free_hwe (struct hwentry * hwe);
index aa350f4..e5f2f52 100644 (file)
@@ -465,13 +465,13 @@ device_handler(vector strvec)
 {
        struct hwentry * hwe;
 
-       hwe = (struct hwentry *)MALLOC(sizeof(struct hwentry));
+       hwe = alloc_hwe();
 
        if (!hwe)
                return 1;
 
        if (!vector_alloc_slot(conf->hwtable)) {
-               FREE(hwe);
+               free_hwe(hwe);
                return 1;
        }
        vector_set_slot(conf->hwtable, hwe);
@@ -802,13 +802,13 @@ multipath_handler(vector strvec)
 {
        struct mpentry * mpe;
 
-       mpe = (struct mpentry *)MALLOC(sizeof(struct mpentry));
+       mpe = alloc_mpe();
 
        if (!mpe)
                return 1;
 
        if (!vector_alloc_slot(conf->mptable)) {
-               FREE(mpe);
+               free_mpe(mpe);
                return 1;
        }
        vector_set_slot(conf->mptable, mpe);
index 6e42590..eb3815e 100644 (file)
@@ -303,14 +303,14 @@ read_value_block(void)
        vector vec = NULL;
        vector elements = vector_alloc();
 
+       if (!elements)
+               return NULL;
+
        buf = (char *) MALLOC(MAXBUF);
 
        if (!buf)
                return NULL;
 
-       if (!elements)
-               goto out;
-
        while (read_line(buf, MAXBUF)) {
                vec = alloc_strvec(buf);
                if (vec) {
@@ -324,6 +324,8 @@ read_value_block(void)
                                for (i = 0; i < VECTOR_SIZE(vec); i++) {
                                        str = VECTOR_SLOT(vec, i);
                                        dup = (char *) MALLOC(strlen(str) + 1);
+                                       if (!dup)
+                                               goto out;
                                        memcpy(dup, str, strlen(str));
 
                                        if (!vector_alloc_slot(elements)) {
@@ -400,16 +402,17 @@ set_value(vector strvec)
                                alloc =
                                    REALLOC(alloc, sizeof (char *) * (len + 1));
                                tmp = VECTOR_SLOT(strvec, i-1);
-                               if (*str != '"' && *tmp != '"')
+                               if (alloc && *str != '"' && *tmp != '"')
                                        strncat(alloc, " ", 1);
                        }
 
-                       if (i != VECTOR_SIZE(strvec)-1)
+                       if (alloc && i != VECTOR_SIZE(strvec)-1)
                                strncat(alloc, str, strlen(str));
                }
        } else {
                alloc = MALLOC(sizeof (char *) * (size + 1));
-               memcpy(alloc, str, size);
+               if (alloc)
+                       memcpy(alloc, str, size);
        }
        return alloc;
 }
index 4c5f4f0..9c40f1d 100644 (file)
@@ -16,7 +16,7 @@ int init_prio (void)
        return 0;
 }
 
-struct prio * alloc_prio (void)
+static struct prio * alloc_prio (void)
 {
        return MALLOC(sizeof(struct prio));
 }
index 29441c1..0ed7e12 100644 (file)
@@ -53,7 +53,7 @@ pthread_cond_t  uev_cond,  *uev_condp  = &uev_cond;
 uev_trigger *my_uev_trigger;
 void * my_trigger_data;
 
-struct uevent * alloc_uevent (void)
+static struct uevent * alloc_uevent (void)
 {
        return (struct uevent *)MALLOC(sizeof(struct uevent));
 }
index 7bfc5d2..d5d1be9 100644 (file)
@@ -154,6 +154,8 @@ int recv_packet(int fd, char **buf, size_t *len)
 {
        if (read_all(fd, len, sizeof(*len)) != sizeof(*len)) return -1;
        (*buf) = MALLOC(*len);
+       if (!*buf)
+               return -1;
        if (read_all(fd, *buf, *len) != *len) {
                FREE(*buf);
                return -1;