]> ocean-lang.org Git - ocean/commitdiff
scanner: handle missing newline at EOF
authorNeilBrown <neil@brown.name>
Fri, 17 May 2019 13:31:48 +0000 (23:31 +1000)
committerNeilBrown <neil@brown.name>
Fri, 17 May 2019 13:57:53 +0000 (23:57 +1000)
If there is no newline at EOF, we can see EOF immediately after
a valid symbol.  This can lead to calling close_token() when
state->node is NULL, which crashes.
The code in close_token() only makes sense if state->node is still the
same as token->node.  If it isn't, the token must be at the very end of
its code-node, so a different calculation is needed.

This avoids the NULL deref.

Signed-off-by: NeilBrown <neil@brown.name>
csrc/scanner.mdc

index 0d7a62d05127697cfdfed7ff207dd923de60c69c..15306dd17494245986dbd014ebd2a2c8693c977d 100644 (file)
@@ -1068,8 +1068,11 @@ parsed too much already.  For that there is `reset_token`.
        static void close_token(struct token_state *state,
                                struct token *tk)
        {
-               tk->txt.len = (state->node->code.txt + state->offset)
-                             - tk->txt.txt;
+               if (state->node != tk->node)
+                       tk->txt.len = tk->node->code.len - (tk->txt.txt - tk->node->code.txt);
+               else
+                       tk->txt.len = (state->node->code.txt + state->offset)
+                                     - tk->txt.txt;
        }
 
        static void reset_token(struct token_state *state, struct token *tok)