Skip to content

fix: remove implicit narrowing conversion in CipherInputStream.skip - #522

Closed
sharmabikram wants to merge 1 commit into
aws:mainfrom
sharmabikram:fix/cipherinputstream-skip-narrowing
Closed

sharmabikram wants to merge 1 commit into
aws:mainfrom
sharmabikram:fix/cipherinputstream-skip-narrowing

Conversation

@sharmabikram

Copy link
Copy Markdown
Contributor

Issue

GitHub code scanning flags an implicit narrowing conversion in CipherInputStream.skip(long) (alert code-scanning/3). The compound assignment currentPosition += n is int += long, which implicitly narrows the long back to int.

Fix

The buffered remainder (maxPosition - currentPosition) is bounded by the input buffer size (512 bytes by default) and always fits in an int. The requested skip is now clamped to that range as an int before the assignment, so:

  • there is no implicit long -> int narrowing on the compound assignment, and
  • the value added to currentPosition cannot overflow.

Behavior is unchanged for every input:

input before after
n within buffered remainder skip n, advance skip n, advance
n > buffered remainder clamp to remainder clamp to remainder
n == 0 return 0, no move return 0, no move
n < 0 return 0, no move return 0, no move
n > Integer.MAX_VALUE clamp to remainder clamp to remainder

Tests

Adds CipherInputStreamTest (5 cases, JUnit 5) covering skip within the buffer, clamping to the buffered remainder, zero, negative, and Long.MAX_VALUE inputs. mvn -Dtest=CipherInputStreamTest test -> Tests run: 5, Failures: 0, Errors: 0.

Notes

The pre-existing SME assessment on the internal tracking ticket is that this is a low-severity, benign finding (currentPosition never approaches Integer.MAX_VALUE in practice). This change resolves the static-analysis alert with a behavior-preserving refactor and adds test coverage where there previously was none for skip.

The compound assignment `currentPosition += n` narrowed the long
parameter to int implicitly, which GitHub code scanning flags. The
buffered remainder is bounded by the buffer size and always fits in an
int, so clamp the requested skip to that range as an int before the
assignment. Behavior is unchanged for all inputs (in-range, zero,
negative, and values beyond Integer.MAX_VALUE).

Adds CipherInputStreamTest covering skip within the buffer, clamping to
the buffered remainder, zero, negative, and Long.MAX_VALUE inputs.
@sharmabikram
sharmabikram requested a review from a team as a code owner September 24, 2026 19:17
@sharmabikram

Copy link
Copy Markdown
Contributor Author

Closing — will resubmit without a fork.

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