From 3065ebc14bb96506429b4ebde3aeb3793c72a66d Mon Sep 17 00:00:00 2001 From: Thomas Loimer Date: Sun, 16 Feb 2020 18:54:01 +0100 Subject: [PATCH] Allow last line of file lacking eol char, #83, #84 If the last line of a fig file does not end with a newline, the code parsing the input could read beyond the allocated buffer. This commit fixes the parsing at two locations in the code, one in string parsing, the second where sequences of a backslash and octal digits are converted to characters. --- fig2dev/read.c | 6 ++++-- fig2dev/tests/read.at | 11 +++++++++++ 2 files changed, 15 insertions(+), 2 deletions(-) --- fig2dev/read.c +++ fig2dev/read.c 2020-09-30 10:46:34.214234522 +0000 @@ -1483,6 +1483,8 @@ read_textobject(FILE *fp, char **restric len = strlen(start); start[len++] = '\n'; /* put back the newline */ + start[len] = '\0'; /* and terminate the string, + in case nothing else is found */ /* allocate plenty of space */ next = malloc(len + BUFSIZ); @@ -1491,7 +1493,7 @@ read_textobject(FILE *fp, char **restric free(t); return NULL; } - memcpy(next, start, len); + memcpy(next, start, len + 1); while ((chars = getline(line, line_len, fp)) != -1) { ++(*line_no); @@ -1525,7 +1527,7 @@ read_textobject(FILE *fp, char **restric len = end - start; l = len; while (c[l] != '\0') { - if (c[l] == '\\') { + if (c[l] == '\\' && c[l+1] != '\0') { /* convert 3 digit octal value */ if (isdigit(c[l+1]) && c[l+2] != '\0' && c[l+3] != '\0') { --- fig2dev/tests/read.at +++ fig2dev/tests/read.at 2020-09-30 10:46:34.262233620 +0000 @@ -416,6 +416,17 @@ AT_CHECK([fig2dev -L tikz text.fig ], 0, ignore) AT_CLEANUP +AT_SETUP([allow files end without eol, tickets #83, #84]) +AT_KEYWORDS([read.c]) +AT_CHECK([AS_ECHO_N(["FIG_FILE_TOP +4 0 0 50 0 -1 12 0 0 150 405 0 0 No end-of-line here -->"]) | \ + fig2dev -L box], 0, ignore) +AT_CHECK([AS_ECHO_N(["FIG_FILE_TOP +4 0 0 50 0 -1 12 0 0 150 405 0 0 Start string +No end-of-line after one backslash --> \\"]) | \ + fig2dev -L box], 0, ignore) +AT_CLEANUP + AT_BANNER([Dynamically allocate picture file name.]) AT_SETUP([prepend fig file path to picture file name])