summaryrefslogtreecommitdiff
blob: 1b92572cc820977d23e98e72225bf9ec4bc8e536 (plain)
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
Upstream: https://git.savannah.gnu.org/cgit/pspp.git/commit/?id=fe94912b9c8682c4666873b84c83cda88f4c135d

commit fe94912b9c8682c4666873b84c83cda88f4c135d
Author: Ben Pfaff <blp@cs.stanford.edu>
Date:   Mon Nov 26 06:54:52 2018 -0800

    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

diff --git a/src/language/lexer/segment.c b/src/language/lexer/segment.c
index c0a09973c..c607c4bd1 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)