Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix heap overflow in canvas.c + fix utf8 wrong crop in panels #10336

Merged
merged 1 commit into from Jun 13, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
37 changes: 16 additions & 21 deletions libr/cons/canvas.c
Expand Up @@ -57,7 +57,6 @@ static int _get_piece(const char *p, char *chr) {
return p - q;
}

//XXX todo
static const char **attr_at(RConsCanvas *c, int loc) {
int i, j, delta;
if (!c->color || c->attrslen == 0) {
Expand Down Expand Up @@ -94,7 +93,6 @@ static const char **attr_at(RConsCanvas *c, int loc) {
return NULL;
}

//XXX todo
static void sort_attrs(RConsCanvas *c) {
int i, j;
RConsCanvasAttr value;
Expand All @@ -107,7 +105,6 @@ static void sort_attrs(RConsCanvas *c) {
}
}

//XXX todo
static void stamp_attr(RConsCanvas *c, int loc, int length) {
int i;
const char **s;
Expand All @@ -131,7 +128,6 @@ static void stamp_attr(RConsCanvas *c, int loc, int length) {
}
}

//XXX todo
/* check for ANSI sequences and use them as attr */
static const char *set_attr(RConsCanvas *c, const char *s) {
if (!c || !s) {
Expand Down Expand Up @@ -168,26 +164,26 @@ R_API bool r_cons_canvas_gotoxy(RConsCanvas *c, int x, int y) {
y += c->sy;
x += c->sx;

if (x > c->blen[y] * 2) {
return false;
}
if (y > c->h * 2) {
return false;
}
if (x >= c->blen[y]) {
c->x = c->blen[y];
if (y >= c->h) {
y = c->h - 1;
ret = false;
}
if (y >= c->h) {
c->y = c->h;
if (y < 0) {
y = 0;
ret = false;
}
if (x < 0) {
//c->x = 0;
ret = false;
}
if (y < 0) {
c->y = 0;
if (x > c->blen[y] * 2) {
return false;
}
if (x >= c->blen[y]) {
c->x = c->blen[y];
ret = false;
}
if (x < c->blen[y] && x >= 0) {
Expand Down Expand Up @@ -269,19 +265,19 @@ static int utf8len_fixed(const char *s, int n) {
return j;
}

static int bytes_utf8len(const char *s, int n) {
static int bytes_utf8len(const char *s, int n, int left) {
int i = 0;
while (s[i] && n > 0) {
while (s[i] && n > -1 && i < left) {
if ((s[i] & 0xc0) != 0x80) {
n--;
}
i++;
}
return i;
return i - 1;
}

static int expand_line (RConsCanvas *c, int real_len, int utf8_len) {
int buf_utf8_len = bytes_utf8len (c->b[c->y] + c->x, utf8_len);
int buf_utf8_len = bytes_utf8len (c->b[c->y] + c->x, utf8_len, c->blen[c->y] - c->x);
int goback = R_MAX (0, (buf_utf8_len - real_len));
int padding = (real_len - utf8_len) - goback;

Expand Down Expand Up @@ -320,8 +316,7 @@ R_API void r_cons_canvas_write(RConsCanvas *c, const char *s) {
int left, slen, attr_len, piece_len;
int orig_x = c->x, attr_x = c->x;

int x_padding = c->x - utf8len_fixed (c->b[c->y], c->x);
c->x += x_padding;
c->x = bytes_utf8len (c->b[c->y], c->x, c->blen[c->y]);

/* split the string into pieces of non-ANSI chars and print them normally,
** using the ANSI chars to set the attr of the canvas */
Expand All @@ -336,6 +331,7 @@ R_API void r_cons_canvas_write(RConsCanvas *c, const char *s) {
left = c->blen[c->y] - c->x;
slen = R_MIN (left, piece_len);
attr_len = slen <= 0 && s_part != s? 1: slen;

if (attr_len > 0 && attr_x < c->blen[c->y]) {
stamp_attr (c, c->y*c->w + attr_x, attr_len);
}
Expand All @@ -358,8 +354,7 @@ R_API void r_cons_canvas_write(RConsCanvas *c, const char *s) {
if (*s == '\0' || c->y >= c->h) {
break;
}
x_padding = utf8len_fixed (c->b[c->y], orig_x);
c->x = 2*orig_x - x_padding;
c->x = bytes_utf8len (c->b[c->y], orig_x, c->blen[c->y]);
attr_x = orig_x;
} else {
c->x += slen;
Expand Down
4 changes: 2 additions & 2 deletions libr/core/panels.c
Expand Up @@ -193,15 +193,15 @@ static void panelPrint(RCore *core, RConsCanvas *can, RPanel *panel, int color)
}
white[idx] = 0;
text = r_str_ansi_crop (cmdStr,
0, delta_y, panel->w + delta_x - 2, panel->h - 2 + delta_y);
0, delta_y, panel->w + delta_x - 3, panel->h - 2 + delta_y);
char *newText = r_str_prefix_all (text, white);
if (newText) {
free (text);
text = newText;
}
} else {
text = r_str_ansi_crop (cmdStr,
delta_x, delta_y, panel->w + delta_x - 2, panel->h - 2 + delta_y);
delta_x, delta_y, panel->w + delta_x - 3, panel->h - 2 + delta_y);
}
if (text) {
r_cons_canvas_write (can, text);
Expand Down
4 changes: 4 additions & 0 deletions libr/util/str.c
Expand Up @@ -1552,6 +1552,10 @@ R_API char *r_str_ansi_crop(const char *str, ut32 x, ut32 y, ut32 x2, ut32 y2) {
cw = 0;
} else {
if (ch >= y && ch < y2) {
if ((*str & 0xc0) == 0x80) {
*r++ = *str++;
continue;
}
if (*str == 0x1b && *(str + 1) == '[') {
const char *ptr = str;
if ((r_end - r) > 2) {
Expand Down