lexer: Treat null characters in input as spaces.
authorBen Pfaff <blp@cs.stanford.edu>
Sun, 3 Apr 2016 00:53:52 +0000 (17:53 -0700)
committerBen Pfaff <blp@cs.stanford.edu>
Sun, 3 Apr 2016 00:53:52 +0000 (17:53 -0700)
In some circumstances nulls could confuse the lexer and cause crashes.

Thanks to John Darrington for reporting the problem.

Bug #47602.

src/language/lexer/lexer.c
tests/language/lexer/lexer.at

index ea81f19559926a37fc47e69efe7c464a82384909..1f455119f00b1779f51787d54dedf4b469c46b1b 100644 (file)
@@ -1174,19 +1174,33 @@ lex_source_read__ (struct lex_source *src)
 {
   do
     {
-      size_t head_ofs;
-      size_t space;
-      size_t n;
-
       lex_source_expand__ (src);
 
-      head_ofs = src->head - src->tail;
-      space = src->allocated - head_ofs;
-      n = src->reader->class->read (src->reader, &src->buffer[head_ofs],
-                                    space,
-                                    segmenter_get_prompt (&src->segmenter));
+      size_t head_ofs = src->head - src->tail;
+      size_t space = src->allocated - head_ofs;
+      enum prompt_style prompt = segmenter_get_prompt (&src->segmenter);
+      size_t n = src->reader->class->read (src->reader, &src->buffer[head_ofs],
+                                           space, prompt);
       assert (n <= space);
 
+      for (char *p = &src->buffer[head_ofs]; p < &src->buffer[head_ofs + n];
+           p++)
+        if (*p == '\0')
+          {
+            struct msg m;
+            m.category = MSG_C_SYNTAX;
+            m.severity = MSG_S_ERROR;
+            m.file_name = src->reader->file_name;
+            m.first_line = 0;
+            m.last_line = 0;
+            m.first_column = 0;
+            m.last_column = 0;
+            m.text = xstrdup ("Bad character U+0000 in input.");
+            msg_emit (&m);
+
+            *p = ' ';
+          }
+
       if (n == 0)
         {
           /* End of input.
index 08c146447fbed1964843a9544efec6f63b9292e2..af6de0c904cccef117e7c5abce282f21e1f4c105 100644 (file)
@@ -61,3 +61,17 @@ lexer.sps:10.1: error: Syntax error at ``': Bad character ``' in input.
 lexer.sps:11.1: error: Syntax error at `�': Bad character U+FFFD in input.
 ])
 AT_CLEANUP
+
+# Bug #47602.
+AT_SETUP([lexer crash due to null byte])
+# Intentionally leave out the new-line and add a null byte:
+printf "datA dist list notable file='input.txt'/a b c.
+lis|.\0" > lexer.sps
+AT_CHECK([pspp -O format=csv lexer.sps], [1], [dnl
+lexer.sps: error: Bad character U+0000 in input.
+
+lexer.sps:1: error: Unknown command `datA dist'.
+
+lexer.sps:2: error: LIST: LIST is allowed only after the active dataset has been defined.
+])
+AT_CLEANUP