Skip to content

Widen int multiplications to long before overflow (CodeQL java/integer-multiplication-cast-to-long)#227

Open
vharseko wants to merge 2 commits into
OpenIdentityPlatform:masterfrom
vharseko:features/codeql-int-mult-overflow
Open

Widen int multiplications to long before overflow (CodeQL java/integer-multiplication-cast-to-long)#227
vharseko wants to merge 2 commits into
OpenIdentityPlatform:masterfrom
vharseko:features/codeql-int-mult-overflow

Conversation

@vharseko

@vharseko vharseko commented Jul 8, 2026

Copy link
Copy Markdown
Member

Summary

Resolves 4 java/integer-multiplication-cast-to-long CodeQL alerts. In each spot an
int * int product is computed in 32-bit arithmetic and only afterwards widened to
long (by assignment, a long parameter, or a long-context division), so a large
operand overflows before the widening. Forcing the multiplication into long with
an L suffix fixes it.

Fixes

Module File Change
persistit/core JournalManagerBench prealloc * 1024 * 10241024L (product passed to preallocateFile(long)) and extension * 1024 * 10241024L (assigned to a long) — MByte → byte-count conversions.
persistit/examples SimpleTransaction _committedTransactionCount * 10001000L before the divide by the long elapsed time; the committed count can exceed ~2.1M in a long run.
persistit/ui AdminUI _refreshInterval * 10001000L, passed as the long period of Timer.schedule.

Behaviour

Unchanged for in-range inputs — the products are identical; only the silent 32-bit
overflow at large inputs is removed. All three are benchmark / example / admin-UI
classes, not core data paths.

Testing

mvn -o -pl persistit/core,persistit/ui -am compile — BUILD SUCCESS.
SimpleTransaction is an Ant example (no Maven module); the one-character change is
compile-trivial.

…ger-multiplication-cast-to-long)

In each case the product was computed in int arithmetic and only then widened to
long, so a large enough operand overflows before the conversion. Add an L suffix
to the first factor so the multiplication is performed in long.

- JournalManagerBench.runTest: prealloc*1024*1024 (passed to preallocateFile(long))
  and extension*1024*1024 (assigned to a long) convert an MByte argument to a byte
  count; use 1024L so the byte count is exact for any int MByte value.
- SimpleTransaction: _committedTransactionCount * 1000 is evaluated as int before
  the division by the long "time"; the committed count can exceed ~2.1M in a
  long-running run, overflowing the rate calculation. Use 1000L.
- AdminUI.resetRefreshTimer: _refreshInterval (int seconds) * 1000 is passed as the
  long period of Timer.schedule; use 1000L.

Behaviour is unchanged for in-range inputs; only the silent int overflow at large
inputs is removed. These are benchmark/example/admin-UI classes, not core data
paths.
@vharseko vharseko added codeql CodeQL static-analysis findings java bug labels Jul 8, 2026
@vharseko vharseko requested a review from maximthomas July 8, 2026 11:45
@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