From b4b4a473cd34fb2ad96ff36872f370421cb24b5a Mon Sep 17 00:00:00 2001 From: Sam Hocevar Date: Tue, 19 Oct 2021 11:41:18 +0200 Subject: [PATCH] Fix off-by-one memory read in binary canvas import (fixes #56, #58) --- caca/codec/import.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/caca/codec/import.c b/caca/codec/import.c index 2dafe3c..ae25dc0 100644 --- a/caca/codec/import.c +++ b/caca/codec/import.c @@ -420,6 +420,9 @@ ssize_t _import_bin(caca_canvas_t *cv, void const *data, size_t len) caca_set_canvas_size(cv, 0, 0); caca_set_canvas_size(cv, 160, len / 160); + /* Only read an even number of bytes */ + len &= ~(size_t)1; + for (i = 0; i < len; i += 2) { caca_set_color_ansi(cv, buf[i + 1] & 0xf, buf[i + 1] >> 4); @@ -433,6 +436,6 @@ ssize_t _import_bin(caca_canvas_t *cv, void const *data, size_t len) } } - return len & ~(size_t)1; + return len; }