Blame SOURCES/telnet-0.17-CAN-2005-468_469.patch

fbcb50
--- netkit-telnet-0.17/telnet/telnet.c.CAN-2005-468_469	2005-03-17 13:48:58.000000000 +0100
fbcb50
+++ netkit-telnet-0.17/telnet/telnet.c	2005-03-17 14:02:27.000000000 +0100
fbcb50
@@ -1310,22 +1310,66 @@
fbcb50
 }
fbcb50
 
fbcb50
 
fbcb50
-unsigned char slc_reply[128];
fbcb50
+#define SLC_REPLY_SIZE 128
fbcb50
+unsigned char *slc_reply;
fbcb50
 unsigned char *slc_replyp;
fbcb50
+unsigned char *slc_replyend;
fbcb50
 
fbcb50
 	void
fbcb50
 slc_start_reply(void)
fbcb50
 {
fbcb50
+        slc_reply = (unsigned char *)malloc(SLC_REPLY_SIZE);
fbcb50
+        if (slc_reply == NULL) {
fbcb50
+/*@*/           printf("slc_start_reply: malloc()/realloc() failed!!!\n");
fbcb50
+                slc_reply = slc_replyp = slc_replyend = NULL;
fbcb50
+                return;
fbcb50
+	}
fbcb50
+
fbcb50
 	slc_replyp = slc_reply;
fbcb50
+	slc_replyend = slc_reply + SLC_REPLY_SIZE;
fbcb50
 	*slc_replyp++ = IAC;
fbcb50
 	*slc_replyp++ = SB;
fbcb50
 	*slc_replyp++ = TELOPT_LINEMODE;
fbcb50
 	*slc_replyp++ = LM_SLC;
fbcb50
 }
fbcb50
 
fbcb50
+static int
fbcb50
+slc_assure_buffer(int want_len);
fbcb50
+
fbcb50
+	static int
fbcb50
+slc_assure_buffer(int want_len)
fbcb50
+{
fbcb50
+        if ((slc_replyp + want_len) >= slc_replyend) {
fbcb50
+                int len;
fbcb50
+		int old_len = slc_replyp - slc_reply;
fbcb50
+		unsigned char *p;
fbcb50
+
fbcb50
+                len = old_len
fbcb50
+			+ (want_len / SLC_REPLY_SIZE + 1) * SLC_REPLY_SIZE;
fbcb50
+                p = (unsigned char *)realloc(slc_reply, len);
fbcb50
+                if (p == NULL)
fbcb50
+                        free(slc_reply);
fbcb50
+                slc_reply = p;
fbcb50
+                if (slc_reply == NULL) {
fbcb50
+/*@*/                   printf("slc_add_reply: realloc() failed!!!\n");
fbcb50
+                        slc_reply = slc_replyp = slc_replyend = NULL;
fbcb50
+                        return 1;
fbcb50
+                }
fbcb50
+                slc_replyp = slc_reply + old_len;
fbcb50
+                slc_replyend = slc_reply + len;
fbcb50
+        }
fbcb50
+	return 0;
fbcb50
+}
fbcb50
+
fbcb50
 	void
fbcb50
 slc_add_reply(unsigned char func, unsigned char flags, cc_t value)
fbcb50
 {
fbcb50
+	if (slc_assure_buffer(6))
fbcb50
+		return;
fbcb50
+
fbcb50
+	if (slc_replyp == NULL)
fbcb50
+		return;
fbcb50
+
fbcb50
 	if ((*slc_replyp++ = func) == IAC)
fbcb50
 		*slc_replyp++ = IAC;
fbcb50
 	if ((*slc_replyp++ = flags) == IAC)
fbcb50
@@ -1339,6 +1383,12 @@
fbcb50
 {
fbcb50
     int len;
fbcb50
 
fbcb50
+    if (slc_assure_buffer(2))
fbcb50
+	return;
fbcb50
+
fbcb50
+    if (slc_replyp == NULL)
fbcb50
+	return;
fbcb50
+
fbcb50
     *slc_replyp++ = IAC;
fbcb50
     *slc_replyp++ = SE;
fbcb50
     len = slc_replyp - slc_reply;
fbcb50
@@ -1456,7 +1506,7 @@
fbcb50
 	}
fbcb50
 }
fbcb50
 
fbcb50
-#define	OPT_REPLY_SIZE	256
fbcb50
+#define	OPT_REPLY_SIZE	1024
fbcb50
 unsigned char *opt_reply;
fbcb50
 unsigned char *opt_replyp;
fbcb50
 unsigned char *opt_replyend;
fbcb50
@@ -1490,10 +1540,38 @@
fbcb50
 env_opt_start_info(void)
fbcb50
 {
fbcb50
 	env_opt_start();
fbcb50
-	if (opt_replyp)
fbcb50
+	if (opt_replyp && (opt_replyp > opt_reply))
fbcb50
 	    opt_replyp[-1] = TELQUAL_INFO;
fbcb50
 }
fbcb50
 
fbcb50
+static int
fbcb50
+env_opt_assure_buffer(int want_len);
fbcb50
+
fbcb50
+	static int
fbcb50
+env_opt_assure_buffer(int want_len)
fbcb50
+{
fbcb50
+        if ((opt_replyp + want_len) >= opt_replyend) {
fbcb50
+		int len;
fbcb50
+		unsigned char *p;
fbcb50
+		int old_len = opt_replyp - opt_reply;
fbcb50
+
fbcb50
+		len = old_len
fbcb50
+			+ (want_len / OPT_REPLY_SIZE + 1) * OPT_REPLY_SIZE;
fbcb50
+		p = (unsigned char *)realloc(opt_reply, len);
fbcb50
+		if (p == NULL)
fbcb50
+			free(opt_reply);
fbcb50
+		opt_reply = p;
fbcb50
+		if (opt_reply == NULL) {
fbcb50
+/*@*/			printf("env_opt_add: realloc() failed!!!\n");
fbcb50
+			opt_reply = opt_replyp = opt_replyend = NULL;
fbcb50
+			return 1;
fbcb50
+		}
fbcb50
+		opt_replyp = opt_reply + old_len;
fbcb50
+		opt_replyend = opt_reply + len;
fbcb50
+	}
fbcb50
+	return 0;
fbcb50
+}
fbcb50
+
fbcb50
 	void
fbcb50
 env_opt_add(unsigned char *ep)
fbcb50
 {
fbcb50
@@ -1515,25 +1593,12 @@
fbcb50
 		return;
fbcb50
 	}
fbcb50
 	vp = env_getvalue(ep, 1);
fbcb50
-	if (opt_replyp + (vp ? strlen((char *)vp) : 0) +
fbcb50
-				strlen((char *)ep) + 6 > opt_replyend)
fbcb50
-	{
fbcb50
-		int len;
fbcb50
-		unsigned char *p;
fbcb50
-		opt_replyend += OPT_REPLY_SIZE;
fbcb50
-		len = opt_replyend - opt_reply;
fbcb50
-		p = (unsigned char *)realloc(opt_reply, len);
fbcb50
-		if (p == NULL)
fbcb50
-			free(opt_reply);
fbcb50
-		opt_reply = p;
fbcb50
-		if (opt_reply == NULL) {
fbcb50
-/*@*/			printf("env_opt_add: realloc() failed!!!\n");
fbcb50
-			opt_reply = opt_replyp = opt_replyend = NULL;
fbcb50
-			return;
fbcb50
-		}
fbcb50
-		opt_replyp = opt_reply + len - (opt_replyend - opt_replyp);
fbcb50
-		opt_replyend = opt_reply + len;
fbcb50
-	}
fbcb50
+
fbcb50
+	/* use the double length in case it gots escaped */
fbcb50
+	if (env_opt_assure_buffer((vp ? strlen((char *)vp)*2 : 0) +
fbcb50
+				strlen((char *)ep)*2 + 6))
fbcb50
+		return;
fbcb50
+
fbcb50
 	if (opt_welldefined((char *)ep))
fbcb50
 #ifdef	OLD_ENVIRON
fbcb50
 		if (telopt_environ == TELOPT_OLD_ENVIRON)
fbcb50
@@ -1588,8 +1653,14 @@
fbcb50
 {
fbcb50
 	int len;
fbcb50
 
fbcb50
+        if (opt_reply == NULL)          /*XXX*/
fbcb50
+                return;                 /*XXX*/
fbcb50
+
fbcb50
+
fbcb50
 	len = opt_replyp - opt_reply + 2;
fbcb50
 	if (emptyok || len > 6) {
fbcb50
+		if (env_opt_assure_buffer(2))
fbcb50
+			return;
fbcb50
 		*opt_replyp++ = IAC;
fbcb50
 		*opt_replyp++ = SE;
fbcb50
 		if (NETROOM() > len) {