Skip to content

fix(native): return empty string for negative rightstr length - #1509

Open
sarthakdev143-lite wants to merge 1 commit into
xerial:masterfrom
sarthakdev143-lite:fix/rightstr-negative-length
Open

sarthakdev143-lite wants to merge 1 commit into
xerial:masterfrom
sarthakdev143-lite:fix/rightstr-negative-length

Conversation

@sarthakdev143-lite

@sarthakdev143-lite sarthakdev143-lite commented Nov 14, 2026 •

Copy link
Copy Markdown

Summary

rightstr(text, length) in the bundled extension-functions.c computed the number of leading characters to skip as c - l. With a negative length that value is larger than the string itself, and the cursor walk that follows has no upper bound, so the pointer moves past the terminating NUL until it reaches an unmapped page:

SELECT rightstr('abcdef', -1000000000);

aborts the whole JVM with SIGSEGV (exit 134) inside NativeDB.step(). No JDBC exception can catch it.

A negative length has always produced the empty string for small magnitudes (e.g. -1), but only because sqlite3_malloc(0) and the following strcpy happened to read an empty value. This returns the empty string explicitly before the walk, which also avoids the c - l signed overflow at INT_MIN.

Tests

ExtensionTest#rightstrNegativeLength covers -1000000000, -1, and a positive control (rightstr('abcdef', 2) = 'ef').

  • Before the fix: the new test aborts the forked JVM (Aborted (core dumped), exit 134).
  • After the fix: Tests run: 8, Failures: 0, Errors: 0, Skipped: 0.

Verified in a clean clone with:

make clean-native && make native CC="gcc -Wno-implicit-function-declaration" && mvn -B -Dtest=ExtensionTest test

The CC override is only for my local GCC 15, which treats the pre-existing implicit RegisterExtensionFunctions declaration as an error; it is not needed by CI.

Note on the committed native libraries

As with #1489, the tests run against the committed libraries under src/main/resources/org/sqlite/native, so the test jobs will fail until Build Native regenerates them. I did not commit rebuilt binaries.

Fixes #1508

Prepared with AI assistance; reviewed and tested by me.

rightstr() computed the number of leading characters to skip as c - l.
A negative length made that larger than the string, and the cursor walk
that followed had no upper bound, so it moved past the terminating nul
until it reached an unmapped page. One statement,
`select rightstr('abcdef', -1000000000)`, therefore killed the JVM with
SIGSEGV (exit 134); no JDBC exception can catch it.

A negative length has always produced the empty string for small
magnitudes, but only because sqlite3_malloc(0) and the subsequent strcpy
happened to read an empty value. Return the empty string explicitly
before the walk. This also avoids the c - l signed overflow at INT_MIN.

Add ExtensionTest#rightstrNegativeLength covering -1000000000, -1 and a
positive-length control.
@sarthakdev143-lite

Copy link
Copy Markdown
Author

Thanks for the approval. The red jobs all have one cause, and it is the new test itself rather than a regression elsewhere: the PR's CI runs mvn test against the prebuilt libraries committed under src/main/resources/org/sqlite/native, and those were built before the C change. rightstrNegativeLength calls rightstr('abcdef', -1000000000), which on the old library is the out-of-bounds walk this PR fixes, so the forked JVM dies (Process Exit Code: 134, Crashed tests: org.sqlite.ExtensionTest) on every job that reaches that test.

I reproduced both sides in a clean Linux container (JDK 25):

  • With the committed libsqlitejdbc.so (the PR's C change not compiled in): mvn -Dtest=ExtensionTest test crashes with exit code 134, as in CI.
  • With a library built from this branch (make native; I had to add -Wno-error=implicit-function-declaration for gcc 15, unrelated to this change): ExtensionTest runs 8 tests, 0 failures, 0 skipped.

So CI should go green once the native libraries are rebuilt, for example with the Build Native workflow from CONTRIBUTING, which commits the updated binaries to the branch. I can't run that workflow from a fork. Until then I would not merge on the current red state. If you would rather have a test that does not take the JVM down on a stale library, I can replace the -1000000000 case with one that only fails an assertion; say so and I will push it.

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.

SIGSEGV (exit 134) in bundled rightstr() when the length argument is a large negative number

2 participants