`square() silently wraps on 64-bit integer overflow (extension-functions.c)`
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
- 72/100
Direzione di ricerca
Inizia da squareFunc e dalla sua voce di registrazione { "square", 1, 0, ... } in src/main/ext/extension-functions.c; il ramo intero esegue iVal*iVal e restituisce int64 senza controlli, mentre assert(argc==2) contraddice la registrazione con nArg=1. Decidi se promuovere a double (rispecchiando il comportamento di overflow di SQL *) o sollevare SQLITE_ERROR "integer overflow", correggi l'assert, poi ricostruisci la libreria nativa ed esegui la riproduzione JDBC nell'issue; fatto significa che tutti e tre gli input di overflow restituiscono un REAL o un errore e nessun valore avvolto/negativo. Controlla la struttura dei test esistente sotto src/test per dove aggiungere un caso di regressione.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Descrizione
Describe the bug
square(X) returns a mathematically wrong value when X is a 64-bit INTEGER whose square overflows int64. The integer branch in src/main/ext/extension-functions.c multiplies with raw int64 arithmetic and returns the wrapped result as int64, instead of promoting to floating point or raising an error. Since these contrib functions are compiled into the bundled native library and registered on every connection (pragma_function_list reports builtin=0 for square), plain JDBC users hit it with no load_extension needed:
square(9223372036854775807)returns1((2^63-1)^2 mod 2^64)square(-9223372036854775808)returns0square(-3037000500)returns a negative number-9223372036709301616
For the same input, SQLite's own * operator returns 9.22337203700025E18, because lang_expr.html states arithmetic uses "floating point arithmetic, per IEEE Standard 754, ... if integer arithmetic would produce an overflow". square() bypasses that rule.
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()) {
String[] q = {
"SELECT square(9223372036854775807)",
"SELECT square(-9223372036854775808)",
"SELECT square(-3037000500)",
"SELECT -3037000500 * -3037000500"
};
for (String x : q) {
try (ResultSet r = s.executeQuery(x)) {
r.next();
System.out.println(x + " => " + r.getObject(1));
}
}
}
}
}
Expected behavior
square(X) should return the square of X, i.e. for integer input either 9.22337203700025e18 (promoted to REAL, the way SQL's * behaves on overflow) or an error such as integer overflow — never a wrapped value, and never a negative result for -3037000500.
Reference point: SQL Server defines the same function as SQUARE(float_expression) RETURNS float, so no integer wraparound occurs there.
Logs
SELECT square(9223372036854775807) => 1
SELECT square(-9223372036854775808) => 0
SELECT square(-3037000500) => -9223372036709301616
SELECT -3037000500 * -3037000500 => 9.22337203700025E18
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:
static void squareFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
i64 iVal = 0;
double rVal = 0.0;
assert( argc==2 ); /* registered with nArg = 1 below */
switch( sqlite3_value_type(argv[0]) ){
case SQLITE_INTEGER: {
iVal = sqlite3_value_int64(argv[0]);
sqlite3_result_int64(context, iVal*iVal); /* no overflow check */
break;
}
...
Two defects:
iVal*iValis unchecked and the result is returned as int64, so overflow silently wraps.assert(argc==2)contradicts the registration{ "square", 1, 0, SQLITE_UTF8, 0, squareFunc }; a debug build would abort on the first call, which suggests the function is untested.
Suggested fix: if the int64 product would overflow, either compute in double (as the non-integer branch already does) or raise SQLITE_ERROR "integer overflow", mirroring how abs() handles INT64_MIN. Also correct the assertion to assert(argc==1).
Note: square is not the only function registered from this file. ATN2 CHARINDEX COT COTH DIFFERENCE LEFTSTR PADC PADL PADR PROPER REPLICATE REVERSE RIGHTSTR STDEV VARIANCE come from the same source and appear to share the same unchecked-arithmetic pattern; they deserve the same review.
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
- Nessun Dockerfile né file Docker Compose
- Nessun modello di pull request
- Leggi la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Altre issue di xerial/sqlite-jdbc
-
triage
Difficoltà 2/5 1-3 ore Idoneità per principianti 72/100
xerial/sqlite-jdbc#1515 ·
I maintainer di solito rispondono entro 1 giorno
-
triage
Difficoltà 2/5 1-3 ore Idoneità per principianti 74/100
xerial/sqlite-jdbc#1514 ·
I maintainer di solito rispondono entro 1 giorno
-
triage
Difficoltà 2/5 1-3 ore Idoneità per principianti 78/100
xerial/sqlite-jdbc#1513 ·
I maintainer di solito rispondono entro 1 giorno
-
SIGSEGV (exit 134) in bundled `rightstr()` when the length argument is a large negative numberForse già presa @sarthakdev143-lite l’ha presa 2 giorni fa. Apertahelp wanted impacts:JNI
Difficoltà 4/5 1-2 giorni Idoneità per principianti 38/100
xerial/sqlite-jdbc#1508 ·
I maintainer di solito rispondono entro 1 giorno
-
triage
Difficoltà 4/5 3-5 giorni Idoneità per principianti 48/100
xerial/sqlite-jdbc#1483 ·
I maintainer di solito rispondono entro 1 giorno
Tutte le issue di xerial/sqlite-jdbc
Issue simili
-
hermes-queue
Difficoltà 1/5 1-3 ore Idoneità per principianti 85/100
chrisalunlloyd2-sudo/mindpalace#403 ·
I maintainer di solito rispondono entro 1 giorno
-
Area/Configuration Type/Improvement
Difficoltà 2/5 1-3 ore Idoneità per principianti 68/100
ballerina-platform/static-code-analysis-tool#217 ·
I maintainer di solito rispondono entro 1 giorno
-
SequenceEncoder: a value ending with a backslash swallows the tokens after it (“No state for trait”)Aperta
Difficoltà 2/5 1-3 ore Idoneità per principianti 80/100
vassalengine/vassal#15337 ·
I maintainer di solito rispondono entro 1 giorno
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 76/100
eclipse-dirigible/dirigible#8636 · 1 commento ·
I maintainer di solito rispondono entro 1 giorno
-
bug severity-medium XS
Difficoltà 2/5 1-3 ore Idoneità per principianti 80/100
nikolas-charalambidis/facepalm-maven-plugin#291 ·
I maintainer di solito rispondono entro 1 giorno