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