libmultipath: resource leak in read_value_block()
[multipath-tools/.git] / libmultipath / parser.c
index b964ce7..526c45b 100644 (file)
@@ -25,8 +25,8 @@
 
 /* local vars */
 static int sublevel = 0;
-vector keywords = NULL;
-vector *keywords_addr = NULL;
+static vector keywords = NULL;
+static vector *keywords_addr = NULL;
 static int line_nr;
 
 void set_current_keywords (vector *k)
@@ -311,8 +311,10 @@ read_value_block(void)
 
        buf = (char *) MALLOC(MAXBUF);
 
-       if (!buf)
+       if (!buf) {
+               vector_free(elements);
                return NULL;
+       }
 
        while (read_line(buf, MAXBUF)) {
                vec = alloc_strvec(buf);
@@ -323,21 +325,20 @@ read_value_block(void)
                                break;
                        }
 
-                       if (VECTOR_SIZE(vec))
-                               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)) {
-                                               free_strvec(vec);
-                                               goto out1;
-                                       }
+                       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));
 
-                                       vector_set_slot(elements, dup);
+                               if (!vector_alloc_slot(elements)) {
+                                       free_strvec(vec);
+                                       goto out1;
                                }
+
+                               vector_set_slot(elements, dup);
+                       }
                        free_strvec(vec);
                }
                memset(buf, 0, MAXBUF);
@@ -348,6 +349,7 @@ out1:
        FREE(dup);
 out:
        FREE(buf);
+       vector_free(elements);
        return NULL;
 }
 
@@ -387,12 +389,19 @@ void *
 set_value(vector strvec)
 {
        char *str = VECTOR_SLOT(strvec, 1);
-       int size = strlen(str);
+       size_t size;
        int i = 0;
        int len = 0;
        char *alloc = NULL;
        char *tmp;
 
+       if (!str)
+               return NULL;
+
+       size = strlen(str);
+       if (size == 0)
+               return NULL;
+
        if (*str == '"') {
                for (i = 2; i < VECTOR_SIZE(strvec); i++) {
                        str = VECTOR_SLOT(strvec, i);
@@ -526,16 +535,23 @@ out:
        return r;
 }
 
+int alloc_keywords(void)
+{
+       if (!keywords)
+               keywords = vector_alloc();
+
+       if (!keywords)
+               return 1;
+
+       return 0;
+}
+
 /* Data initialization */
 int
 init_data(char *conf_file, void (*init_keywords) (void))
 {
        int r;
 
-       if (!keywords)
-               keywords = vector_alloc();
-       if (!keywords)
-               return 1;
        stream = fopen(conf_file, "r");
        if (!stream) {
                syslog(LOG_WARNING, "Configuration file open problem");