Skip to content

fix: handle null code blocks in loops #3465

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/sentry.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,4 @@ jobs:
SENTRY_PROJECT: ${{ secrets.SENTRY_PROJECT }}
with:
environment: ${{ env.ENVIRONMENT }}
version: ${{ env.RELEASE_VERSION }}
release: ${{ env.RELEASE_VERSION }}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import com.github._1c_syntax.bsl.parser.BSLParser;
import com.github._1c_syntax.bsl.parser.BSLParserBaseVisitor;
import com.github._1c_syntax.bsl.parser.BSLParserRuleContext;
import jakarta.annotation.Nullable;
import org.antlr.v4.runtime.tree.ParseTree;

import java.util.ArrayList;
Expand Down Expand Up @@ -194,7 +195,7 @@ public ParseTree visitElsifBranch(BSLParser.ElsifBranchContext ctx) {
public ParseTree visitCodeBlock(BSLParser.CodeBlockContext ctx) {
var currentBlock = blocks.getCurrentBlock();
graph.addVertex(currentBlock.begin());
return super.visitCodeBlock(ctx);
return ctx == null ? null : super.visitCodeBlock(ctx);
}

@Override
Expand Down Expand Up @@ -524,7 +525,7 @@ private void makeJump(CfgVertex jumpTarget) {
graph.addVertex(blocks.getCurrentBlock().end());
}

private void buildLoopSubgraph(BSLParser.CodeBlockContext ctx, LoopVertex loopStart) {
private void buildLoopSubgraph(@Nullable BSLParser.CodeBlockContext ctx, LoopVertex loopStart) {
graph.addVertex(loopStart);
connectGraphTail(blocks.getCurrentBlock(), loopStart);

Expand All @@ -536,9 +537,7 @@ private void buildLoopSubgraph(BSLParser.CodeBlockContext ctx, LoopVertex loopSt
jumpState.loopBreak = blocks.getCurrentBlock().end();

blocks.enterBlock(jumpState);

ctx.accept(this);

visitCodeBlock(ctx);
var body = blocks.leaveBlock();

graph.addEdge(loopStart, body.begin(), CfgEdgeType.TRUE_BRANCH);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,40 @@ void testInnerLoops() {
assertThat(walker.getCurrentNode()).isInstanceOf(ExitVertex.class);
}

@Test
void testLoopWithNullCodeBlock() {
var code = """
Для х = 1 По 10 Цикл
КонецЦикла;""";

var parseTree = parse(code);

// Force null code block by removing the CodeBlock child
var forStatement = parseTree.statement(0).compoundStatement().forStatement();
var children = forStatement.children;

// Find and remove the CodeBlockContext child
for (int i = 0; i < children.size(); i++) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

А как без шаманства с нодами, чисто с помощью bsl получить нулл вместо код-блока?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Нужно какую-то фигню пустить на вход, которая обычно случается при наборе кода. Я могу попробовать подобрать кусок кода, который бы сломал парсер, но это зависимость теста на поведение конкретной версии парсера. А так тест проверяет уже конечный сломанный результат

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Да не, не надо

if (children.get(i) instanceof BSLParser.CodeBlockContext) {
children.remove(i);
break;
}
}

var builder = new CfgBuildingParseTreeVisitor();
var graph = builder.buildGraph(parseTree);

// Check if graph was built successfully
assertThat(graph).isNotNull();
assertThat(graph.vertexSet()).isNotEmpty();

// Verify basic structure - should at least have loop vertex and exit
var vertices = traverseToOrderedList(graph);
assertThat(vertices)
.hasAtLeastOneElementOfType(ForLoopVertex.class)
.hasAtLeastOneElementOfType(ExitVertex.class);
}

@Test
void tryHandlerFlowTest() {
var code = """
Expand Down
Loading