rightstr(s, negative) triggers a huge allocation and returns SQLITE_NOMEM

Open Beginner friendly
#1,513 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
78/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
c, java
Domain
database

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

triage

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

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from xerial/sqlite-jdbc

All issues in xerial/sqlite-jdbc

Similar issues

More Java issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.