Bail out before allocating slides if file is empty

In load() we allocated slides before checking if we actually read
anything from the FILE fp and then continue with an allocated but
“empty” space wich would lead to errors.
This commit is contained in:
Quentin Rameau 2015-11-18 23:21:33 +01:00 committed by Markus Teich
parent eac14478e9
commit a1dcdad14f

8
sent.c
View File

@ -413,10 +413,6 @@ void load(FILE *fp)
/* read each line from fp and add it to the item list */ /* read each line from fp and add it to the item list */
while (1) { while (1) {
if ((slidecount+1) * sizeof(*slides) >= size)
if (!(slides = realloc(slides, (size += BUFSIZ))))
die("cannot realloc %u bytes:", size);
/* eat consecutive empty lines */ /* eat consecutive empty lines */
while ((p = fgets(buf, sizeof(buf), fp))) while ((p = fgets(buf, sizeof(buf), fp)))
if (strcmp(buf, "\n") != 0 && buf[0] != '#') if (strcmp(buf, "\n") != 0 && buf[0] != '#')
@ -424,6 +420,10 @@ void load(FILE *fp)
if (!p) if (!p)
break; break;
if ((slidecount+1) * sizeof(*slides) >= size)
if (!(slides = realloc(slides, (size += BUFSIZ))))
die("cannot realloc %u bytes:", size);
/* read one slide */ /* read one slide */
maxlines = 0; maxlines = 0;
memset((s = &slides[slidecount]), 0, sizeof(Slide)); memset((s = &slides[slidecount]), 0, sizeof(Slide));