Widen int multiplications to long before overflow (CodeQL java/integer-multiplication-cast-to-long)#227
Open
vharseko wants to merge 2 commits into
Conversation
…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.
maximthomas
approved these changes
Jul 8, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Resolves 4
java/integer-multiplication-cast-to-longCodeQL alerts. In each spot anint * intproduct is computed in 32-bit arithmetic and only afterwards widened tolong(by assignment, alongparameter, or along-context division), so a largeoperand overflows before the widening. Forcing the multiplication into
longwithan
Lsuffix fixes it.Fixes
persistit/coreJournalManagerBenchprealloc * 1024 * 1024→1024L(product passed topreallocateFile(long)) andextension * 1024 * 1024→1024L(assigned to along) — MByte → byte-count conversions.persistit/examplesSimpleTransaction_committedTransactionCount * 1000→1000Lbefore the divide by thelongelapsed time; the committed count can exceed ~2.1M in a long run.persistit/uiAdminUI_refreshInterval * 1000→1000L, passed as thelongperiod ofTimer.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.SimpleTransactionis an Ant example (no Maven module); the one-character change iscompile-trivial.