Skip to content

Commit 7809598

Browse files
committed
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
1 parent 2963358 commit 7809598

File tree

1 file changed

+24
-1
lines changed

1 file changed

+24
-1
lines changed

src/dscanner/analysis/if_constraints_indent.d

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,19 @@ final class IfConstraintsIndentCheck : BaseAnalyzer
2424
{
2525
super(fileName, null, skipTests);
2626

27+
// convert tokens to a list of token starting positions per line
28+
2729
// libdparse columns start at 1
2830
foreach (t; tokens)
2931
{
30-
while (firstSymbolAtLine.length < t.line - 1)
32+
// pad empty positions if we skip empty token-less lines
33+
// t.line (unsigned) may be 0 if the token is uninitialized/broken, so don't subtract from it
34+
// equivalent to: firstSymbolAtLine.length < t.line - 1
35+
while (firstSymbolAtLine.length + 1 < t.line)
3136
firstSymbolAtLine ~= Pos(1);
3237

38+
// insert a new line with positions if new line is reached
39+
// (previous while pads skipped lines)
3340
if (firstSymbolAtLine.length < t.line)
3441
firstSymbolAtLine ~= Pos(t.column, t.type == tok!"if");
3542
}
@@ -233,3 +240,19 @@ if
233240

234241
stderr.writeln("Unittest for IfConstraintsIndentCheck passed.");
235242
}
243+
244+
@("issue #829")
245+
unittest
246+
{
247+
import dscanner.analysis.config : StaticAnalysisConfig, Check, disabledConfig;
248+
import dscanner.analysis.helpers : assertAnalyzerWarnings;
249+
import std.format : format;
250+
import std.stdio : stderr;
251+
252+
StaticAnalysisConfig sac = disabledConfig();
253+
sac.if_constraints_indent = Check.enabled;
254+
255+
assertAnalyzerWarnings(`void foo() {
256+
''
257+
}`, sac);
258+
}

0 commit comments

Comments
 (0)