Skip to content

SONARJAVA-7228 Properly resolve constant chars in string concatenations - #6393

Merged
aurelien-coet-sonarsource merged 3 commits into
masterfrom
ac/SONARJAVA-7228
Oct 26, 2026
Merged

aurelien-coet-sonarsource merged 3 commits into
masterfrom
ac/SONARJAVA-7228

Conversation

@aurelien-coet-sonarsource

@aurelien-coet-sonarsource aurelien-coet-sonarsource commented Oct 23, 2026 •

Copy link
Copy Markdown
Contributor

Summary by Gitar

  • Character constant resolution:
    • Resolves char literals and char constants in string concatenations to their character values in ExpressionUtils
    • Decodes Unicode escapes before escape processing via LiteralUtils.charLiteralValue
  • Security check tests:
    • Adds noncompliant samples for char operands in PubliclyWritableDirectories, FilePermissionsCheckSample, and WeakSSLContextCheckJava8
  • Duplicate collection check:
    • Distinguishes char and int constants so Set.of('a', 97) is not flagged as duplicate in DuplicateImmutableCollectionArgumentsCheck
  • False positive fix:
    • Marks the char-placeholder query in PreparedStatementAndResultSetCheckSample as compliant now that the frontend resolves it correctly

This will update automatically on new commits.

@hashicorp-vault-sonar-prod

hashicorp-vault-sonar-prod Bot commented Oct 23, 2026 •

Copy link
Copy Markdown
Contributor

SONARJAVA-7228

@datadog-sonarsource

datadog-sonarsource Bot commented Oct 23, 2026 •

Copy link
Copy Markdown

Pipelines

This comment will be updated automatically if new data arrives.
🔗 Commit SHA: 942a784 | Docs | Give us feedback!

@aurelien-coet-sonarsource
aurelien-coet-sonarsource marked this pull request as ready for review October 23, 2026 14:18

@asya-vorobeva asya-vorobeva left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Generally LGTM, but should be merged after #6379 with fix of test uncompliant sample.

@gitar-bot

gitar-bot Bot commented Oct 26, 2026

Copy link
Copy Markdown
Code Review ✅ Approved 1 closed / 1 findings

🟡 Medium risk · Resolving character constants in concatenations changes analyzer results across security and collection checks, potentially exposing missed findings or new false positives.

Resolves char literals and char constants in string concatenations to their character values, and decodes Unicode escapes before escape processing. Char literals are no longer treated as equal to int constants in the duplicate immutable collection argument check, addressing that finding. No open issues remain.

✅ 1 closed
✅ Bug: Char literals now equal int constants in duplicate-element check

📄 java-frontend/src/main/java/org/sonar/java/model/ExpressionUtils.java:257-259
After this change, resolveAsConstant turns a char literal into its promoted Integer value: 'a' becomes Integer(97) where it used to be null. DuplicateImmutableCollectionArgumentsCheck.areEquivalent compares e1.asConstant() with e2.asConstant() using equals and never checks the types of the source expressions.

So Set.of('a', 97) or Map.of('a', x, 97, y) now gets a duplicate-element/key issue. At runtime these values are a boxed Character and a boxed Integer, which are not equal, so the warning is a false positive. Char constants already had this problem before this PR, but the PR extends it to every char literal. The check's sample file has no char cases, so tests don't catch it.

Fix: in areEquivalent, treat two expressions as equivalent only when their resolved constants are equal and their symbolType()s match (or both are char/neither is char). Another option is to keep char values distinct in the resolver.

Verification Rules 2/2 met Repos 4 inspected · PRs 1 inspected

Functional validation No results

Rules 2 passed 1 not applicable

✅ Internal artifact registries
✅ Repository license compliance

Broader impact Repos 4 inspected PRs 1 inspected
Cross-repo 4 repositories inspected 0 open impacts

SonarSource/sonar-java-symbolic-execution Inspected
SonarSource/sonarlint-language-server Inspected
SonarSource/sonarlint-vscode Inspected
SonarSource/sonarlint-intellij Inspected

Cross-PR 1 PR inspected 0 open impacts

PR 6379: SONARJAVA-7215 Do not report S2695 when part of the query cannot be resolved Inspected

Auto-approval Not enabled

Set up

For more verification details, comment gitar display:verbose.

Options

Auto-apply is off → Gitar will not commit updates to this branch.
Display: compact → Showing compact verification results.

Comment with these commands to change the behavior for this request:

Auto-apply Compact
gitar auto-apply:on         
gitar display:verbose         

Was this helpful? React with 👍 / 👎 | Gitar

@sonarqube-next

Copy link
Copy Markdown
Contributor

@aurelien-coet-sonarsource
aurelien-coet-sonarsource merged commit d7866af into master Oct 26, 2026
18 checks passed
@aurelien-coet-sonarsource
aurelien-coet-sonarsource deleted the ac/SONARJAVA-7228 branch October 26, 2026 11:55
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.

2 participants