Skip to content

Commit cb4a5a6

Browse files
committed
upstream fix for fts5 secure-delete corruption (cc0f82a480a400c6)
1 parent ee99751 commit cb4a5a6

File tree

2 files changed

+24
-14
lines changed

2 files changed

+24
-14
lines changed

ext/fts5/fts5_index.c

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4711,7 +4711,6 @@ static void fts5DoSecureDelete(
47114711
int iIdx = 0;
47124712
int iStart = 0;
47134713
int iKeyOff = 0;
4714-
int iPrevKeyOff = 0;
47154714
int iDelKeyOff = 0; /* Offset of deleted key, if any */
47164715

47174716
nIdx = nPg-iPgIdx;
@@ -4893,26 +4892,24 @@ static void fts5DoSecureDelete(
48934892
}
48944893

48954894
if( p->rc==SQLITE_OK ){
4896-
const int nMove = nPg - iNextOff;
4897-
int nShift = 0;
4895+
const int nMove = nPg - iNextOff; /* Number of bytes to move */
4896+
int nShift = iNextOff - iOff; /* Distance to move them */
4897+
int iPrevKeyOut = 0;
4898+
int iKeyIn = 0;
48984899

48994900
memmove(&aPg[iOff], &aPg[iNextOff], nMove);
4900-
iPgIdx -= (iNextOff - iOff);
4901+
iPgIdx -= nShift;
49014902
nPg = iPgIdx;
49024903
fts5PutU16(&aPg[2], iPgIdx);
49034904

4904-
nShift = iNextOff - iOff;
4905-
for(iIdx=0, iKeyOff=0, iPrevKeyOff=0; iIdx<nIdx; /* no-op */){
4905+
for(iIdx=0; iIdx<nIdx; /* no-op */){
49064906
u32 iVal = 0;
49074907
iIdx += fts5GetVarint32(&aIdx[iIdx], iVal);
4908-
iKeyOff += iVal;
4909-
if( iKeyOff!=iDelKeyOff ){
4910-
if( iKeyOff>iOff ){
4911-
iKeyOff -= nShift;
4912-
nShift = 0;
4913-
}
4914-
nPg += sqlite3Fts5PutVarint(&aPg[nPg], iKeyOff - iPrevKeyOff);
4915-
iPrevKeyOff = iKeyOff;
4908+
iKeyIn += iVal;
4909+
if( iKeyIn!=iDelKeyOff ){
4910+
int iKeyOut = (iKeyIn - (iKeyIn>iOff ? nShift : 0));
4911+
nPg += sqlite3Fts5PutVarint(&aPg[nPg], iKeyOut - iPrevKeyOut);
4912+
iPrevKeyOut = iKeyOut;
49164913
}
49174914
}
49184915

ext/fts5/test/fts5secure.test

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,19 @@ do_execsql_test 5.3 {
273273
do_execsql_test 5.4 { SELECT rowid FROM t1('abc'); } 2
274274
do_execsql_test 5.5 { SELECT rowid FROM t1('aa'); } 2
275275

276+
#-------------------------------------------------------------------------
277+
reset_db
278+
279+
do_execsql_test 6.0 {
280+
CREATE VIRTUAL TABLE fts USING fts5(content);
281+
INSERT INTO fts(fts, rank) VALUES ('secure-delete', 1);
282+
INSERT INTO fts(rowid, content) VALUES
283+
(3407, 'profile profile profile profile profile profile profile profile pull pulling pulling really');
284+
DELETE FROM fts WHERE rowid IS 3407;
285+
INSERT INTO fts(fts) VALUES ('integrity-check');
286+
}
287+
288+
276289

277290
finish_test
278291

0 commit comments

Comments
 (0)