From 7c70f1b831fd61e71efea1f4a3e264a0fa303131 Mon Sep 17 00:00:00 2001 From: Patrick McDermott Date: Wed, 17 Jul 2019 21:03:59 -0400 Subject: text_new(): Handle missing trailing newline Or maybe people who omit trailing newlines deserve buffer overflows. --- diff --git a/src/text.c b/src/text.c index 2ae5a66..10996c5 100644 --- a/src/text.c +++ b/src/text.c @@ -40,6 +40,7 @@ text_new(const char *string) { struct text *text; int width; + int eol; if (!string) { return NULL; @@ -52,6 +53,7 @@ text_new(const char *string) text->string = string; width = 0; + eol = 0; while (string && *string) { if (*string == 0x1B && *(string + 1) == '[') { /* CSI */ string += 2; @@ -60,16 +62,22 @@ text_new(const char *string) if (*string >= 0x40 && *string <= 0x7E) { ++string; } + eol = 0; } else if (*string == '\n') { text->width = MAX(text->width, width); width = 0; ++text->height; ++string; + eol = 1; } else { ++width; ++string; + eol = 0; } } + if (!eol) { + ++text->height; + } return text; } -- cgit v0.9.1