Blame SOURCES/0145-UP-resize-help-msg.patch

4ae388
diff --git a/multipathd/cli.c b/multipathd/cli.c
4ae388
index acc4249..8d26956 100644
4ae388
--- a/multipathd/cli.c
4ae388
+++ b/multipathd/cli.c
4ae388
@@ -320,52 +320,90 @@ alloc_handlers (void)
4ae388
 }
4ae388
 
4ae388
 static int
4ae388
-genhelp_sprint_aliases (char * reply, vector keys, struct key * refkw)
4ae388
+genhelp_sprint_aliases (char * reply, int maxlen, vector keys,
4ae388
+			struct key * refkw)
4ae388
 {
4ae388
-	int i, fwd = 0;
4ae388
+	int i, len = 0;
4ae388
 	struct key * kw;
4ae388
 
4ae388
-	vector_foreach_slot (keys, kw, i)
4ae388
-		if (kw->code == refkw->code && kw != refkw)
4ae388
-			fwd += sprintf(reply, "|%s", kw->str);
4ae388
+	vector_foreach_slot (keys, kw, i) {
4ae388
+		if (kw->code == refkw->code && kw != refkw) {
4ae388
+			len += snprintf(reply + len, maxlen - len,
4ae388
+					"|%s", kw->str);
4ae388
+			if (len >= maxlen)
4ae388
+				return len;
4ae388
+		}
4ae388
+	}
4ae388
 
4ae388
-	return fwd;
4ae388
+	return len;
4ae388
 }
4ae388
 
4ae388
-static char *
4ae388
-genhelp_handler (void)
4ae388
-{
4ae388
+static int
4ae388
+do_genhelp(char *reply, int maxlen) {
4ae388
+	int len = 0;
4ae388
 	int i, j;
4ae388
 	unsigned long fp;
4ae388
 	struct handler * h;
4ae388
 	struct key * kw;
4ae388
-	char * reply;
4ae388
-	char * p;
4ae388
-
4ae388
-	reply = MALLOC(INITIAL_REPLY_LEN);
4ae388
 
4ae388
-	if (!reply)
4ae388
-		return NULL;
4ae388
-
4ae388
-	p = reply;
4ae388
-	p += sprintf(p, VERSION_STRING);
4ae388
-	p += sprintf(p, "CLI commands reference:\n");
4ae388
+	len += snprintf(reply + len, maxlen - len, VERSION_STRING);
4ae388
+	if (len >= maxlen)
4ae388
+		goto out;
4ae388
+	len += snprintf(reply + len, maxlen - len, "CLI commands reference:\n");
4ae388
+	if (len >= maxlen)
4ae388
+		goto out;
4ae388
 
4ae388
 	vector_foreach_slot (handlers, h, i) {
4ae388
 		fp = h->fingerprint;
4ae388
 		vector_foreach_slot (keys, kw, j) {
4ae388
 			if ((kw->code & fp)) {
4ae388
 				fp -= kw->code;
4ae388
-				p += sprintf(p, " %s", kw->str);
4ae388
-				p += genhelp_sprint_aliases(p, keys, kw);
4ae388
-
4ae388
-				if (kw->has_param)
4ae388
-					p += sprintf(p, " $%s", kw->str);
4ae388
+				len += snprintf(reply + len , maxlen - len,
4ae388
+						" %s", kw->str);
4ae388
+				if (len >= maxlen)
4ae388
+					goto out;
4ae388
+				len += genhelp_sprint_aliases(reply + len,
4ae388
+							      maxlen - len,
4ae388
+							      keys, kw);
4ae388
+				if (len >= maxlen)
4ae388
+					goto out;
4ae388
+
4ae388
+				if (kw->has_param) {
4ae388
+					len += snprintf(reply + len,
4ae388
+							maxlen - len,
4ae388
+							" $%s", kw->str);
4ae388
+					if (len >= maxlen)
4ae388
+						goto out;
4ae388
+				}
4ae388
 			}
4ae388
 		}
4ae388
-		p += sprintf(p, "\n");
4ae388
+		len += snprintf(reply + len, maxlen - len, "\n");
4ae388
+		if (len >= maxlen)
4ae388
+			goto out;
4ae388
 	}
4ae388
+out:
4ae388
+	return len;
4ae388
+}
4ae388
+
4ae388
 
4ae388
+static char *
4ae388
+genhelp_handler (void)
4ae388
+{
4ae388
+	char * reply;
4ae388
+	char * p = NULL;
4ae388
+	int maxlen = INITIAL_REPLY_LEN;
4ae388
+	int again = 1;
4ae388
+
4ae388
+	reply = MALLOC(maxlen);
4ae388
+
4ae388
+	while (again) {
4ae388
+		if (!reply)
4ae388
+			return NULL;
4ae388
+		p = reply;
4ae388
+		p += do_genhelp(reply, maxlen);
4ae388
+		again = ((p - reply) >= maxlen);
4ae388
+		REALLOC_REPLY(reply, again, maxlen);
4ae388
+	}
4ae388
 	return reply;
4ae388
 }
4ae388
 
4ae388
diff --git a/multipathd/cli.h b/multipathd/cli.h
4ae388
index 09fdc68..2e0e1da 100644
4ae388
--- a/multipathd/cli.h
4ae388
+++ b/multipathd/cli.h
4ae388
@@ -71,7 +71,21 @@ enum {
4ae388
 #define SETPRSTATUS	(1UL << __SETPRSTATUS)
4ae388
 #define UNSETPRSTATUS	(1UL << __UNSETPRSTATUS)
4ae388
 
4ae388
-#define INITIAL_REPLY_LEN	1100
4ae388
+#define INITIAL_REPLY_LEN	1200
4ae388
+
4ae388
+#define REALLOC_REPLY(r, a, m)					\
4ae388
+	do {							\
4ae388
+		if ((a)) {					\
4ae388
+			char *tmp = (r);			\
4ae388
+			(r) = REALLOC((r), (m) * 2);		\
4ae388
+			if ((r)) {				\
4ae388
+				memset((r) + (m), 0, (m));	\
4ae388
+				(m) *= 2;			\
4ae388
+			}					\
4ae388
+			else					\
4ae388
+				free(tmp);			\
4ae388
+		}						\
4ae388
+	} while (0)
4ae388
 
4ae388
 struct key {
4ae388
 	char * str;
4ae388
diff --git a/multipathd/cli_handlers.c b/multipathd/cli_handlers.c
4ae388
index e47899a..23683f2 100644
4ae388
--- a/multipathd/cli_handlers.c
4ae388
+++ b/multipathd/cli_handlers.c
4ae388
@@ -23,20 +23,6 @@
4ae388
 #include "cli.h"
4ae388
 #include "uevent.h"
4ae388
 
4ae388
-#define REALLOC_REPLY(r, a, m)					\
4ae388
-	do {							\
4ae388
-		if ((a)) {					\
4ae388
-			char *tmp = (r);			\
4ae388
-			(r) = REALLOC((r), (m) * 2);		\
4ae388
-			if ((r)) {				\
4ae388
-				memset((r) + (m), 0, (m));	\
4ae388
-				(m) *= 2;			\
4ae388
-			}					\
4ae388
-			else					\
4ae388
-				free(tmp);			\
4ae388
-		}						\
4ae388
-	} while (0)
4ae388
-
4ae388
 int
4ae388
 show_paths (char ** r, int * len, struct vectors * vecs, char * style)
4ae388
 {