several differnt memory leaks, missing checks and other minor issues.

Spotted by Coverity Scan

Signed-off-by: Fabio M. Di Nitto <fdini...@redhat.com>
---
:100644 100644 7607714... dd69401... M  config/tools/ldap/rng2ldif/genclass.c
:100644 100644 6b24dd2... 635d4c8... M  config/tools/ldap/rng2ldif/rng2ldif.c
:100644 100644 4c6c28f... 7c63124... M  config/tools/ldap/rng2ldif/tree.c
:100644 100644 7a99417... 010503a... M  config/tools/ldap/rng2ldif/value-list.c
 config/tools/ldap/rng2ldif/genclass.c   |    4 ++
 config/tools/ldap/rng2ldif/rng2ldif.c   |   10 ++++++
 config/tools/ldap/rng2ldif/tree.c       |   54 +++++++++++++++++++++++++-----
 config/tools/ldap/rng2ldif/value-list.c |   18 +++++++++-
 4 files changed, 75 insertions(+), 11 deletions(-)

diff --git a/config/tools/ldap/rng2ldif/genclass.c 
b/config/tools/ldap/rng2ldif/genclass.c
index 7607714..dd69401 100644
--- a/config/tools/ldap/rng2ldif/genclass.c
+++ b/config/tools/ldap/rng2ldif/genclass.c
@@ -1,6 +1,7 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <sys/types.h>
+#include <sys/stat.h>
 #include <string.h>
 #include <unistd.h>
 
@@ -40,12 +41,15 @@ write_class_struct(char *csv, char *arg, struct idinfo *ids)
        char filename[4096];
        FILE *out = NULL;
        int fd = -1;
+       mode_t oldumask;
 
        if (!strcmp(arg, "-")) {
                out = stdout;
        } else {
+               oldumask=umask(S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH);
                snprintf(filename, sizeof(filename), "%s.XXXXXX", arg);
                fd = mkstemp(filename);
+               umask(oldumask);
                if (fd < 0) {
                        perror("mkstemp");
                        return -1;
diff --git a/config/tools/ldap/rng2ldif/rng2ldif.c 
b/config/tools/ldap/rng2ldif/rng2ldif.c
index 6b24dd2..635d4c8 100644
--- a/config/tools/ldap/rng2ldif/rng2ldif.c
+++ b/config/tools/ldap/rng2ldif/rng2ldif.c
@@ -1,6 +1,7 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <sys/types.h>
+#include <sys/stat.h>
 #include <string.h>
 #include <unistd.h>
 #include <time.h>
@@ -118,6 +119,12 @@ open_relaxng(const char *filename)
        }
 
        n = xmlDocGetRootElement(p);
+       if (!n) {
+               printf("Unable to determine xml root element\n");
+               xmlFreeDoc(p);
+               return NULL;
+       }
+
        if (xmlStrcmp(n->name, (xmlChar *)"grammar")) {
                printf("%s is not a relaxng grammar\n", filename);
                xmlFreeDoc(p);
@@ -141,12 +148,15 @@ write_ldap_schema(const char *rng, const char *arg,
        time_t now;
        struct tm now_tm;
        int fd = -1;
+       mode_t oldumask;
 
        if (!strcmp(arg, "-")) {
                out_ldap = stdout;
        } else {
+               oldumask=umask(S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH);
                snprintf(filename, sizeof(filename), "%s.XXXXXX", arg);
                fd = mkstemp(filename);
+               umask(oldumask);
                if (fd < 0) {
                        perror("mkstemp");
                        return -1;
diff --git a/config/tools/ldap/rng2ldif/tree.c 
b/config/tools/ldap/rng2ldif/tree.c
index 4c6c28f..7c63124 100644
--- a/config/tools/ldap/rng2ldif/tree.c
+++ b/config/tools/ldap/rng2ldif/tree.c
@@ -95,6 +95,10 @@ get_attr(xmlNodePtr curr_node, struct ldap_attr_node **attrs,
        char *name, *normalized;
 
        name = (char *)xmlGetProp(curr_node, (xmlChar *)"name");
+       if (!name) {
+               return NULL;
+       }
+
        normalized = normalize_name((const char *)name);
 
        n = find_attr_byname(*attrs, normalized);
@@ -152,8 +156,16 @@ find_ref(xmlNodePtr curr_node)
 
        dbg_printf("Trying to parse ref tag\n");
        name = (char *)xmlGetProp(curr_node, (xmlChar *)"name");
+       if (!name) {
+               fprintf(stderr, "Unable to determine xml name prop\n");
+               exit(1);
+       }
 
        n = xmlDocGetRootElement(curr_node->doc);
+       if (!n) {
+               fprintf(stderr, "Unable to determine xml root element\n");
+               exit(1);
+       }
        n = n->xmlChildrenNode;
        for (; n; n = n->next) {
                if (n->type != XML_ELEMENT_NODE)
@@ -189,7 +201,7 @@ find_optional_attributes(xmlNodePtr curr_node, int in_block,
 {
        xmlNodePtr node;
        struct ldap_attr_node *attr;
-       struct ldap_attr_meta_node *n;
+       struct ldap_attr_meta_node *n = NULL;
 
        if (!curr_node || (curr_node->type == XML_ELEMENT_NODE &&
            (curr_node->name && !strcasecmp((char *)curr_node->name, 
"element")))) {
@@ -201,6 +213,13 @@ find_optional_attributes(xmlNodePtr curr_node, int 
in_block,
        for (node = curr_node; node; node = node->next) {
                if (node->type != XML_ELEMENT_NODE)
                        continue;
+
+               if (!node->name) 
+                       continue;
+
+               if (strcmp((char *)node->name, "attribute"))
+                       continue;
+
                if (!strcasecmp((char *)node->name, "ref")) {
                        find_optional_attributes(
                                find_ref(node), 1, curr_obj, attrs, ids);
@@ -224,16 +243,14 @@ find_optional_attributes(xmlNodePtr curr_node, int 
in_block,
                        continue;
                }
 
-               if (!node->name || strcmp((char *)node->name,
-                           "attribute")) {
-                       continue;
-               }
-
                if (!in_block)
                        continue;
 
                attr = get_attr(node, attrs, ids);
-               n = zalloc(sizeof(*n));
+               if (!n) {
+                       n = zalloc(sizeof(*n));
+                       assert(n);
+               }
 
                dbg_printf("opt attr '%s'\n", attr->idval->name);
 
@@ -251,6 +268,12 @@ find_optional_attributes(xmlNodePtr curr_node, int 
in_block,
                n->node = attr;
                n->next = curr_obj->optional_attrs;
                curr_obj->optional_attrs = n;
+               n=NULL;
+       }
+
+       if (n) {
+               free(n);
+               n=NULL;
        }
        return 0;
 }
@@ -264,7 +287,7 @@ find_required_attributes(xmlNodePtr curr_node,
 {
        xmlNodePtr node;
        struct ldap_attr_node *attr;
-       struct ldap_attr_meta_node *n;
+       struct ldap_attr_meta_node *n = NULL;
 
        dbg_printf("lookin for required\n");
 
@@ -275,7 +298,10 @@ find_required_attributes(xmlNodePtr curr_node,
                        continue;
 
                attr = get_attr(node, attrs, ids);
-               n = zalloc(sizeof(*n));
+               if (!n) {
+                       n = zalloc(sizeof(*n));
+                       assert(n);
+               }
 
                dbg_printf("req attr '%s'\n", attr->idval->name);
 
@@ -293,6 +319,12 @@ find_required_attributes(xmlNodePtr curr_node,
                n->node = attr;
                n->next = curr_obj->required_attrs;
                curr_obj->required_attrs = n;
+               n=NULL;
+       }
+
+       if (n) {
+               free(n);
+               n=NULL;
        }
        return 0;
 }
@@ -311,6 +343,10 @@ parse_element_tag(xmlNodePtr curr_node,
        
        dbg_printf("Trying to parse element tag\n");
        n = (char *)xmlGetProp(curr_node, (xmlChar *)"name");
+       if (!n) {
+               printf("Unable to parse element tag\n");
+               exit(1);
+       }
        normalized = normalize_name(n);
        v = id_find(ids, normalized, OBJ, 0);
 
diff --git a/config/tools/ldap/rng2ldif/value-list.c 
b/config/tools/ldap/rng2ldif/value-list.c
index 7a99417..010503a 100644
--- a/config/tools/ldap/rng2ldif/value-list.c
+++ b/config/tools/ldap/rng2ldif/value-list.c
@@ -3,6 +3,8 @@
 #include <assert.h>
 #include <unistd.h>
 #include <string.h>
+#include <sys/types.h>
+#include <sys/stat.h>
 #include "value-list.h"
 #include "zalloc.h"
 
@@ -71,11 +73,15 @@ int
 id_writefile(struct idinfo *oi, char *filename)
 {
        char tmpfn[4096];
-       FILE *fp;
+       FILE *fp = NULL;
        int fd;
+       mode_t oldumask;
+
+       oldumask=umask(S_IWGRP | S_IRGRP | S_IWOTH | S_IROTH);
 
        snprintf(tmpfn, sizeof(tmpfn), "%s.XXXXXX", filename);
        fd = mkstemp(tmpfn);
+       umask(oldumask);
        if (fd < 0)
                return -1;
 
@@ -109,8 +115,13 @@ id_readfile(struct idinfo *oi, char *filename)
        char buf[4096];
        int len, lineno = 0, entries = 0;
 
-       fp = fopen(filename, "r");
        if (!filename) {
+               perror("no file?");
+               return 1;
+       }
+
+       fp = fopen(filename, "r");
+       if (!fp) {
                perror("fopen");
                return 1;
        }
@@ -127,6 +138,7 @@ id_readfile(struct idinfo *oi, char *filename)
                        --len;
                }
                v = zalloc(sizeof(*v));
+               assert(v);
 
                /* Attribute / object */
                c = strchr(buf, ',');
@@ -184,6 +196,8 @@ id_readfile(struct idinfo *oi, char *filename)
                }
 
                ++entries;
+               free(v);
+               v = NULL;
        }
 
        fclose(fp);
-- 
1.7.4.4

Reply via email to