Skip to content

Commit

Permalink
Protect the code agains corrupted grammars
Browse files Browse the repository at this point in the history
  • Loading branch information
danipen committed Jan 14, 2022
1 parent 20f563d commit eb755da
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 2 deletions.
4 changes: 4 additions & 0 deletions src/TextMateSharp/Internal/Grammars/LineTokenizer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,10 @@ private IMatchResult MatchRule(Grammar grammar, string lineText, in bool isFirst
return null;

ICompiledRule ruleScanner = rule.Compile(grammar, stack.endRule, isFirstLine, linePos == anchorPosition);

if (ruleScanner == null)
return null;

IOnigNextMatchResult r = ruleScanner.scanner.FindNextMatchSync(lineText, linePos);

if (r != null)
Expand Down
4 changes: 2 additions & 2 deletions src/TextMateSharp/Internal/Rules/CaptureRule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ public CaptureRule(int? id, string name, string contentName, int? retokenizeCapt

public override void CollectPatternsRecursive(IRuleRegistry grammar, RegExpSourceList sourceList, bool isFirst)
{
throw new InvalidOperationException("Not supported");

}

public override ICompiledRule Compile(IRuleRegistry grammar, string endRegexSource, bool allowA, bool allowG)
{
throw new InvalidOperationException("Not supported");
return null;
}

}
Expand Down
3 changes: 3 additions & 0 deletions src/TextMateSharp/Model/TMModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -189,12 +189,15 @@ public int UpdateTokensInRange(ModelTokensChangedEventBuilder eventBuilder, int
try
{
text = model.lines.GetLineText(lineIndex);
if (text == null)
continue;
// Tokenize only the first X characters
r = model.tokenizer.Tokenize(text, modeLine.GetState(), 0, MAX_LEN_TO_TOKENIZE);
}
catch (Exception e)
{
System.Diagnostics.Debug.WriteLine(e.Message);
continue;
}

if (r != null && r.Tokens != null && r.Tokens.Count != 0)
Expand Down

0 comments on commit eb755da

Please sign in to comment.