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

Aperta Adatta ai principianti
#1,513 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
2/5
Tempo stimato
1-3 ore
Idoneità per principianti
78/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
c, java
Ambito
database

Direzione di ricerca

Inizia in src/main/ext/extension-functions.c: leggi rightFunc() e confrontala con la funzione sorella leftFunc(), che restituisce già una stringa vuota per lunghezze negative. Aggiungi lo stesso limite n < 0 prima dell'aritmetica offset/dimensione e mantieni l'allocazione protetta. Fatto significa che il repro Java seleziona rightstr('abcdef', -1) senza SQLITE_NOMEM, restituendo '' o un SQLITE_ERROR regolare, coerentemente con rightstr('abcdef', 0).

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

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.

Lingua principale
Java
Stelle
3.3k
Fork
676
Merge medio
1g 15h
PR unite (30g)
18

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di xerial/sqlite-jdbc

Tutte le issue di xerial/sqlite-jdbc

Issue simili

Altre issue su Java

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.