From 3566be591a971a118dca6edc263dd7eefaa5f648 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Ekl=C3=B6f?= Date: Sun, 28 Mar 2021 12:55:09 +0200 Subject: [PATCH 1/3] sixel: initialize max_non_empty_row_no to -1, not 0 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 0 is a perfectly valid row number, and if max_non_empty_row_no==0, that means we have *1* sixel row, and after trimming the image, the image will have a height of 6 pixels. If the sixel sequence is empty (or at least doesn’t emit any non-empty pixels), then trimming the image should result in an image height of 0. When max_non_empty_row_no is initialized to -1, it will still have that value in unhook(), which makes the final image height 0. --- sixel.c | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/sixel.c b/sixel.c index 3953f735..c856122e 100644 --- a/sixel.c +++ b/sixel.c @@ -48,7 +48,7 @@ sixel_init(struct terminal *term, int p1, int p2, int p3) term->sixel.state = SIXEL_DECSIXEL; term->sixel.pos = (struct coord){0, 0}; - term->sixel.max_non_empty_row_no = 0; + term->sixel.max_non_empty_row_no = -1; term->sixel.row_byte_ofs = 0; term->sixel.color_idx = 0; term->sixel.param = 0; @@ -1032,7 +1032,7 @@ sixel_add(struct terminal *term, int col, int width, uint32_t color, uint8_t six size_t ofs = term->sixel.row_byte_ofs + col; uint32_t *data = &term->sixel.image.data[ofs]; - int max_non_empty_row = 0; + int max_non_empty_row = -1; int row = term->sixel.pos.row; for (int i = 0; i < 6; i++, sixel >>= 1, data += width) { @@ -1175,7 +1175,11 @@ decgra(struct terminal *term, uint8_t c) ph <= term->sixel.max_height && pv <= term->sixel.max_width) { resize(term, ph, pv); - term->sixel.max_non_empty_row_no = pv - 1; + if (!term->sixel.transparent_bg) { + /* This ensures the sixel’s final image size is *at + * least* this large */ + term->sixel.max_non_empty_row_no = pv - 1; + } } term->sixel.state = SIXEL_DECSIXEL; From 19289bad5ef60f4c5ca2b45c0e495db60ed34267 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Ekl=C3=B6f?= Date: Sun, 28 Mar 2021 12:57:49 +0200 Subject: [PATCH 2/3] sixel: free backing buffer if final image size is zero --- sixel.c | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/sixel.c b/sixel.c index c856122e..cac60493 100644 --- a/sixel.c +++ b/sixel.c @@ -726,6 +726,11 @@ sixel_unhook(struct terminal *term) term->sixel.image.height = term->sixel.max_non_empty_row_no + 1; } + if (term->sixel.image.height == 0 || term->sixel.image.width == 0) { + /* We won’t be emitting any sixels - free backing image */ + free(term->sixel.image.data); + } + int pixel_row_idx = 0; int pixel_rows_left = term->sixel.image.height; const int stride = term->sixel.image.width * sizeof(uint32_t); From efbbcf289f28f61b936b94a8daab84fd04158000 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Ekl=C3=B6f?= Date: Sun, 28 Mar 2021 13:04:15 +0200 Subject: [PATCH 3/3] changelog: empty sixels resulted in non-empty images --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0b10e083..38d2a395 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -50,6 +50,7 @@ sometimes not showing up, caused by invalidly sized surface buffers when output scaling was enabled (https://codeberg.org/dnkl/foot/issues/409). +* Empty sixels resulted in non-empty images. ### Security