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

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