Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Jan 2, 2026

The expression tree builder crashes when parsing incomplete ternary operators (e.g., Возврат ?) because it attempts to dereference null expression contexts.

Changes

  • buildExpressionTree: Add null guard to return early instead of passing null to visitExpression
  • visitTernaryOperator: Check for null sub-expressions and create ErrorExpressionNode instead of calling Objects.requireNonNull
  • Test coverage: Add test case for incomplete ternary operator error handling

Example

// Before: NPE when ctx is null
public static @Nullable BslExpression buildExpressionTree(BSLParser.ExpressionContext ctx) {
  var instance = new ExpressionTreeBuildingVisitor();
  instance.visitExpression(ctx);  // crashes if ctx is null
  return instance.getExpressionTree();
}

// After: Null-safe
public static @Nullable BslExpression buildExpressionTree(BSLParser.@Nullable ExpressionContext ctx) {
  if (ctx == null) {
    return null;
  }
  var instance = new ExpressionTreeBuildingVisitor();
  instance.visitExpression(ctx);
  return instance.getExpressionTree();
}

The fix maintains consistency with existing error handling patterns by treating incomplete ternary operators as error expressions rather than failing hard.

Original prompt

This section details on the original issue you should resolve

<issue_title>NullPointerException: Cannot invoke "com.github._1c_syntax.bsl.parser.BSLParser$ExpressionContext.getChildCount()" because "ctx" is null</issue_title>
<issue_description>Sentry Issue: BSL-LANGUAGE-SERVER-M2

NullPointerException: Cannot invoke "com.github._1c_syntax.bsl.parser.BSLParser$ExpressionContext.getChildCount()" because "ctx" is null
    at com.github._1c_syntax.bsl.languageserver.utils.expressiontree.ExpressionTreeBuildingVisitor.visitExpression(ExpressionTreeBuildingVisitor.java:92)
    at com.github._1c_syntax.bsl.languageserver.utils.expressiontree.ExpressionTreeBuildingVisitor.buildExpressionTree(ExpressionTreeBuildingVisitor.java:71)
    at com.github._1c_syntax.bsl.languageserver.utils.expressiontree.ExpressionTreeBuildingVisitor.makeSubexpression(ExpressionTreeBuildingVisitor.java:435)
    at com.github._1c_syntax.bsl.languageserver.utils.expressiontree.ExpressionTreeBuildingVisitor.visitTernaryOperator(ExpressionTreeBuildingVisitor.java:423)
    at com.github._1c_syntax.bsl.languageserver.utils.expressiontree.ExpressionTreeBuildingVisitor.visitTernaryOperator(ExpressionTreeBuildingVisitor.java:46)
...
(55 additional frame(s) were not displayed)
```</issue_description>

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

<comments>
<comment_new><author>@nixel2007</author><body>
При вводе тернарных операторов вида:

Возврат ?

</comments>


💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Jan 2, 2026

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 NullPointerException in BSL parser Fix NullPointerException in incomplete ternary operator parsing Jan 2, 2026
Copilot AI requested a review from nixel2007 January 2, 2026 07:36
@nixel2007 nixel2007 marked this pull request as ready for review January 2, 2026 08:41
Copilot AI review requested due to automatic review settings January 2, 2026 08:41
@nixel2007 nixel2007 enabled auto-merge January 2, 2026 08:41
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR fixes a NullPointerException that occurs when parsing incomplete ternary operators in BSL code (e.g., when a user types Возврат ? or just ?). The fix adds defensive null checks at two key points in the expression tree building process to prevent crashes and instead create error nodes for graceful error handling.

Key changes:

  • Added null guard in buildExpressionTree to return null early when expression context is null
  • Modified visitTernaryOperator to check for null sub-expressions and create ErrorExpressionNode instead of crashing with NPE
  • Added test coverage for incomplete ternary operator parsing

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
ExpressionTreeBuildingVisitor.java Added null safety checks in buildExpressionTree (early return) and visitTernaryOperator (error node creation) to prevent NPE when parsing incomplete ternary operators
ExpressionTreeBuildingVisitorTest.java Added test case for incomplete ternary operator scenario to verify error handling works correctly without throwing exceptions

The implementation follows the existing error handling patterns in the codebase by using ErrorExpressionNode for malformed expressions, consistent with how other error cases are handled throughout the visitor (e.g., in visitNewExpression, visitParenthesis, visitMember). The fix is minimal, targeted, and maintains backward compatibility while preventing crashes during live editing scenarios.

@github-actions
Copy link
Contributor

github-actions bot commented Jan 2, 2026

Test Results

  312 files   - 2 496    312 suites   - 2 496   9m 59s ⏱️ - 43m 36s
1 150 tests +    1  1 150 ✅ +    1  0 💤 ±0  0 ❌ ±0 
1 150 runs   - 9 191  1 150 ✅  - 9 191  0 💤 ±0  0 ❌ ±0 

Results for commit d6e3e22. ± Comparison against base commit e5b5781.

This pull request removes 8 and adds 5 tests. Note that renamed tests count towards both.
#, count=1
, count=0
, count=1
A, count=1
com.github._1c_syntax.bsl.languageserver.diagnostics.MetadataObjectNameLengthDiagnosticTest ‑ [2] content=
com.github._1c_syntax.bsl.languageserver.diagnostics.MetadataObjectNameLengthDiagnosticTest ‑ [6] content=
com.github._1c_syntax.bsl.languageserver.diagnostics.MetadataObjectNameLengthDiagnosticTest ‑ [7] content=#
com.github._1c_syntax.bsl.languageserver.diagnostics.MetadataObjectNameLengthDiagnosticTest ‑ [8] content=
com.github._1c_syntax.bsl.languageserver.diagnostics.MetadataObjectNameLengthDiagnosticTest ‑ [2] content=
, count=0
com.github._1c_syntax.bsl.languageserver.diagnostics.MetadataObjectNameLengthDiagnosticTest ‑ [6] content=
A, count=1
com.github._1c_syntax.bsl.languageserver.diagnostics.MetadataObjectNameLengthDiagnosticTest ‑ [7] content=#
, count=1
com.github._1c_syntax.bsl.languageserver.diagnostics.MetadataObjectNameLengthDiagnosticTest ‑ [8] content=
#, count=1
com.github._1c_syntax.bsl.languageserver.utils.expressiontree.ExpressionTreeBuildingVisitorTest ‑ Incomplete ternary operator - only question mark

@sonarqubecloud
Copy link

sonarqubecloud bot commented Jan 2, 2026

@nixel2007 nixel2007 merged commit c6986b0 into develop Jan 2, 2026
41 checks passed
@nixel2007 nixel2007 deleted the copilot/fix-nullpointerexception-bsl-parser branch January 2, 2026 10:05
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.

NullPointerException: Cannot invoke "com.github._1c_syntax.bsl.parser.BSLParser$ExpressionContext.getChildCount()" because "ctx" is null

2 participants