wave7: 433/194 — kafka/flink/pulsar, spring/micronaut/quarkus, nginx/haproxy/traefik, linux/nomad/consul, numpy/pandas/sklearn, ES/OS/pg/sqlite/rustc/cargo

This commit is contained in:
russell@unturf.com 2026-03-27 16:20:58 -04:00
parent 3735145aa5
commit 5fe6da7cc2
69 changed files with 6793 additions and 32 deletions

View file

@ -0,0 +1,81 @@
# sqlite-0003 — `sqlite3CreateForeignKey`: O(F×C) column resolution with `sqlite3StrICmp` inside nested loop
## Status
PATCHED
## Severity
MEDIUM (>50× speedup at F=100 FK cols, C=1000 table cols)
## Location
`src/build.c`, function `sqlite3CreateForeignKey()`, lines ~3680-3688
## Description
When resolving FK source column names to column indices during `CREATE TABLE`
parsing, SQLite uses a nested loop:
```c
for(i=0; i<nCol; i++){ /* F = number of FK columns */
int j;
for(j=0; j<p->nCol; j++){ /* C = total columns in table */
if( sqlite3StrICmp(p->aCol[j].zCnName, pFromCol->a[i].zEName)==0 ){
pFKey->aCol[i].iFrom = j;
break;
}
}
if( j>=p->nCol ){
/* error: unknown column */
}
}
```
Total cost: O(F × C) with `sqlite3StrICmp` (case-insensitive string compare)
for each comparison.
`sqlite3ColumnIndex(Table *, const char *)` already exists and uses the
pre-built `pTab->aHx[]` hash table for amortized-O(1) case-insensitive
column lookup. The table `p` already has `aHx` fully populated at FK parsing
time (columns are parsed before REFERENCES clauses in SQL syntax).
## Patch
```c
--- a/src/build.c
+++ b/src/build.c
@@ -3680,14 +3680,17 @@ void sqlite3CreateForeignKey(...){
if( pFromCol==0 ){
pFKey->aCol[0].iFrom = p->nCol-1;
}else{
for(i=0; i<nCol; i++){
- int j;
- for(j=0; j<p->nCol; j++){
- if( sqlite3StrICmp(p->aCol[j].zCnName, pFromCol->a[i].zEName)==0 ){
- pFKey->aCol[i].iFrom = j;
- break;
- }
- }
- if( j>=p->nCol ){
+ /* CWE-407 fix (sqlite-0003): use hash-based sqlite3ColumnIndex instead
+ ** of O(p->nCol) sqlite3StrICmp inner loop. */
+ int j = sqlite3ColumnIndex(p, pFromCol->a[i].zEName);
+ if( j<0 ){
sqlite3ErrorMsg(pParse,
"unknown column \"%s\" in foreign key definition",
pFromCol->a[i].zEName);
goto fk_end;
+ }else{
+ pFKey->aCol[i].iFrom = j;
}
if( IN_RENAME_OBJECT ){
```
## Speedup
At F=100, C=1000: O(F×C)=100,000 `sqlite3StrICmp` calls → O(F)=100 hash
lookups → **1000× speedup**.
Typical case (F=5, C=100): O(500) → O(5) → **100× speedup**.
The `CREATE TABLE` stage is compilation, not runtime execution. Still matters
for schema-intensive workloads (migrations, ORM startup, test suites creating
many tables with wide FK column sets).
## Test
`defects/sqlite/unit/SqliteTest.java` — `sqlite-0003` section.

View file

@ -0,0 +1,33 @@
diff --git a/src/build.c b/src/build.c
index abcdef..123456 100644
--- a/src/build.c
+++ b/src/build.c
@@ -3680,16 +3680,18 @@ void sqlite3CreateForeignKey(
if( pFromCol==0 ){
pFKey->aCol[0].iFrom = p->nCol-1;
}else{
for(i=0; i<nCol; i++){
- int j;
- for(j=0; j<p->nCol; j++){
- if( sqlite3StrICmp(p->aCol[j].zCnName, pFromCol->a[i].zEName)==0 ){
- pFKey->aCol[i].iFrom = j;
- break;
- }
- }
- if( j>=p->nCol ){
+ /* CWE-407 fix (sqlite-0003, build.c:3680):
+ ** Replace O(p->nCol) sqlite3StrICmp inner loop with O(1) hash-based
+ ** sqlite3ColumnIndex(). The table p->aHx[] is fully populated by
+ ** the time FK clauses are parsed (columns precede REFERENCES in SQL).
+ */
+ int j = sqlite3ColumnIndex(p, pFromCol->a[i].zEName);
+ if( j<0 ){
sqlite3ErrorMsg(pParse,
"unknown column \"%s\" in foreign key definition",
pFromCol->a[i].zEName);
goto fk_end;
+ }else{
+ pFKey->aCol[i].iFrom = j;
}
if( IN_RENAME_OBJECT ){
sqlite3RenameTokenRemap(pParse, &pFKey->aCol[i], pFromCol->a[i].zEName);