From fe94912b9c8682c4666873b84c83cda88f4c135d Mon Sep 17 00:00:00 2001 From: Ben Pfaff Date: Mon, 26 Nov 2018 06:54:52 -0800 Subject: [PATCH] segment: Fix behavior when #! line is not new-line terminated. The code here is supposed to maintain a invariant that, when it returns a nonnegative value, it initializes *type, but it failed to do that when a #! line did not end in a new-line. This fixes the problem. Bug #55101. Thanks for Friedrich Beckmann for narrowing down the problem. Found by the Debian buildd: https://buildd.debian.org/status/fetch.php?pkg=pspp&arch=arm64&ver=1.2.0-1&stamp=1543183214&raw=0 --- src/language/lexer/segment.c | 33 +++++++++++++++++++-------------- 1 file changed, 19 insertions(+), 14 deletions(-) diff --git a/src/language/lexer/segment.c b/src/language/lexer/segment.c index c0a09973ce..c607c4bd1f 100644 --- a/src/language/lexer/segment.c +++ b/src/language/lexer/segment.c @@ -92,21 +92,26 @@ segmenter_parse_shbang__ (struct segmenter *s, const char *input, size_t n, { if (input[1] == '!') { - int ofs; - - for (ofs = 2; ofs < n; ofs++) - if (input[ofs] == '\n') - { - if (input[ofs] == '\n' && input[ofs - 1] == '\r') - ofs--; - - s->state = S_GENERAL; - s->substate = SS_START_OF_COMMAND; - *type = SEG_SHBANG; - return ofs; - } + for (int ofs = 2; ; ofs++) + { + if (ofs >= n) + { + if (!eof) + return -1; + } + else if (input[ofs] == '\n') + { + if (input[ofs - 1] == '\r') + ofs--; + } + else + continue; - return eof ? ofs : -1; + s->state = S_GENERAL; + s->substate = SS_START_OF_COMMAND; + *type = SEG_SHBANG; + return ofs; + } } } else if (!eof) -- 2.30.2