Skip to content

fix(native): avoid signed overflow in square() - #1519

Open
hutiefang76 wants to merge 1 commit into
xerial:masterfrom
hutiefang76:frank/fix-sqlite1512-square-overflow-20261118
Open

hutiefang76 wants to merge 1 commit into
xerial:masterfrom
hutiefang76:frank/fix-sqlite1512-square-overflow-20261118

Conversation

@hutiefang76

@hutiefang76 hutiefang76 commented Nov 18, 2026 •

Copy link
Copy Markdown

Fixes #1512.

square() currently multiplies signed 64-bit integers without checking overflow. Keep the exact INTEGER result through +/-3037000499 and promote larger magnitudes to REAL, matching SQLite's multiplication operator. Also correct the unary function's argument-count assertion.

The 14 parameterized cases compare both the value and SQLite storage type with x * x, covering the safe boundaries, the next integers outside them, both long extrema, small integers, REAL inputs and NULL.

Validation used a Mac/aarch64 JNI built from the C source with the project's make native, after make clean-native. A custom library name/output directory kept bundled binaries unchanged; Maven's org.sqlite.lib.path and org.sqlite.lib.name selected that library.

  • Before the C fix: 14 cases, 4 overflow assertion failures, 0 errors/skips.
  • After rebuilding: all 14 pass.
  • JDK 11 full mvn verify: 519 tests, 0 failures/errors, 8 skipped; Spotless passed.
  • JDK 25 MathFunctionsTest and ExtensionTest: 53 tests, 0 failures/errors, 1 skipped. JVM loading diagnostics confirm the rebuilt JNI was loaded.

This PR contains source and tests only. The official Build Native run 44288275645 built all 24 native targets successfully. Its final automatic push failed with HTTP 403 because the bot token is read-only; the workflow as a whole is therefore failed.

All 24 artifact ZIP digests were checked against GitHub metadata. The generated libraries were recovered through a normal user push solely on a separate verification branch. A fresh JDK 11 full verify using Maven's default JNI loader and the cross-compiled Mac/aarch64 artifact completed with 519 test records: 511 passed, 0 failures/errors and 8 skipped.

Official CI on verification commit aa41ddff22ba is queued; runtime results for other platforms remain pending. This PR's source commit and bundled binaries are unchanged. Upstream PR CI still needs maintainer approval, and tests using older bundled binaries do not validate the C change.

AI assistance: DeepSeek generated the initial patch/tests and performed a first-pass review. Codex checked the complete diff and executed the native validation.

Preserve exact integer squares within the signed 64-bit range and promote
larger magnitudes to REAL, matching SQLite multiplication semantics.

Fixes xerial#1512

Generated-by: DeepSeek deepseek-v4-pro
Generated-by: OpenAI Codex
@hutiefang76

Copy link
Copy Markdown
Author

The upstream JDK 21 job reports the four overflow failures seen before the C fix. It runs mvn test against the merge ref and copies the checked-in native resources. The Linux x86-64 library in this source PR has the same Git blob as its base.

The verification matrix with the JNI libraries rebuilt from this exact source commit has now passed. The rebuilt libraries are on the separate verification branch linked in the description.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

square() silently wraps on 64-bit integer overflow (extension-functions.c)

2 participants