segment: Fix behavior when #! line is not new-line terminated.
authorBen Pfaff <blp@cs.stanford.edu>
Mon, 26 Nov 2018 14:54:52 +0000 (06:54 -0800)
committerBen Pfaff <blp@cs.stanford.edu>
Mon, 26 Nov 2018 14:54:52 +0000 (06:54 -0800)
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

index c0a09973ce8059535ea7c1e487c5fa2016ff8134..c607c4bd1ffc52061ab7cf7d6d6632d3e4a8b249 100644 (file)
@@ -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)