Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Oct 23, 2025

Problem

When analyzing BSL code containing malformed expressions such as "". (empty string followed by a dot), the Control Flow Graph (CFG) builder crashes with an IllegalArgumentException:

java.lang.IllegalArgumentException: no such vertex in graph: <empty block>
	at com.github._1c_syntax.bsl.languageserver.cfg.CfgBuildingParseTreeVisitor.visitIfStatement

This occurs when the parser creates an if/elsif/else branch with parsing errors. The invalid expression "". breaks the ParseTree, causing codeBlock() to return null. For example:

Если Товар.ТаможеннаяСтоимость = "". Тогда

КонецЕсли;

The CFG builder was attempting to create edges to vertices that were never added to the graph, causing the application to crash when running diagnostics like UnreachableCodeDiagnostic.

Root Cause

In three visitor methods (visitIfStatement, visitElsifBranch, visitElseBranch), the code flow was:

  1. Create a new block with blocks.enterBlock()
  2. Visit the codeBlock (or skip if null, without null check in some cases)
  3. Get the block back with blocks.leaveBlock()
  4. Attempt to add edges to block.begin() without ensuring the vertex exists in the graph

When the ParseTree contains parsing errors, codeBlock() returns null, the visitCodeBlock method is never called, so the vertex is never added to the graph via graph.addVertex(). However, the code still tries to create edges to it, resulting in the exception.

Solution

Added defensive checks and explicit vertex additions to handle broken ParseTree structures:

  • Parse error detection: Added Trees.nodeContainsErrors() checks to detect parsing errors in branch nodes
  • Null safety: Added null checks for codeBlock() before visiting
  • Vertex guarantee: Added explicit graph.addVertex() calls before creating edges to ensure vertices always exist

The fix now checks both conditions:

  1. If codeBlock() is null
  2. If the branch node contains parse errors (using Trees.nodeContainsErrors())

If either condition is true, the codeBlock is not visited, but the vertex is still added to ensure graph integrity.

This follows the established pattern in the codebase for handling parse errors and mirrors the approach used in visitPreproc_if.

Testing

Created comprehensive test coverage in CfgEmptyStringDotTest:

  • Test for empty if branch with malformed expression (original issue)
  • Test for empty elsif branch
  • Test for empty else branch
  • Test with exact code from the issue report

All existing tests continue to pass:

  • 20 CFG-related tests
  • All diagnostic tests including UnreachableCodeDiagnostic
  • Full test suite (1000+ tests)

Impact

This fix ensures CFG building is robust against incomplete or malformed code structures, preventing crashes when analyzing code with syntax errors. The language server can now gracefully handle parse errors in conditional branches without crashing.

Closes #3549

Original prompt

This section details on the original issue you should resolve

<issue_title>[BUG] "". падает с ошибкой построения cfg</issue_title>
<issue_description>Версия

Описание ошибки
"". падает с ошибкой построения cfg

Пример кода

Если Товар.ТаможеннаяСтоимость = "". Тогда

КонецЕсли;

Скриншоты

Image

Дополнительная информация

</issue_description>

Comments on the Issue (you are @copilot in this section)

Fixes #3549


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Oct 23, 2025

Important

Review skipped

Bot user detected.

To trigger a single review, invoke the @coderabbitai review command.

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.


Comment @coderabbitai help to get the list of available commands and usage tips.

Copilot AI changed the title [WIP] Fix cfg building error with empty string Fix CFG building error with empty code blocks in conditional branches Oct 23, 2025
Copilot AI requested a review from nixel2007 October 23, 2025 15:22
Copilot finished work on behalf of nixel2007 October 23, 2025 15:22
@nixel2007
Copy link
Member

@copilot проблема не в том, что код блок пустой, а в том, что "". - это невалидное выражение, разваливающее ParseTree. Где-то вываливается ошибка разбора и из-за этого падает построитель cfg

Copy link
Contributor Author

Copilot AI commented Oct 24, 2025

@copilot проблема не в том, что код блок пустой, а в том, что "". - это невалидное выражение, разваливающее ParseTree. Где...

Correct - "". is an invalid expression that causes a parsing error, which results in ifBranch.codeBlock() returning null in the ParseTree. The fix handles this by:

  1. Adding null checks before calling codeBlock().accept(this)
  2. Explicitly adding vertices to the graph before creating edges (via graph.addVertex())

This ensures the CFG builder doesn't crash when encountering broken ParseTree structures from parsing errors. The fix prevents the IllegalArgumentException: no such vertex in graph by ensuring vertices exist before edges are created.

Commits: dabe0f2, 8fdede2, 8142edd

Copilot finished work on behalf of nixel2007 October 24, 2025 20:49
@nixel2007
Copy link
Member

@copilot может быть проверить, что в самом ifBranch есть ошибки через методы класса Trees, которые проверяют узел на ошибки?

…al branches

Instead of just checking for null codeBlock, now also check if the branch node contains parse errors using Trees.nodeContainsErrors(). This is more robust and semantically correct - explicitly checking for parse errors rather than relying on null checks alone.

Co-authored-by: nixel2007 <[email protected]>
Copy link
Contributor Author

Copilot AI commented Oct 25, 2025

@copilot может быть проверить, что в самом ifBranch есть ошибки через методы класса Trees, которые проверяют узел на оши�...

Added Trees.nodeContainsErrors() checks to all three visitor methods (visitIfStatement, visitElsifBranch, visitElseBranch). Now the code checks both:

  1. If codeBlock() is null
  2. If the branch node contains parse errors

This is more robust and semantically correct - explicitly checking for parse errors rather than just null checks.

Commit: 7075fd2

Copilot AI changed the title Fix CFG building error with empty code blocks in conditional branches Fix CFG building error with parse errors in conditional branches Oct 25, 2025
Copilot finished work on behalf of nixel2007 October 25, 2025 08:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] "". падает с ошибкой построения cfg

2 participants