[Asterisk-code-review] term: truncate the message rather than the escapes. (asterisk[master])

Jaco Kroon asteriskteam at digium.com
Thu Oct 28 09:24:49 CDT 2021


Jaco Kroon has uploaded this change for review. ( https://gerrit.asterisk.org/c/asterisk/+/16640 )


Change subject: term: truncate the message rather than the escapes.
......................................................................

term: truncate the message rather than the escapes.

This is a two edged sword.  Without this we end up potentially odd
escape sequences (which has been seen to cause screen corruption through
long function names in ast_debug), with this we change the meaning of
the message (but truncation could happen in any case).

Change-Id: I80ef7a4bfd2947e090ef830143391d11baebdb0d
Signed-off-by: Jaco Kroon <jaco at uls.co.za>
---
M main/term.c
1 file changed, 12 insertions(+), 2 deletions(-)



  git pull ssh://gerrit.asterisk.org:29418/asterisk refs/changes/40/16640/1

diff --git a/main/term.c b/main/term.c
index be10163..cbe3c40 100644
--- a/main/term.c
+++ b/main/term.c
@@ -235,6 +235,8 @@
 char *term_color(char *outbuf, const char *inbuf, int fgcolor, int bgcolor, int maxout)
 {
 	int attr = 0;
+	char *tail_out;
+	int p, tl;
 
 	if (!vt100compat) {
 		ast_copy_string(outbuf, inbuf, maxout);
@@ -262,10 +264,18 @@
 		if (!bgcolor) {
 			bgcolor = COLOR_BLACK;
 		}
-		snprintf(outbuf, maxout, "%c[%d;%d;%dm%s%s", ESC, attr, fgcolor, bgcolor + 10, inbuf, term_end());
+		p = snprintf(outbuf, maxout, "%c[%d;%d;%dm", ESC, attr, fgcolor, bgcolor + 10);
 	} else {
-		snprintf(outbuf, maxout, "%c[%d;%dm%s%s", ESC, attr, fgcolor, inbuf, term_end());
+		p = snprintf(outbuf, maxout, "%c[%d;%dm", ESC, attr, fgcolor);
 	}
+	tail_out = term_end();
+	tl = strlen(tail_out);
+	if (maxout <= p + tl) {
+		/* not even the ANSI sequences will fit in the buffer */
+		ast_copy_string(outbuf, inbuf, maxout);
+		return outbuf;
+	}
+	snprintf(outbuf + p, maxout - p, "%.*s%s", maxout - p - tl, inbuf, tail_out);
 	return outbuf;
 }
 

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

Gerrit-Project: asterisk
Gerrit-Branch: master
Gerrit-Change-Id: I80ef7a4bfd2947e090ef830143391d11baebdb0d
Gerrit-Change-Number: 16640
Gerrit-PatchSet: 1
Gerrit-Owner: Jaco Kroon <jaco at uls.co.za>
Gerrit-MessageType: newchange
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.digium.com/pipermail/asterisk-code-review/attachments/20211028/638dcee2/attachment-0001.html>


More information about the asterisk-code-review mailing list