Skip to content

Widen volumeHandle to long before shifting by 48 (CodeQL java/lshift-larger-than-type-width)#231

Open
vharseko wants to merge 2 commits into
OpenIdentityPlatform:masterfrom
vharseko:features/codeql-lshift-width
Open

Widen volumeHandle to long before shifting by 48 (CodeQL java/lshift-larger-than-type-width)#231
vharseko wants to merge 2 commits into
OpenIdentityPlatform:masterfrom
vharseko:features/codeql-lshift-width

Conversation

@vharseko

@vharseko vharseko commented Jul 8, 2026

Copy link
Copy Markdown
Member

Summary

Resolves the single java/lshift-larger-than-type-width CodeQL alert in
IOMeter.java.

IOMeter.dump() builds a composite key that groups journal events by
(volumeHandle, pageAddress):

final int volumeHandle = is.readInt();
final long pageAddress = is.readLong();
...
final long handle = (volumeHandle << 48) + pageAddress;   // bug

volumeHandle is an int, and Java masks an int shift distance to its low
5 bits, so << 48 is actually evaluated as << (48 & 31) = << 16 in 32-bit
arithmetic and only then widened to long. As a result:

  • the volume handle lands in bits 16–47 instead of 48+, and
  • volumeHandle << 16 can overflow int,

so events for different volumes/pages can collide on the same HashMap key.

Fix

Cast to long before the shift so it is performed in 64 bits:

final long handle = ((long) volumeHandle << 48) + pageAddress;

This only affects the -a (analyze pages) mode of the journal-dump diagnostic
utility; it is not on a data path.

Testing

mvn -o -pl persistit/core -am compile — BUILD SUCCESS.

…larger-than-type-width)

In IOMeter.dump() the composite event key was built as
"(volumeHandle << 48) + pageAddress". volumeHandle is an int, and Java uses
only the low 5 bits of an int shift distance, so "<< 48" was silently
evaluated as "<< 16" in 32-bit arithmetic before being widened to long. The
handle therefore placed volumeHandle in the wrong bit range (and could
overflow int), so events for different volumes/pages could collide on the
same HashMap key when dumping with analyzePages.

Cast volumeHandle to long first so the shift is performed in 64 bits:
((long) volumeHandle << 48) + pageAddress. This only affects the -a page
analysis of the journal dump utility.
@vharseko vharseko added bug java codeql CodeQL static-analysis findings labels Jul 8, 2026
@vharseko vharseko requested a review from maximthomas July 8, 2026 12:41
@vharseko vharseko removed the java label Jul 8, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug codeql CodeQL static-analysis findings

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants