SONARJAVA-6786 Implement new rule S9346: Integer values should not be cast to long for use as timestamps - #5957
SONARJAVA-6786 Implement new rule S9346: Integer values should not be cast to long for use as timestamps#5957romainbrenguier wants to merge 4 commits into
Conversation
Detect 32-bit or smaller integer values (int, short, byte, char) passed as arguments to timestamp-consuming APIs (Date, Timestamp, Instant, Calendar), where the narrow type causes overflow or data corruption.
| private void checkArgument(ExpressionTree argument) { | ||
| ExpressionTree arg = ExpressionUtils.skipParentheses(argument); | ||
| if (arg.is(Tree.Kind.TYPE_CAST)) { | ||
| arg = ((TypeCastTree) arg).expression(); | ||
| } | ||
| Type type = arg.symbolType(); | ||
| if (type.isUnknown()) { | ||
| return; | ||
| } | ||
| if (isNarrowIntegerType(type)) { | ||
| reportIssue(argument, MESSAGE); | ||
| } | ||
| } |
There was a problem hiding this comment.
⚠️ Bug: Rule misses documented case: int cast stored in long variable
The canonical Noncompliant example in S9346.html stores (long) timestamp in a long variable and then passes that variable to new Date(epochMillis). The implementation's checkArgument only inspects the direct argument at the call site (skipping parentheses and unwrapping a single TypeCast), so a long-typed variable argument yields a non-narrow type and is never reported. The primary documented pattern is therefore a false negative and is not covered by the sample test file. Either narrow the documentation example to match what the check detects (cast/int directly in the call), or extend the check to trace the argument's initializer when it is a local variable assigned from a narrow-int cast, and add a corresponding test case.
Was this helpful? React with 👍 / 👎
|
❌ Ruling needs updating. A fix PR has been created: #5958 Please review and merge it into your branch. |
Ruling Diff SummaryDetected changes in 1 rule files: 0 issues removed, 2 issues added. S9346 (
|
- Fix secondary location marker alignment in test sample (off by one space) - Exclude int literal arguments (e.g., `new Date(0)`) from detection to reduce false positives on intentional small values - Update HTML noncompliant example to show patterns the rule actually detects (direct int arg and explicit cast) instead of the variable indirection pattern which is not detected Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
The previous commit excluded int literals from S9346, which means the eclipse-jetty findings at JSONTest.java lines 366 and 433 are no longer raised. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Code Review
|
| Auto-apply | Compact | Unblock |
|
|
|
Was this helpful? React with 👍 / 👎 | Gitar
|




Detect 32-bit or smaller integer values (int, short, byte, char) passed as arguments to timestamp-consuming APIs (Date, Timestamp, Instant, Calendar), where the narrow type causes overflow or data corruption.