[Asterisk-code-review] term.c: Truncate the message rather than the escape sequences. (asterisk[master])

Sean Bright asteriskteam at digium.com
Thu Nov 11 12:09:43 CST 2021


Sean Bright has uploaded this change for review. ( https://gerrit.asterisk.org/c/asterisk/+/17333 )


Change subject: term.c: Truncate the message rather than the escape sequences.
......................................................................

term.c: Truncate the message rather than the escape sequences.

Change-Id: Ie460acf577faeb4a6d74062e22ea9f3ffd3233de
---
M main/term.c
1 file changed, 22 insertions(+), 5 deletions(-)



  git pull ssh://gerrit.asterisk.org:29418/asterisk refs/changes/33/17333/1

diff --git a/main/term.c b/main/term.c
index be10163..72fdb6c 100644
--- a/main/term.c
+++ b/main/term.c
@@ -43,6 +43,7 @@
 static int vt100compat;
 
 static char enddata[80] = "";
+static int enddata_len = 0;
 static char quitdata[80] = "";
 
 static const char * const termpath[] = {
@@ -221,11 +222,11 @@
 	if (vt100compat) {
 		/* Make commands show up in nice colors */
 		if (ast_opt_light_background) {
-			snprintf(enddata, sizeof(enddata), "%c[%dm", ESC, COLOR_BLACK);
+			enddata_len = snprintf(enddata, sizeof(enddata), "%c[%dm", ESC, COLOR_BLACK);
 		} else if (ast_opt_force_black_background) {
-			snprintf(enddata, sizeof(enddata), "%c[%d;%d;%dm", ESC, ATTR_RESET, COLOR_WHITE, COLOR_BLACK + 10);
+			enddata_len = snprintf(enddata, sizeof(enddata), "%c[%d;%d;%dm", ESC, ATTR_RESET, COLOR_WHITE, COLOR_BLACK + 10);
 		} else {
-			snprintf(enddata, sizeof(enddata), "%c[%dm", ESC, ATTR_RESET);
+			enddata_len = snprintf(enddata, sizeof(enddata), "%c[%dm", ESC, ATTR_RESET);
 		}
 		snprintf(quitdata, sizeof(quitdata), "%c[%dm", ESC, ATTR_RESET);
 	}
@@ -235,6 +236,7 @@
 char *term_color(char *outbuf, const char *inbuf, int fgcolor, int bgcolor, int maxout)
 {
 	int attr = 0;
+	int written = 0;
 
 	if (!vt100compat) {
 		ast_copy_string(outbuf, inbuf, maxout);
@@ -262,10 +264,25 @@
 		if (!bgcolor) {
 			bgcolor = COLOR_BLACK;
 		}
-		snprintf(outbuf, maxout, "%c[%d;%d;%dm%s%s", ESC, attr, fgcolor, bgcolor + 10, inbuf, term_end());
+		written = snprintf(outbuf, maxout, "%c[%d;%d;%dm%s%s", ESC, attr, fgcolor, bgcolor + 10, inbuf, term_end());
 	} else {
-		snprintf(outbuf, maxout, "%c[%d;%dm%s%s", ESC, attr, fgcolor, inbuf, term_end());
+		written = snprintf(outbuf, maxout, "%c[%d;%dm%s%s", ESC, attr, fgcolor, inbuf, term_end());
 	}
+
+	if (written >= maxout) {
+		/* We truncated so stamp out some trailing bytes with our enddata */
+		int offset = maxout - enddata_len - 1;
+
+		if (offset < 0) {
+			/* If we get here, the passed buffer is comically undersized, so just
+			   gracefully bail out */
+			ast_copy_string(outbuf, inbuf, maxout);
+			return outbuf;
+		}
+
+		memcpy(&outbuf[offset], enddata, enddata_len + 1);
+	}
+
 	return outbuf;
 }
 

-- 
To view, visit https://gerrit.asterisk.org/c/asterisk/+/17333
To unsubscribe, or for help writing mail filters, visit https://gerrit.asterisk.org/settings

Gerrit-Project: asterisk
Gerrit-Branch: master
Gerrit-Change-Id: Ie460acf577faeb4a6d74062e22ea9f3ffd3233de
Gerrit-Change-Number: 17333
Gerrit-PatchSet: 1
Gerrit-Owner: Sean Bright <sean at seanbright.com>
Gerrit-MessageType: newchange
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.digium.com/pipermail/asterisk-code-review/attachments/20211111/68e66cbf/attachment.html>


More information about the asterisk-code-review mailing list