fix if_constraints_indent with broken/null tokens

Subtraction from t.line (which is unsigned) caused the while loop
to run nearly infinitely before, starving the system of memory as it
was allocating memory.

fixes #829
This commit is contained in:
WebFreak001 2021-05-02 21:21:14 +02:00 committed by Jan Jurzitza
parent 2963358eb4
commit 7809598da0
1 changed files with 24 additions and 1 deletions

View File

@ -24,12 +24,19 @@ final class IfConstraintsIndentCheck : BaseAnalyzer
{
super(fileName, null, skipTests);
// convert tokens to a list of token starting positions per line
// libdparse columns start at 1
foreach (t; tokens)
{
while (firstSymbolAtLine.length < t.line - 1)
// pad empty positions if we skip empty token-less lines
// t.line (unsigned) may be 0 if the token is uninitialized/broken, so don't subtract from it
// equivalent to: firstSymbolAtLine.length < t.line - 1
while (firstSymbolAtLine.length + 1 < t.line)
firstSymbolAtLine ~= Pos(1);
// insert a new line with positions if new line is reached
// (previous while pads skipped lines)
if (firstSymbolAtLine.length < t.line)
firstSymbolAtLine ~= Pos(t.column, t.type == tok!"if");
}
@ -233,3 +240,19 @@ if
stderr.writeln("Unittest for IfConstraintsIndentCheck passed.");
}
@("issue #829")
unittest
{
import dscanner.analysis.config : StaticAnalysisConfig, Check, disabledConfig;
import dscanner.analysis.helpers : assertAnalyzerWarnings;
import std.format : format;
import std.stdio : stderr;
StaticAnalysisConfig sac = disabledConfig();
sac.if_constraints_indent = Check.enabled;
assertAnalyzerWarnings(`void foo() {
''
}`, sac);
}