Avoid out-of-bounds access when a slide input line begins with \0
authorChris Down <chris@chrisdown.name>
Wed, 13 May 2020 11:20:53 +0000 (12:20 +0100)
committerHiltjo Posthuma <hiltjo@codemadness.org>
Thu, 14 May 2020 09:43:34 +0000 (11:43 +0200)
If we read in a line with \0 at the beginning, blen will be 0. However,
we then try to index our copy of the buffer with
s->lines[s->linecount][blen-1], we'll read (and potentially write if the
data happens to be 0x0A) outside of strdup's allocated memory, and may
crash.

Fix this by just rejecting lines with a leading \0. Lines with nulls
embedded in other places don't invoke similar behaviour, since the
length is still >0.

sent.c

diff --git a/sent.c b/sent.c
index c50a572c24269769d1a84e1bb584df84278991e1..9534fcaf97b94eec6cff1d2ced1c2c699d829293 100644 (file)
--- a/sent.c
+++ b/sent.c
@@ -428,6 +428,10 @@ load(FILE *fp)
                maxlines = 0;
                memset((s = &slides[slidecount]), 0, sizeof(Slide));
                do {
+                       /* if there's a leading null, we can't do blen-1 */
+                       if (buf[0] == '\0')
+                               continue;
+
                        if (buf[0] == '#')
                                continue;