1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
|
From fe94912b9c8682c4666873b84c83cda88f4c135d Mon Sep 17 00:00:00 2001
From: Ben Pfaff <blp@cs.stanford.edu>
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 c0a09973..c607c4bd 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.19.2
|