From bd42986a8bd67e5ea647d5ed91fc4a89307e308c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Ekl=C3=B6f?= Date: Wed, 30 Dec 2020 13:30:39 +0100 Subject: [PATCH] box-drawing: {h,v}line(): handle both start/end point being too small *and* too large --- box-drawing.c | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/box-drawing.c b/box-drawing.c index 8584f8ad..07deb387 100644 --- a/box-drawing.c +++ b/box-drawing.c @@ -24,8 +24,11 @@ thickness(float pts, int dpi) static void _hline(uint8_t *buf, int x1, int x2, int y, int thick, int width, int height, int stride) { + x1 = min(max(x1, 0), width); + x2 = min(max(x2, 0), width); + for (size_t row = max(y, 0); row < min(y + thick, height); row++) { - for (size_t col = max(x1, 0); col < min(x2, width); col++) { + for (size_t col = x1; col < x2; col++) { size_t idx = col / 8; size_t bit_no = col % 8; buf[row * stride + idx] |= 1 << bit_no; @@ -38,7 +41,10 @@ _hline(uint8_t *buf, int x1, int x2, int y, int thick, int width, int height, in static void _vline(uint8_t *buf, int y1, int y2, int x, int thick, int width, int height, int stride) { - for (size_t row = max(y1, 0); row < min(y2, height); row++) { + y1 = min(max(y1, 0), height); + y2 = min(max(y2, 0), height); + + for (size_t row = y1; row < y2; row++) { for (size_t col = max(x, 0); col < min(x + thick, width); col++) { size_t idx = col / 8; size_t bit_no = col % 8;