Skip to content

SONARJAVA-6197 S1451 should not fail at analysis time when an empty headerFormat rule property marked as a regular expression is provided#5577

Open
NoemieBenard wants to merge 5 commits intomasterfrom
nb/sonarjava-6197
Open

SONARJAVA-6197 S1451 should not fail at analysis time when an empty headerFormat rule property marked as a regular expression is provided#5577
NoemieBenard wants to merge 5 commits intomasterfrom
nb/sonarjava-6197

Conversation

@NoemieBenard
Copy link
Copy Markdown
Contributor

Modify S1451 to handle empty header separately whether or not it is marked as a regular expression.

@hashicorp-vault-sonar-prod
Copy link
Copy Markdown
Contributor

hashicorp-vault-sonar-prod bot commented Apr 17, 2026

SONARJAVA-6197

@sonar-review-alpha
Copy link
Copy Markdown
Contributor

sonar-review-alpha bot commented Apr 17, 2026

Summary

What changed: Modified S1451 (FileHeaderCheck) to gracefully handle empty headerFormat rule properties without crashing at analysis time, whether or not regex mode is enabled.

Why: The original code tried to compile empty headerFormat strings as regex patterns when isRegularExpression=true, which caused an IllegalArgumentException. The fix detects empty headers upfront and treats them as no-header-required (equivalent to expectedLines=[]), skipping regex compilation entirely.

The fix: Added an early check in setContext() that catches empty headerFormat before attempting regex compilation, setting expectedLines to an empty array and disabling regex mode.

What reviewers should know

Start here: Look at FileHeaderCheck.setContext() to see the early-exit logic for empty headers (lines 60-65). This is the core fix.

Understanding the issue:

  • Previously, empty headerFormat + isRegularExpression=true would try Pattern.compile("", DOTALL) → crash
  • Now empty headers are handled uniformly before regex compilation is attempted

Test coverage: New tests verify the fix works for both regex and non-regex modes with empty headers:

  • Class4/Class5: default config (no regex) with empty header
  • Regex5/Regex6: regex-enabled config with empty header
  • Files start with different whitespace patterns to ensure the fix handles edge cases

Minor change: Removed unnecessary blank line in checkHeader() method (line 121).


  • Generate Walkthrough
  • Generate Diagram

🗣️ Give feedback

Copy link
Copy Markdown
Contributor

@sonar-review-alpha sonar-review-alpha bot left a comment

Choose a reason for hiding this comment

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

The fix is correct and covers both sides of the bug: the crash when empty regex is compiled, and a pre-existing false-positive where a plain-text empty header was split to [""] rather than [], causing files like Class5.java to get incorrectly flagged. One design concern is worth noting below.

🗣️ Give feedback

Comment on lines +61 to +63
if (headerFormat.isEmpty()) {
expectedLines = new String[]{};
isRegularExpression = false;
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.

isRegularExpression is a @RuleProperty-annotated public field set by the SonarQube framework to represent user configuration. Mutating it inside setContext() means the field no longer reflects the user's intent after the first file is scanned.

In practice this is harmless — the isEmpty() guard is re-evaluated on every call so the mutation is always re-applied for empty headers. But if the framework ever inspects or logs the field value after scanning (for diagnostics or rule-parameter serialization), it will see false even when the user explicitly set true.

A cleaner approach: replace the mutation with a local boolean that shadows the field for the duration of the call:

boolean effectiveRegex = isRegularExpression;
if (headerFormat.isEmpty()) {
  expectedLines = new String[]{};
  effectiveRegex = false;
} else if (isRegularExpression) {
  ...
}

This keeps rule-property state immutable and makes the intent explicit.

Suggested change
if (headerFormat.isEmpty()) {
expectedLines = new String[]{};
isRegularExpression = false;
if (headerFormat.isEmpty()) {
expectedLines = new String[]{};
} else {
if (isRegularExpression) {
if (searchPattern == null) {
try {
searchPattern = Pattern.compile(getHeaderFormat(), Pattern.DOTALL);
} catch (IllegalArgumentException e) {
throw new IllegalArgumentException("[" + getClass().getSimpleName() + "] Unable to compile the regular expression: " + headerFormat, e);
}
}
} else {
expectedLines = headerFormat.split("(?:\r)?\n|\r");
}
}
  • Mark as noise

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.

1 participant