From 223a17e5fd7772631fe32fe412b17c54e501899b Mon Sep 17 00:00:00 2001 From: Matthew Fernandez Date: Sat, 28 Jan 2023 08:36:48 -0800 Subject: [PATCH] common fullColor: use an agxbuf instead of a static buffer This improves safety by removing an `sprintf` usage as well as removing a long lived static buffer that can prove problematic for tools like Valgrind and Address Sanitizer. Gitlab: #1950 --- lib/common/colxlate.c | 24 +++++++++--------------- 1 file changed, 9 insertions(+), 15 deletions(-) diff --git a/lib/common/colxlate.c b/lib/common/colxlate.c index 59132ef52..d4a967f21 100644 --- a/lib/common/colxlate.c +++ b/lib/common/colxlate.c @@ -159,18 +159,9 @@ char *canontoken(char *str) /* fullColor: * Return "/prefix/str" */ -static char* fullColor (char* prefix, char* str) -{ - static char *fulls; - static size_t allocated; - size_t len = strlen(prefix) + strlen(str) + 3; - - if (len >= allocated) { - allocated = len + 10; - fulls = grealloc(fulls, allocated); - } - sprintf (fulls, "/%s/%s", prefix, str); - return fulls; +static char *fullColor(agxbuf *xb, char *prefix, char *str) { + agxbprint(xb, "/%s/%s", prefix, str); + return agxbuse(xb); } /* resolveColor: @@ -218,13 +209,14 @@ static char* resolveColor (char* str) if (!strcmp(str, "black")) return str; if (!strcmp(str, "white")) return str; if (!strcmp(str, "lightgrey")) return str; + agxbuf xb = {0}; if (*str == '/') { /* if begins with '/' */ c2 = str+1; if ((ss = strchr(c2, '/'))) { /* if has second '/' */ if (*c2 == '/') { /* if second '/' is second character */ /* Do not compare against final '/' */ if (ISNONDFLT(colorscheme)) - s = fullColor (colorscheme, c2+1); + s = fullColor(&xb, colorscheme, c2+1); else s = c2+1; } @@ -233,9 +225,11 @@ static char* resolveColor (char* str) } else s = c2; } - else if (ISNONDFLT(colorscheme)) s = fullColor (colorscheme, str); + else if (ISNONDFLT(colorscheme)) s = fullColor(&xb, colorscheme, str); else s = str; - return canontoken(s); + s = canontoken(s); + agxbfree(&xb); + return s; } int colorxlate(char *str, gvcolor_t * color, color_type_t target_type) -- 2.40.0