]> git.ipfire.org Git - thirdparty/freeswitch.git/commitdiff
Deal with read errors in switch_xml.c
authorTravis Cross <tc@traviscross.com>
Fri, 28 Feb 2014 17:19:43 +0000 (17:19 +0000)
committerTravis Cross <tc@traviscross.com>
Sun, 2 Mar 2014 10:36:14 +0000 (10:36 +0000)
Unlike fread(3), read(3) will return -1 on error.  We were assigning
the result of read to a potentially unsigned variable, and passing the
result down to switch_xml_parse_str() where it would end up
determining how many bytes to malloc(3).

src/switch_xml.c

index 74bc6d28b6bc504a5893bed8d3ec00f841638aa1..614ed178c113cb7a47aaf9ed01081b14c94db633 100644 (file)
@@ -1198,7 +1198,7 @@ SWITCH_DECLARE(switch_xml_t) switch_xml_parse_fd(int fd)
 {
        switch_xml_root_t root;
        struct stat st;
-       switch_size_t l;
+       switch_ssize_t l;
        void *m;
 
        if (fd < 0)
@@ -1212,8 +1212,8 @@ SWITCH_DECLARE(switch_xml_t) switch_xml_parse_fd(int fd)
        m = malloc(st.st_size);
        if (!m)
                return NULL;
-       l = read(fd, m, st.st_size);
-       if (!l || !(root = (switch_xml_root_t) switch_xml_parse_str((char *) m, l))) {
+       if (!(0<(l = read(fd, m, st.st_size)))
+               || !(root = (switch_xml_root_t) switch_xml_parse_str((char *) m, l))) {
                free(m);
                return NULL;
        }
@@ -1583,7 +1583,7 @@ SWITCH_DECLARE(switch_xml_t) switch_xml_parse_file_simple(const char *file)
 {
        int fd = -1;
        struct stat st;
-       switch_size_t l;
+       switch_ssize_t l;
        void *m;
        switch_xml_root_t root;
 
@@ -1592,7 +1592,7 @@ SWITCH_DECLARE(switch_xml_t) switch_xml_parse_file_simple(const char *file)
                if (!st.st_size) goto error;
                m = malloc(st.st_size);
                switch_assert(m);
-               if (!(l = read(fd, m, st.st_size))) goto error;
+               if (!(0<(l = read(fd, m, st.st_size)))) goto error;
                if (!(root = (switch_xml_root_t) switch_xml_parse_str((char *) m, l))) goto error;
                root->dynamic = 1;
                close(fd);