Make r_cons_rgb_str() safe (#9938)

This commit is contained in:
Florian Märkl 2018-04-21 11:31:24 +02:00 committed by radare
parent 4e1cf0d3e6
commit cee122e9df
6 changed files with 48 additions and 26 deletions

View File

@ -228,7 +228,7 @@ R_API char *r_cons_pal_parse (const char *str, RColor *outcol) {
if (!strcmp (fgcolor, "random")) {
rcolor = r_cons_color_random (ALPHA_FG);
if (!outcol) {
r_cons_rgb_str (out, &rcolor);
r_cons_rgb_str (out, sizeof (out), &rcolor);
}
} else if (!strncmp (fgcolor, "#", 1)) { // "#00ff00" HTML format
if (strlen (fgcolor) == 7) {
@ -237,7 +237,7 @@ R_API char *r_cons_pal_parse (const char *str, RColor *outcol) {
eprintf ("Error while parsing HTML color: %s\n", fgcolor);
}
if (!outcol) {
r_cons_rgb_str (out, &rcolor);
r_cons_rgb_str (out, sizeof (out), &rcolor);
}
} else {
eprintf ("Invalid html color code\n");
@ -248,14 +248,14 @@ R_API char *r_cons_pal_parse (const char *str, RColor *outcol) {
rcolor.g = rgbnum (fgcolor[5], '0');
rcolor.b = rgbnum (fgcolor[6], '0');
if (!outcol) {
r_cons_rgb_str (out, &rcolor);
r_cons_rgb_str (out, sizeof (out), &rcolor);
}
} else if (strlen (fgcolor) == 10) {
rcolor.r = rgbnum (fgcolor[4], fgcolor[5]);
rcolor.g = rgbnum (fgcolor[6], fgcolor[7]);
rcolor.b = rgbnum (fgcolor[8], fgcolor[9]);
if (!outcol) {
r_cons_rgb_str (out, &rcolor);
r_cons_rgb_str (out, sizeof (out), &rcolor);
}
}
}
@ -267,7 +267,8 @@ R_API char *r_cons_pal_parse (const char *str, RColor *outcol) {
rcolor.g2 = rgbnum (bgcolor[5], '0');
rcolor.b2 = rgbnum (bgcolor[6], '0');
if (!outcol) {
r_cons_rgb_str (out + strlen (out), &rcolor);
size_t len = strlen (out);
r_cons_rgb_str (out + len, sizeof (out) - len, &rcolor);
}
} else if (strlen (bgcolor) == 10) {
rcolor.a |= ALPHA_BG;
@ -275,7 +276,8 @@ R_API char *r_cons_pal_parse (const char *str, RColor *outcol) {
rcolor.g2 = rgbnum (bgcolor[6], bgcolor[7]);
rcolor.b2 = rgbnum (bgcolor[8], bgcolor[9]);
if (!outcol) {
r_cons_rgb_str (out + strlen (out), &rcolor);
size_t len = strlen (out);
r_cons_rgb_str (out + len, sizeof (out) - len, &rcolor);
}
}
}
@ -344,7 +346,7 @@ static void r_cons_pal_show_gs () {
if (i < 0x76) strcpy (fg, Color_WHITE);
else strcpy (fg, Color_BLACK);
r_cons_rgb_str (bg, &rcolor);
r_cons_rgb_str (bg, sizeof (bg), &rcolor);
r_cons_printf ("%s%s rgb:%02x%02x%02x "Color_RESET,
fg, bg, i, i, i);
if (n++ == 5) {
@ -377,7 +379,7 @@ static void r_cons_pal_show_256 () {
rc.g = 0x5f;
}
const char *fg = ((rc.r <= 0x5f) && (rc.g <= 0x5f)) ? Color_WHITE: Color_BLACK;
r_cons_rgb_str (bg, &rc);
r_cons_rgb_str (bg, sizeof (bg), &rc);
r_cons_printf ("%s%s rgb:%02x%02x%02x "
Color_RESET, fg, bg, rc.r, rc.g, rc.b);
}
@ -400,7 +402,7 @@ static void r_cons_pal_show_rgb () {
rc.b = k * 16;
strcpy (fg, ((i < 6) && (j < 5))
? Color_WHITE: Color_BLACK);
r_cons_rgb_str (bg, &rc);
r_cons_rgb_str (bg, sizeof (bg), &rc);
r_cons_printf ("%s%s rgb:%02x%02x%02x "
Color_RESET, fg, bg, rc.r, rc.g, rc.b);
if (n ++== 5) {
@ -544,7 +546,7 @@ R_API void r_cons_pal_update_event() {
if (*color) {
R_FREE (*color);
}
*color = r_cons_rgb_str (NULL, rcolor);
*color = r_cons_rgb_str (NULL, 0, rcolor);
const char *rgb = sdb_fmt ("rgb:%02x%02x%02x", rcolor->r, rcolor->g, rcolor->b);
sdb_set (db, rgb, "1", 0);
}

View File

@ -155,21 +155,33 @@ R_API int r_cons_rgb_parse(const char *p, ut8 *r, ut8 *g, ut8 *b, ut8 *a) {
return 1;
}
R_API char *r_cons_rgb_str_off(char *outstr, ut64 off) {
R_API char *r_cons_rgb_str_off(char *outstr, size_t sz, ut64 off) {
RColor rc = RColor_BLACK;
rc.r = (off >> 2) & 0xff;
rc.g = (off >> 6) & 0xff;
rc.b = (off >> 12) & 0xff;
return r_cons_rgb_str (outstr, &rc);
return r_cons_rgb_str (outstr, sz, &rc);
}
/* Compute color string depending on cons->color */
static void r_cons_rgb_gen (char *outstr, ut8 attr, ut8 a, ut8 r, ut8 g, ut8 b) {
static void r_cons_rgb_gen (char *outstr, size_t sz, ut8 attr, ut8 a, ut8 r, ut8 g, ut8 b) {
ut8 fgbg = (a == ALPHA_BG)? 48: 38; // ANSI codes for Background/Foreground
int i = 2;
if (sz < 4) { // must have at least room for "<esc>[m\0"
if (sz > 0) {
outstr[0] = '\0';
}
return;
}
size_t i = 2;
outstr[0] = '\x1b';
outstr[1] = '[';
for (; attr; attr &= attr - 1) {
if (sz < i + 4) { // must have at least room for e.g. "1;m\0"
outstr[0] = '\0';
return;
}
switch (attr & -attr) {
case 1u << 1: outstr[i] = '1'; break;
case 1u << 2: outstr[i] = '2'; break;
@ -180,12 +192,14 @@ static void r_cons_rgb_gen (char *outstr, ut8 attr, ut8 a, ut8 r, ut8 g, ut8 b)
outstr[i + 1] = ';';
i += 2;
}
int written = -1;
switch (r_cons_singleton ()->color) {
case COLOR_MODE_256: // 256 color palette
sprintf (outstr + i, "%d;5;%dm", fgbg, rgb (r, g, b));
written = snprintf (outstr + i, sz - i, "%d;5;%dm", fgbg, rgb (r, g, b));
break;
case COLOR_MODE_16M: // 16M (truecolor)
sprintf (outstr + i, "%d;2;%d;%d;%dm", fgbg, r, g, b);
written = snprintf (outstr + i, sz - i, "%d;2;%d;%d;%dm", fgbg, r, g, b);
break;
case COLOR_MODE_16: // ansi 16 colors
{
@ -195,19 +209,24 @@ static void r_cons_rgb_gen (char *outstr, ut8 attr, ut8 a, ut8 r, ut8 g, ut8 b)
g = (g >= k) ? 1 : 0;
b = (b >= k) ? 1 : 0;
k = (r ? 1 : 0) + (g ? (b ? 6 : 2) : (b ? 4 : 0));
sprintf (outstr + i, "%dm", fgbg + k);
written = snprintf (outstr + i, sz - i, "%dm", fgbg + k);
}
break;
}
if (written < 0 || written >= sz - i) {
outstr[0] = '\0';
}
}
/* Return the computed color string for the specified color */
R_API char *r_cons_rgb_str (char *outstr, RColor *rcolor) {
R_API char *r_cons_rgb_str (char *outstr, size_t sz, RColor *rcolor) {
if (!rcolor) {
return NULL;
}
if (!outstr) {
outstr = calloc (32, 1);
sz = 64;
outstr = calloc (sz, 1);
}
*outstr = 0;
if (rcolor->a == ALPHA_RESET) {
@ -216,10 +235,11 @@ R_API char *r_cons_rgb_str (char *outstr, RColor *rcolor) {
}
// If the color handles both foreground and background, also add background
if (rcolor->a == ALPHA_FGBG) {
r_cons_rgb_gen (outstr, 0, ALPHA_BG, rcolor->r2, rcolor->g2, rcolor->b2);
r_cons_rgb_gen (outstr, sz, 0, ALPHA_BG, rcolor->r2, rcolor->g2, rcolor->b2);
}
// APPEND
r_cons_rgb_gen (outstr + strlen (outstr), rcolor->attr, rcolor->a, rcolor->r, rcolor->g, rcolor->b);
size_t len = strlen (outstr);
r_cons_rgb_gen (outstr + len, sz - len, rcolor->attr, rcolor->a, rcolor->r, rcolor->g, rcolor->b);
return outstr;
}

View File

@ -452,7 +452,7 @@ static int cmd_eval(void *data, const char *input) {
} else {
char color[32];
RColor rcolor = r_cons_pal_get (p);
r_cons_rgb_str (color, &rcolor);
r_cons_rgb_str (color, sizeof (color), &rcolor);
eprintf ("(%s)(%sCOLOR"Color_RESET")\n", p, color);
}
free (p);

View File

@ -5644,7 +5644,7 @@ R_API void r_print_offset_sg(RPrint *p, ut64 off, int invert, int offseg, int se
char rgbstr[32];
const char *k = r_cons_singleton ()->pal.offset; // TODO etooslow. must cache
if (p->flags & R_PRINT_FLAGS_RAINBOW) {
k = r_cons_rgb_str_off (rgbstr, off);
k = r_cons_rgb_str_off (rgbstr, sizeof (rgbstr), off);
}
if (invert) {
r_cons_invert (true, true);

View File

@ -3166,7 +3166,7 @@ R_API void r_core_visual_colors(RCore *core) {
k = r_cons_pal_get_name (opt);
}
r_cons_gotoxy (0, 0);
r_cons_rgb_str (cstr, &rcolor);
r_cons_rgb_str (cstr, sizeof (cstr), &rcolor);
if (r_cons_singleton ()->color < COLOR_MODE_16M) {
rcolor.r &= 0xf;
rcolor.g &= 0xf;

View File

@ -738,8 +738,8 @@ R_API int r_cons_grepbuf(char *buf, int len);
R_API void r_cons_rgb(ut8 r, ut8 g, ut8 b, ut8 a);
R_API void r_cons_rgb_fgbg(ut8 r, ut8 g, ut8 b, ut8 R, ut8 G, ut8 B);
R_API void r_cons_rgb_init(void);
R_API char *r_cons_rgb_str(char *outstr, RColor *rcolor);
R_API char *r_cons_rgb_str_off(char *outstr, ut64 off);
R_API char *r_cons_rgb_str(char *outstr, size_t sz, RColor *rcolor);
R_API char *r_cons_rgb_str_off(char *outstr, size_t sz, ut64 off);
R_API void r_cons_color(int fg, int r, int g, int b);
R_API RColor r_cons_color_random(ut8 alpha);
R_API void r_cons_invert(int set, int color);