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

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