Skip to content

Commit

Permalink
Fix crashes inside Markdown
Browse files Browse the repository at this point in the history
Resolves #365
Problem
---
Parsing Markdown with nested Scala code using significant indentation crashes with SIGSEGV
Example code:
```scala
for
  i <- 1 to 10
yield i
```

Solution
---
Stop calling `lexer->get_column` in `scanner.c` without prior eof-checks
  • Loading branch information
susliko committed Dec 21, 2023
1 parent 1b4c2fa commit 53c4cb6
Showing 1 changed file with 3 additions and 6 deletions.
9 changes: 3 additions & 6 deletions src/scanner.c
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,6 @@ bool tree_sitter_scala_external_scanner_scan(void *payload, TSLexer *lexer,
int prev = peekStack(stack);
int newline_count = 0;
int indentation_size = 0;
LOG("scanner was called at column: %d\n", lexer->get_column(lexer));

while (iswspace(lexer->lookahead)) {
if (lexer->lookahead == '\n') {
Expand Down Expand Up @@ -191,18 +190,16 @@ bool tree_sitter_scala_external_scanner_scan(void *payload, TSLexer *lexer,
}

// Recover newline_count from the outdent reset
bool is_eof = lexer->eof(lexer);
if (stack->last_newline_count > 0 &&
((lexer->eof(lexer) && stack->last_column == -1)
|| lexer->get_column(lexer) == stack->last_column)) {
((is_eof && stack->last_column == -1) ||
(!is_eof && lexer->get_column(lexer) == stack->last_column))) {
newline_count += stack->last_newline_count;
}
stack->last_newline_count = 0;

printStack(stack, " after");

LOG(" indentation_size: %d, newline_count: %d, column: %d, indent_is_valid: %d, dedent_is_valid: %d\n", indentation_size,
newline_count, lexer->get_column(lexer), valid_symbols[INDENT], valid_symbols[OUTDENT]);

if (valid_symbols[AUTOMATIC_SEMICOLON] && newline_count > 0) {
// AUTOMATIC_SEMICOLON should not be issued in the middle of expressions
// Thus, we exit this branch when encountering comments, else/catch clauses, etc.
Expand Down

0 comments on commit 53c4cb6

Please sign in to comment.