From 9cf864ba2b4b13a640e92f7cb61d5b58cb6e355e Mon Sep 17 00:00:00 2001
From: Travis Cross <tc@traviscross.com>
Date: Fri, 28 Feb 2014 17:19:43 +0000
Subject: [PATCH] Deal with read errors in switch_xml.c

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 | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)

diff --git a/src/switch_xml.c b/src/switch_xml.c
index 1d7adaab28..9fcbd44eb2 100644
--- a/src/switch_xml.c
+++ b/src/switch_xml.c
@@ -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);