rightstr(s, negative) triggers a huge allocation and returns SQLITE_NOMEM
Maintainers usually reply within 1 day
Nobody has claimed this yet.
Assessment
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Newbie friendliness
- 78/100
Research direction
Start in src/main/ext/extension-functions.c: read rightFunc() and compare it with the sibling leftFunc(), which already returns an empty string for negative lengths. Add the same n < 0 clamp before the offset/size arithmetic and keep the allocation guarded. Done means the Java repro selects rightstr('abcdef', -1) without SQLITE_NOMEM, returning '' or a regular SQLITE_ERROR, consistent with rightstr('abcdef', 0).
Written by the indexing model from the issue text.
Description
Describe the bug
rightstr(s, n) with a negative n does not validate the argument before computing the allocation size. The size arithmetic underflows, sqlite3_malloc() receives an enormous value, and the call fails with SQLITE_NOMEM. The same input can fatally abort the connection in other contexts. A negative length should be treated as 0 (returning an empty string) or rejected with a normal error, never turned into a huge allocation.
To Reproduce
import java.sql.*;
public class Repro {
public static void main(String[] args) throws Exception {
try (Connection c = DriverManager.getConnection("jdbc:sqlite::memory:");
Statement s = c.createStatement()) {
try (ResultSet r = s.executeQuery("SELECT rightstr('abcdef', -1)")) {
r.next();
System.out.println("=> [" + r.getObject(1) + "]");
}
}
}
}
Expected behavior
rightstr('abcdef', -1) should return an empty string (''), the same way rightstr('abcdef', 0) does, or raise a regular SQLITE_ERROR. It must not attempt an unbounded allocation and must not return SQLITE_NOMEM.
Logs
SELECT rightstr('abcdef', 0) => (empty string)
SELECT rightstr('abcdef', -1) => [SQLITE_NOMEM] A malloc() failed (out of memory)
Environment (please complete the following information):
- OS: macOS 15.6.1
- CPU architecture: arm64
- sqlite-jdbc version: 3.54.1.0
Additional context
Root cause in src/main/ext/extension-functions.c, rightFunc(): n is taken via sqlite3_value_int() and used directly when computing the result buffer size (sqlite3_malloc(ze - zt + 1)), without a n < 0 guard. With a negative n the length expression goes negative and is converted to a huge size_t, so the allocation request is absurd.
Suggested fix: clamp n to 0 when negative before computing the offset/size (or reject it explicitly), and guard the allocation with an overflow check.
Related: leftstr(s, n) handles the same input by returning '', so the two sibling functions disagree on negative lengths.
Found by PST while testing built-in functions locally.
- Dominant language
- Java
- Stars
- 3.3k
- Forks
- 676
- Avg merge
- 1d 15h
- Merged PRs (30d)
- 18
Getting set up
- No Dockerfile or Docker Compose file
- No pull request template
- Read the contributing guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from xerial/sqlite-jdbc
-
triage
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
xerial/sqlite-jdbc#1515 ·
Maintainers usually reply within 1 day
-
triage
Difficulty 2/5 1-3 hours Newbie friendliness 74/100
xerial/sqlite-jdbc#1514 ·
Maintainers usually reply within 1 day
-
triage
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
xerial/sqlite-jdbc#1512 ·
Maintainers usually reply within 1 day
-
SIGSEGV (exit 134) in bundled `rightstr()` when the length argument is a large negative numberPossibly taken @sarthakdev143-lite claimed this 1 day ago. Openhelp wanted impacts:JNI
Difficulty 4/5 1-2 days Newbie friendliness 38/100
xerial/sqlite-jdbc#1508 ·
Maintainers usually reply within 1 day
-
triage
Difficulty 4/5 3-5 days Newbie friendliness 48/100
xerial/sqlite-jdbc#1483 ·
Maintainers usually reply within 1 day
All issues in xerial/sqlite-jdbc
Similar issues
-
Area/Configuration Type/Improvement
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
ballerina-platform/static-code-analysis-tool#217 ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
eclipse-dirigible/dirigible#8636 · 1 comment ·
Maintainers usually reply within 1 day
-
bug severity-medium XS
Difficulty 2/5 1-3 hours Newbie friendliness 80/100
nikolas-charalambidis/facepalm-maven-plugin#291 ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 62/100
OakLoaf/PluginUpdater#264 ·
Maintainers usually reply within 1 day
-
[Bug]: withStartupTimeout() changes the startup timeout of all containers that use the default wait strategyPossibly taken @aymenjamaaoui claimed this today. Opentype/bug
Difficulty 2/5 1-3 hours Newbie friendliness 64/100
testcontainers/testcontainers-java#12171 ·
Maintainers usually reply within 1 day