Skip to content

Commit

Permalink
Fix graph in panels rendering
Browse files Browse the repository at this point in the history
Fix use after free in canvas resize

Fix utf8 working only in leftmost panel
  • Loading branch information
cyanpencil committed Jun 20, 2018
1 parent 3feb950 commit fcc0f04
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 5 deletions.
36 changes: 31 additions & 5 deletions libr/cons/canvas.c
Expand Up @@ -114,7 +114,7 @@ static void stamp_attr(RConsCanvas *c, int loc, int length) {
s = attr_at (c, loc);

if (s) {
if (strlen (*s) > 2 && *(*s + 2) == '0') {
if (*s != 0 && strlen (*s) > 2 && *(*s + 2) == '0') {
if (strlen (c->attr) == 5 && *(c->attr + 2) != '0') {
char tmp[9];
memcpy (tmp, c->attr, 2);
Expand All @@ -135,7 +135,7 @@ static void stamp_attr(RConsCanvas *c, int loc, int length) {
for (i = 1; i < length; i++) {
s = attr_at (c, loc + i);
if (s) {
*s = Color_RESET;
*s = 0;
}
}
}
Expand Down Expand Up @@ -285,10 +285,13 @@ static int bytes_utf8len(const char *s, int n, int left) {
}
i++;
}
return i - 1;
return n == -1 ? i - 1 : i;
}

static int expand_line (RConsCanvas *c, int real_len, int utf8_len) {
if (real_len == 0) {
return true;
}
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 All @@ -311,6 +314,13 @@ static int expand_line (RConsCanvas *c, int real_len, int utf8_len) {
return false;
}
memcpy (tmp, start, size);
if (padding < 0) {
int lap = R_MAX (0, c->b[c->y] - (start + padding));
memcpy(start + padding + lap, tmp + lap, size - lap);
free (tmp);
c->blen[c->y] += padding;
return true;
}
memcpy (start + padding, tmp, size);
free (tmp);
c->blen[c->y] += padding;
Expand Down Expand Up @@ -339,8 +349,15 @@ R_API void r_cons_canvas_write(RConsCanvas *c, const char *s) {
if (piece_len == 0 && ch == '\0' && s_part == s) {
break;
}
left = c->blen[c->y] - c->x;
slen = R_MIN (left, piece_len);
left = c->blen[c->y] - c->x;
slen = piece_len;

int utf8_piece_len = utf8len_fixed (s_part, piece_len);
if (piece_len > left) {
if (utf8_piece_len > c->w - attr_x) {
slen = left;
}
}

int real_len = r_str_nlen (s_part, slen);
int utf8_len = utf8len_fixed (s_part, slen);
Expand All @@ -349,6 +366,12 @@ R_API void r_cons_canvas_write(RConsCanvas *c, const char *s) {
break;
}

/*if (slen < 10) */
/*eprintf ("write at (%d, %d)[%d, %d], slen %d, utf8size %d, left %d, size %d, orig_x %d, c->w %d, piece_len %d, attr_x %d %.5s \n", c->x, c->y, c->sx, c->sy, slen, utf8_piece_len, left, c->bsize[c->y], orig_x, c->w, piece_len, attr_x, s_part);*/
/*eprintf ("[%d, ] %d %d %s\n", c->sx, piece_len, attr_x,s_part);*/

/*slen = R_MIN (slen, left);*/

if (G (c->x - c->sx, c->y - c->sy)) {
memcpy (c->b[c->y] + c->x, s_part, slen);
}
Expand Down Expand Up @@ -399,6 +422,9 @@ R_API char *r_cons_canvas_to_string(RConsCanvas *c) {
for (y = 0; y < c->h; y++) {
if (!is_first) {
o[olen++] = '\n';
int len = strlen (Color_RESET);
memcpy (o + olen, Color_RESET, len);
olen += len;
}
is_first = false;
attr_x = 0;
Expand Down
3 changes: 3 additions & 0 deletions libr/core/panels.c
Expand Up @@ -725,6 +725,9 @@ R_API void r_core_panels_refresh(RCore *core) {
}

static void doPanelsRefresh(RCore *core) {
if (!core->panels) {
return;
}
core->panels->isResizing = true;
r_core_panels_layout (core->panels);
r_core_panels_refresh (core);
Expand Down

0 comments on commit fcc0f04

Please sign in to comment.