From 151f1205ec5aeaed68c69c7693b34dbe703e40dc Mon Sep 17 00:00:00 2001 From: Michael Ortmann <41313082+michaelortmann@users.noreply.github.com> Date: Mon, 19 Oct 2020 15:21:58 +0200 Subject: [PATCH 1/2] Refactor dccutil.c:add_cr() and dcc.c:escape_telnet() into a single misc.c:add_cr() --- src/dcc.c | 20 ++------------------ src/dccutil.c | 17 +---------------- src/misc.c | 23 +++++++++++++++++++++++ src/proto.h | 2 +- 4 files changed, 27 insertions(+), 35 deletions(-) diff --git a/src/dcc.c b/src/dcc.c index b72221038..d337dd475 100644 --- a/src/dcc.c +++ b/src/dcc.c @@ -90,22 +90,6 @@ static int detect_telnet(unsigned char *buf) return 0; } -/* Escape telnet IAC and prepend CR to LF */ -static char *escape_telnet(char *s) -{ - static char buf[1024]; - char *p; - - for (p = buf; *s && (p < (buf + sizeof(buf) - 2)); *p++ = *s++) - if ((unsigned char) *s == TLN_IAC) - *p++ = *s; - else if (*s == '\n') - *p++ = '\r'; - *p = 0; - - return buf; -} - static void strip_telnet(int sock, char *buf, int *len) { unsigned char *p = (unsigned char *) buf, *o = (unsigned char *) buf; @@ -929,7 +913,7 @@ static void out_dcc_general(int idx, char *buf, void *x) strip_mirc_codes(p->strip_flags, buf); if (dcc[idx].status & STAT_TELNET) - y = escape_telnet(buf); + y = add_cr(buf, 1); if (dcc[idx].status & STAT_PAGE) append_line(idx, y); else @@ -1768,7 +1752,7 @@ static void dcc_telnet_pass(int idx, int atr) /* Turn off remote telnet echo (send IAC WILL ECHO). */ if (dcc[idx].status & STAT_TELNET) { char buf[1030]; - egg_snprintf(buf, sizeof buf, "\n%s%s\r\n", escape_telnet(DCC_ENTERPASS), + egg_snprintf(buf, sizeof buf, "\n%s%s\r\n", add_cr(DCC_ENTERPASS, 1), TLN_IAC_C TLN_WILL_C TLN_ECHO_C); tputs(dcc[idx].sock, buf, strlen(buf)); } else diff --git a/src/dccutil.c b/src/dccutil.c index c9fca244e..a05b1dd56 100644 --- a/src/dccutil.c +++ b/src/dccutil.c @@ -132,21 +132,6 @@ int findanyidx(int z) return -1; } -/* Replace \n with \r\n */ -char *add_cr(char *buf) -{ - static char WBUF[1024]; - char *p, *q; - - for (p = buf, q = WBUF; *p; p++, q++) { - if (*p == '\n') - *q++ = '\r'; - *q = *p; - } - *q = *p; - return WBUF; -} - extern void (*qserver) (int, char *, int); void dprintf EGG_VARARGS_DEF(int, arg1) @@ -208,7 +193,7 @@ void dprint(int idx, char *buf, int len) len = LOGLINEMAX-10; } if (dcc[idx].type && ((long) (dcc[idx].type->output) == 1)) { - char *p = add_cr(buf); + char *p = add_cr(buf, 0); tputs(dcc[idx].sock, p, strlen(p)); } else if (dcc[idx].type && dcc[idx].type->output) diff --git a/src/misc.c b/src/misc.c index 256f654e2..a22ab8641 100644 --- a/src/misc.c +++ b/src/misc.c @@ -1575,3 +1575,26 @@ void kill_bot(char *s1, char *s2) write_userfile(-1); fatal(s2, 2); } + +/* Prepend CR to LF (Replace \n with \r\n) and escape telnet IAC if + * escape_telnet is set + */ +char *add_cr(const char *p, const int escape_telnet) +{ + static int buf_size = 0; + static char *buf; + char *q; + + if ((strlen(p) << 1) > buf_size) { + buf_size = strlen(p) << 1; + buf = nrealloc(buf, buf_size); + } + for (q = buf; *p; *q++ = *p++) { + if (escape_telnet && ((unsigned char) *p == TLN_IAC)) + *q++ = *p; + else if (*p == '\n') + *q++ = '\r'; + } + *q = 0; + return buf; +} diff --git a/src/proto.h b/src/proto.h index cc394e709..18d9c1b4e 100644 --- a/src/proto.h +++ b/src/proto.h @@ -137,7 +137,6 @@ int dcc_fingerprint(int); int increase_socks_max(); int findidx(int); int findanyidx(int); -char *add_cr(char *); void dprint(int, char *, int); void dprintf EGG_VARARGS(int, arg1); void chatout EGG_VARARGS(char *, arg1); @@ -261,6 +260,7 @@ char *strchr_unescape(char *, const char, const char); void str_unescape(char *, const char); int str_isdigit(const char *); void kill_bot(char *, char *); +char *add_cr(const char *, const int); void maskaddr(const char *, char *, int); #define maskhost(a,b) maskaddr((a),(b),3) From 51fea86d92806696e6dc47ed2d5afa69274889e6 Mon Sep 17 00:00:00 2001 From: Michael Ortmann <41313082+michaelortmann@users.noreply.github.com> Date: Wed, 10 Jul 2024 19:20:36 +0200 Subject: [PATCH 2/2] Fix off-by-one error --- src/misc.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/misc.c b/src/misc.c index 5e5e7d3e0..f51d1af75 100644 --- a/src/misc.c +++ b/src/misc.c @@ -1596,12 +1596,14 @@ int crypto_verify(const char *x_, const char *y_) */ char *add_cr(const char *p, const int escape_telnet) { - static int buf_size = 0; + size_t len; + static size_t buf_size = 0; static char *buf; char *q; - if ((strlen(p) << 1) > buf_size) { - buf_size = strlen(p) << 1; + len = (strlen(p) << 1) + 1; + if (len > buf_size) { + buf_size = len; buf = nrealloc(buf, buf_size); } for (q = buf; *p; *q++ = *p++) {