From 7b29dd0925763af5e371bf1d6bc9c8f3b342c2cc Mon Sep 17 00:00:00 2001 From: nick black Date: Sat, 27 Feb 2021 17:41:21 -0500 Subject: [PATCH] sixel_blit: no big arrays on the stack #200 --- src/lib/blit.c | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/lib/blit.c b/src/lib/blit.c index 7579a8f92..cf90e91d2 100644 --- a/src/lib/blit.c +++ b/src/lib/blit.c @@ -855,7 +855,9 @@ sixel_blit(ncplane* nc, int placey, int placex, int linesize, int visx = begx; for(x = placex ; visx < (begx + lenx) && x < dimx ; ++x, visx += 1){ size_t offset = 0; - char sixel[128]; // FIXME +#define GROWTHFACTOR 256 + size_t avail = GROWTHFACTOR; + char* sixel = malloc(avail); // FIXME find sixels with common colors for single register program unsigned bitsused = 0; // once 63, we're done int colorreg = 1; // leave 0 as background @@ -882,7 +884,8 @@ sixel_blit(ncplane* nc, int placey, int placex, int linesize, char c = 63 + thesebits; // FIXME use percentages(rgb) // bitstring is added to 63, resulting in [63, 126] aka '?'..'~' - int n = snprintf(sixel + offset, sizeof(sixel) - offset, + // FIXME grow if necessary + int n = snprintf(sixel + offset, avail - offset, "%s#%d;2;%d;%d;%d#%d%c", printed ? "$" : "", colorreg, 100, 100, 100, colorreg, c); if(n < 0){ @@ -899,13 +902,16 @@ sixel_blit(ncplane* nc, int placey, int placex, int linesize, if(offset){ nccell* c = ncplane_cell_ref_yx(nc, y, x); if(pool_blit_direct(&nc->pool, c, sixel, offset, 1) <= 0){ + free(sixel); return -1; } cell_set_pixels(c, 1); } // FIXME otherwise, reset? + free(sixel); } } return total; +#undef GROWTHFACTOR } // NCBLIT_DEFAULT is not included, as it has no defined properties. It ought