diff options
author | Patrick McDermott <patrick.mcdermott@libiquity.com> | 2019-07-17 21:03:59 (EDT) |
---|---|---|
committer | Patrick McDermott <patrick.mcdermott@libiquity.com> | 2019-07-17 21:03:59 (EDT) |
commit | 7c70f1b831fd61e71efea1f4a3e264a0fa303131 (patch) | |
tree | 0c497a25e68e97ac86859a1400ab789cab3e8150 | |
parent | 888d289641be21bd50fa56851ca78eb925e2e710 (diff) |
text_new(): Handle missing trailing newline
Or maybe people who omit trailing newlines deserve buffer overflows.
-rw-r--r-- | src/text.c | 8 |
1 files changed, 8 insertions, 0 deletions
@@ -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; } |