Blame SOURCES/0120-RHBZ-1043093-realloc-fix.patch

ecd2a9
---
ecd2a9
 libmultipath/dmparser.c   |    6 ++++--
ecd2a9
 libmultipath/regex.c      |    9 ++++++++-
ecd2a9
 multipath/main.c          |    9 ++++++---
ecd2a9
 multipathd/cli_handlers.c |   41 ++++++++++++-----------------------------
ecd2a9
 multipathd/uxlsnr.c       |   13 ++++++++++++-
ecd2a9
 5 files changed, 42 insertions(+), 36 deletions(-)
ecd2a9
ecd2a9
Index: multipath-tools-130222/libmultipath/dmparser.c
ecd2a9
===================================================================
ecd2a9
--- multipath-tools-130222.orig/libmultipath/dmparser.c
ecd2a9
+++ multipath-tools-130222/libmultipath/dmparser.c
ecd2a9
@@ -20,14 +20,16 @@
ecd2a9
 static int
ecd2a9
 merge_words (char ** dst, char * word, int space)
ecd2a9
 {
ecd2a9
-	char * p;
ecd2a9
+	char * p = *dst;
ecd2a9
 	int len;
ecd2a9
 
ecd2a9
 	len = strlen(*dst) + strlen(word) + space;
ecd2a9
 	*dst = REALLOC(*dst, len + 1);
ecd2a9
 
ecd2a9
-	if (!*dst)
ecd2a9
+	if (!*dst) {
ecd2a9
+		free(p);
ecd2a9
 		return 1;
ecd2a9
+	}
ecd2a9
 
ecd2a9
 	p = *dst;
ecd2a9
 
ecd2a9
Index: multipath-tools-130222/libmultipath/regex.c
ecd2a9
===================================================================
ecd2a9
--- multipath-tools-130222.orig/libmultipath/regex.c
ecd2a9
+++ multipath-tools-130222/libmultipath/regex.c
ecd2a9
@@ -123,7 +123,14 @@ static void init_syntax_once(void)
ecd2a9
 
ecd2a9
 /* (Re)Allocate N items of type T using malloc, or fail.  */
ecd2a9
 #define TALLOC(n, t)	     ((t *) malloc ((n) * sizeof (t)))
ecd2a9
-#define RETALLOC(addr, n, t) ((addr) = (t *) realloc (addr, (n) * sizeof (t)))
ecd2a9
+#define RETALLOC(addr, n, t)                                            \
ecd2a9
+do {                                                                    \
ecd2a9
+	t *tmp = (t *) realloc (addr, (n) * sizeof (t));                \
ecd2a9
+	if (!tmp)                                                       \
ecd2a9
+		free(addr);                                             \
ecd2a9
+	(addr) = tmp;                                                   \
ecd2a9
+} while(0)
ecd2a9
+
ecd2a9
 #define REGEX_TALLOC(n, t)   ((t *) REGEX_ALLOCATE ((n) * sizeof (t)))
ecd2a9
 
ecd2a9
 #define BYTEWIDTH 8		/* In bits.  */
ecd2a9
Index: multipath-tools-130222/multipath/main.c
ecd2a9
===================================================================
ecd2a9
--- multipath-tools-130222.orig/multipath/main.c
ecd2a9
+++ multipath-tools-130222/multipath/main.c
ecd2a9
@@ -394,7 +394,7 @@ out:
ecd2a9
 static int
ecd2a9
 dump_config (void)
ecd2a9
 {
ecd2a9
-	char * c;
ecd2a9
+	char * c, * tmp = NULL;
ecd2a9
 	char * reply;
ecd2a9
 	unsigned int maxlen = 256;
ecd2a9
 	int again = 1;
ecd2a9
@@ -402,9 +402,12 @@ dump_config (void)
ecd2a9
 	reply = MALLOC(maxlen);
ecd2a9
 
ecd2a9
 	while (again) {
ecd2a9
-		if (!reply)
ecd2a9
+		if (!reply) {
ecd2a9
+			if (tmp)
ecd2a9
+				free(tmp);
ecd2a9
 			return 1;
ecd2a9
-		c = reply;
ecd2a9
+		}
ecd2a9
+		c = tmp = reply;
ecd2a9
 		c += snprint_defaults(c, reply + maxlen - c);
ecd2a9
 		again = ((c - reply) == maxlen);
ecd2a9
 		if (again) {
ecd2a9
Index: multipath-tools-130222/multipathd/cli_handlers.c
ecd2a9
===================================================================
ecd2a9
--- multipath-tools-130222.orig/multipathd/cli_handlers.c
ecd2a9
+++ multipath-tools-130222/multipathd/cli_handlers.c
ecd2a9
@@ -26,11 +26,14 @@
ecd2a9
 #define REALLOC_REPLY(r, a, m)					\
ecd2a9
 	do {							\
ecd2a9
 		if ((a)) {					\
ecd2a9
+			char *tmp = (r);			\
ecd2a9
 			(r) = REALLOC((r), (m) * 2);		\
ecd2a9
 			if ((r)) {				\
ecd2a9
 				memset((r) + (m), 0, (m));	\
ecd2a9
 				(m) *= 2;			\
ecd2a9
 			}					\
ecd2a9
+			else					\
ecd2a9
+				free(tmp);			\
ecd2a9
 		}						\
ecd2a9
 	} while (0)
ecd2a9
 
ecd2a9
@@ -144,7 +147,7 @@ show_config (char ** r, int * len)
ecd2a9
 	unsigned int maxlen = INITIAL_REPLY_LEN;
ecd2a9
 	int again = 1;
ecd2a9
 
ecd2a9
-	reply = MALLOC(maxlen);
ecd2a9
+	c = reply = MALLOC(maxlen);
ecd2a9
 
ecd2a9
 	while (again) {
ecd2a9
 		if (!reply)
ecd2a9
@@ -152,44 +155,24 @@ show_config (char ** r, int * len)
ecd2a9
 		c = reply;
ecd2a9
 		c += snprint_defaults(c, reply + maxlen - c);
ecd2a9
 		again = ((c - reply) == maxlen);
ecd2a9
-		if (again) {
ecd2a9
-			reply = REALLOC(reply, maxlen * 2);
ecd2a9
-			if (!reply)
ecd2a9
-				return 1;
ecd2a9
-			memset(reply + maxlen, 0, maxlen);
ecd2a9
-			maxlen *= 2;
ecd2a9
+		REALLOC_REPLY(reply, again, maxlen);
ecd2a9
+		if (again)
ecd2a9
 			continue;
ecd2a9
-		}
ecd2a9
 		c += snprint_blacklist(c, reply + maxlen - c);
ecd2a9
 		again = ((c - reply) == maxlen);
ecd2a9
-		if (again) {
ecd2a9
-			reply = REALLOC(reply, maxlen * 2);
ecd2a9
-			if (!reply)
ecd2a9
-				return 1;
ecd2a9
-			memset(reply + maxlen, 0, maxlen);
ecd2a9
-			maxlen *= 2;
ecd2a9
+		REALLOC_REPLY(reply, again, maxlen);
ecd2a9
+		if (again)
ecd2a9
 			continue;
ecd2a9
-		}
ecd2a9
 		c += snprint_blacklist_except(c, reply + maxlen - c);
ecd2a9
 		again = ((c - reply) == maxlen);
ecd2a9
-		if (again) {
ecd2a9
-			reply = REALLOC(reply, maxlen * 2);
ecd2a9
-			if (!reply)
ecd2a9
-				return 1;
ecd2a9
-			memset(reply + maxlen, 0, maxlen);
ecd2a9
-			maxlen *= 2;
ecd2a9
+		REALLOC_REPLY(reply, again, maxlen);
ecd2a9
+		if (again)
ecd2a9
 			continue;
ecd2a9
-		}
ecd2a9
 		c += snprint_hwtable(c, reply + maxlen - c, conf->hwtable);
ecd2a9
 		again = ((c - reply) == maxlen);
ecd2a9
-		if (again) {
ecd2a9
-			reply = REALLOC(reply, maxlen * 2);
ecd2a9
-			if (!reply)
ecd2a9
-				return 1;
ecd2a9
-			memset(reply + maxlen, 0, maxlen);
ecd2a9
-			maxlen *= 2;
ecd2a9
+		REALLOC_REPLY(reply, again, maxlen);
ecd2a9
+		if (again)
ecd2a9
 			continue;
ecd2a9
-		}
ecd2a9
 		c += snprint_mptable(c, reply + maxlen - c, conf->mptable);
ecd2a9
 		again = ((c - reply) == maxlen);
ecd2a9
 		REALLOC_REPLY(reply, again, maxlen);
ecd2a9
Index: multipath-tools-130222/multipathd/uxlsnr.c
ecd2a9
===================================================================
ecd2a9
--- multipath-tools-130222.orig/multipathd/uxlsnr.c
ecd2a9
+++ multipath-tools-130222/multipathd/uxlsnr.c
ecd2a9
@@ -64,6 +64,10 @@ static void new_client(int ux_sock)
ecd2a9
 
ecd2a9
 	/* put it in our linked list */
ecd2a9
 	c = (struct client *)MALLOC(sizeof(*c));
ecd2a9
+	if (!c) {
ecd2a9
+		close(fd);
ecd2a9
+		return;
ecd2a9
+	}
ecd2a9
 	memset(c, 0, sizeof(*c));
ecd2a9
 	c->fd = fd;
ecd2a9
 	c->next = clients;
ecd2a9
@@ -124,11 +128,18 @@ void * uxsock_listen(int (*uxsock_trigge
ecd2a9
 	sigdelset(&mask, SIGHUP);
ecd2a9
 	sigdelset(&mask, SIGUSR1);
ecd2a9
 	while (1) {
ecd2a9
+		struct pollfd *tmp;
ecd2a9
 		struct client *c;
ecd2a9
 		int i, poll_count;
ecd2a9
 
ecd2a9
 		/* setup for a poll */
ecd2a9
-		polls = REALLOC(polls, (1+num_clients) * sizeof(*polls));
ecd2a9
+		tmp = REALLOC(polls, (1+num_clients) * sizeof(*polls));
ecd2a9
+		/* If we can't allocate poliing space for the new client,
ecd2a9
+		 * close it */
ecd2a9
+		if (!tmp)
ecd2a9
+			dead_client(clients);
ecd2a9
+		else
ecd2a9
+			polls = tmp;
ecd2a9
 		polls[0].fd = ux_sock;
ecd2a9
 		polls[0].events = POLLIN;
ecd2a9