=================================================================== RCS file: /cvsrepo/anoncvs/cvs/src/usr.bin/tmux/window-copy.c,v retrieving revision 1.288 retrieving revision 1.289 diff -u -r1.288 -r1.289 --- src/usr.bin/tmux/window-copy.c 2020/05/16 16:10:28 1.288 +++ src/usr.bin/tmux/window-copy.c 2020/05/25 09:32:10 1.289 @@ -1,4 +1,4 @@ -/* $OpenBSD: window-copy.c,v 1.288 2020/05/16 16:10:28 nicm Exp $ */ +/* $OpenBSD: window-copy.c,v 1.289 2020/05/25 09:32:10 nicm Exp $ */ /* * Copyright (c) 2007 Nicholas Marriott @@ -2551,23 +2551,33 @@ } static const char * -window_copy_cellstring(const struct grid_line *gl, u_int px, size_t *size) +window_copy_cellstring(const struct grid_line *gl, u_int px, size_t *size, + int *allocated) { + static struct utf8_data ud; struct grid_cell_entry *gce; + char *copy; if (px >= gl->cellsize) { *size = 1; + *allocated = 0; return (" "); } gce = &gl->celldata[px]; if (~gce->flags & GRID_FLAG_EXTENDED) { *size = 1; + *allocated = 0; return (&gce->data.data); } - *size = gl->extddata[gce->offset].data.size; - return (gl->extddata[gce->offset].data.data); + utf8_get_big(gl->extddata[gce->offset].data, &ud); + *size = ud.size; + *allocated = 1; + + copy = xmalloc(ud.size); + memcpy(copy, ud.data, ud.size); + return (copy); } /* Find last match in given range. */ @@ -2630,6 +2640,7 @@ const struct grid_line *gl; const char *d; size_t bufsize = 1024, dlen; + int allocated; while (bufsize < newsize) bufsize *= 2; @@ -2638,7 +2649,7 @@ gl = grid_peek_line(gd, py); bx = *size - 1; for (ax = first; ax < last; ax++) { - d = window_copy_cellstring(gl, ax, &dlen); + d = window_copy_cellstring(gl, ax, &dlen, &allocated); newsize += dlen; while (bufsize < newsize) { bufsize *= 2; @@ -2650,6 +2661,8 @@ memcpy(buf + bx, d, dlen); bx += dlen; } + if (allocated) + free((void *)d); } buf[newsize - 1] = '\0'; @@ -2670,6 +2683,7 @@ struct { const char *d; size_t dlen; + int allocated; } *cells; /* Populate the array of cell data. */ @@ -2680,7 +2694,7 @@ gl = grid_peek_line(gd, pywrap); while (cell < ncells) { cells[cell].d = window_copy_cellstring(gl, px, - &cells[cell].dlen); + &cells[cell].dlen, &cells[cell].allocated); cell++; px++; if (px == gd->sx) { @@ -2738,6 +2752,10 @@ *ppy = pywrap; /* Free cell data. */ + for (cell = 0; cell < ncells; cell++) { + if (cells[cell].allocated) + free((void *)cells[cell].d); + } free(cells); }