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

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