1
0
mirror of https://github.com/sqlite/sqlite.git synced 2025-07-30 19:03:16 +03:00

Following a ROLLBACK that reverts changes to an RTREE, any pending queries

against that same RTREE abort with code SQLITE_ABORT_ROLLBACK.
dbsqlfuzz de7d17b72d0e842352c998dd86a47b7d0f707be9.

FossilOrigin-Name: af5c425114f32c2f84aea20edd4fa46eb1bfdeb3747fce357540e15978a070c8
This commit is contained in:
drh
2024-02-03 19:19:18 +00:00
parent 50f9caf270
commit 7d6f33046f
4 changed files with 186 additions and 9 deletions

View File

@ -170,6 +170,7 @@ struct Rtree {
u32 nBusy; /* Current number of users of this structure */ u32 nBusy; /* Current number of users of this structure */
i64 nRowEst; /* Estimated number of rows in this table */ i64 nRowEst; /* Estimated number of rows in this table */
u32 nCursor; /* Number of open cursors */ u32 nCursor; /* Number of open cursors */
u32 iGeneration; /* Cursors with smaller iGeneration are stale */
u32 nNodeRef; /* Number RtreeNodes with positive nRef */ u32 nNodeRef; /* Number RtreeNodes with positive nRef */
char *zReadAuxSql; /* SQL for statement to read aux data */ char *zReadAuxSql; /* SQL for statement to read aux data */
@ -286,6 +287,7 @@ struct RtreeCursor {
u8 atEOF; /* True if at end of search */ u8 atEOF; /* True if at end of search */
u8 bPoint; /* True if sPoint is valid */ u8 bPoint; /* True if sPoint is valid */
u8 bAuxValid; /* True if pReadAux is valid */ u8 bAuxValid; /* True if pReadAux is valid */
u32 iGeneration; /* Stale if too small */
int iStrategy; /* Copy of idxNum search parameter */ int iStrategy; /* Copy of idxNum search parameter */
int nConstraint; /* Number of entries in aConstraint */ int nConstraint; /* Number of entries in aConstraint */
RtreeConstraint *aConstraint; /* Search constraints. */ RtreeConstraint *aConstraint; /* Search constraints. */
@ -1087,6 +1089,7 @@ static int rtreeOpen(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCursor){
if( pCsr ){ if( pCsr ){
memset(pCsr, 0, sizeof(RtreeCursor)); memset(pCsr, 0, sizeof(RtreeCursor));
pCsr->base.pVtab = pVTab; pCsr->base.pVtab = pVTab;
pCsr->iGeneration = pRtree->iGeneration;
rc = SQLITE_OK; rc = SQLITE_OK;
pRtree->nCursor++; pRtree->nCursor++;
} }
@ -1627,6 +1630,9 @@ static int rtreeStepToLeaf(RtreeCursor *pCur){
int eInt; int eInt;
RtreeSearchPoint x; RtreeSearchPoint x;
if( pCur->iGeneration<pRtree->iGeneration ){
return SQLITE_ABORT_ROLLBACK;
}
eInt = pRtree->eCoordType==RTREE_COORD_INT32; eInt = pRtree->eCoordType==RTREE_COORD_INT32;
while( (p = rtreeSearchPointFirst(pCur))!=0 && p->iLevel>0 ){ while( (p = rtreeSearchPointFirst(pCur))!=0 && p->iLevel>0 ){
u8 *pCellData; u8 *pCellData;
@ -1854,6 +1860,7 @@ static int rtreeFilter(
/* Reset the cursor to the same state as rtreeOpen() leaves it in. */ /* Reset the cursor to the same state as rtreeOpen() leaves it in. */
resetCursor(pCsr); resetCursor(pCsr);
pCsr->iGeneration = pRtree->iGeneration;
pCsr->iStrategy = idxNum; pCsr->iStrategy = idxNum;
if( idxNum==1 ){ if( idxNum==1 ){
@ -3234,6 +3241,16 @@ static int rtreeEndTransaction(sqlite3_vtab *pVtab){
nodeBlobReset(pRtree); nodeBlobReset(pRtree);
return SQLITE_OK; return SQLITE_OK;
} }
static int rtreeRollback(sqlite3_vtab *pVtab){
Rtree *pRtree = (Rtree *)pVtab;
pRtree->iGeneration++;
return rtreeEndTransaction(pVtab);
}
static int rtreeRollbackTo(sqlite3_vtab *pVtab, int notUsed){
Rtree *pRtree = (Rtree *)pVtab;
pRtree->iGeneration++;
return SQLITE_OK;
}
/* /*
** The xRename method for rtree module virtual tables. ** The xRename method for rtree module virtual tables.
@ -3352,12 +3369,12 @@ static sqlite3_module rtreeModule = {
rtreeBeginTransaction, /* xBegin - begin transaction */ rtreeBeginTransaction, /* xBegin - begin transaction */
rtreeEndTransaction, /* xSync - sync transaction */ rtreeEndTransaction, /* xSync - sync transaction */
rtreeEndTransaction, /* xCommit - commit transaction */ rtreeEndTransaction, /* xCommit - commit transaction */
rtreeEndTransaction, /* xRollback - rollback transaction */ rtreeRollback, /* xRollback - rollback transaction */
0, /* xFindFunction - function overloading */ 0, /* xFindFunction - function overloading */
rtreeRename, /* xRename - rename the table */ rtreeRename, /* xRename - rename the table */
rtreeSavepoint, /* xSavepoint */ rtreeSavepoint, /* xSavepoint */
0, /* xRelease */ 0, /* xRelease */
0, /* xRollbackTo */ rtreeRollbackTo, /* xRollbackTo */
rtreeShadowName, /* xShadowName */ rtreeShadowName, /* xShadowName */
rtreeIntegrity /* xIntegrity */ rtreeIntegrity /* xIntegrity */
}; };

159
ext/rtree/rtreeJ.test Normal file
View File

@ -0,0 +1,159 @@
# 2024-02-03
#
# The author disclaims copyright to this source code. In place of
# a legal notice, here is a blessing:
#
# May you do good and not evil.
# May you find forgiveness for yourself and forgive others.
# May you share freely, never taking more than you give.
#
#***********************************************************************
#
# ROLLBACK in the middle of an RTREE query
#
if {![info exists testdir]} {
set testdir [file join [file dirname [info script]] .. .. test]
}
source $testdir/tester.tcl
set testprefix rtreeJ
do_execsql_test 1.0 {
CREATE VIRTUAL TABLE t1 USING rtree(id, x1, x2);
INSERT INTO t1 VALUES(1, 1, 1), (2, 2, 2);
} {}
do_execsql_test 1.1 {
SELECT * FROM t1
} {1 1.0 1.0 2 2.0 2.0}
# If a ROLLBACK occurs that backs out changes to the RTREE, then
# all pending queries to the RTREE are aborted.
#
do_test 1.2 {
db eval {
BEGIN;
INSERT INTO t1 VALUES(3, 3, 3);
INSERT INTO t1 VALUES(4, 4, 4);
}
set rc [catch {
db eval { SELECT * FROM t1 } {
if {$id==1} {
db eval { ROLLBACK }
}
lappend res $id $x1 $x2
}
} msg]
list $rc $msg
} {1 {abort due to ROLLBACK}}
do_execsql_test 1.3 {
SELECT * FROM t1;
} {1 1.0 1.0 2 2.0 2.0}
# A COMMIT of changes to the RTREE does not affect pending queries
#
do_test 1.4 {
set res {}
db eval {
BEGIN;
INSERT INTO t1 VALUES(5, 5, 5);
INSERT INTO t1 VALUES(6, 6, 6);
}
db eval { SELECT * FROM t1 } {
if {$id==1} {
db eval { COMMIT }
}
lappend res $id $x1 $x2
}
set res
} {1 1.0 1.0 2 2.0 2.0 5 5.0 5.0 6 6.0 6.0}
do_execsql_test 1.5 {
SELECT * FROM t1;
} {1 1.0 1.0 2 2.0 2.0 5 5.0 5.0 6 6.0 6.0}
do_execsql_test 1.6 {
DELETE FROM t1;
INSERT INTO t1 VALUES(1,1,1),(2,2,2),(3,3,3),(4,4,4);
CREATE TABLE t2(x);
SELECT * FROM t1;
} {1 1.0 1.0 2 2.0 2.0 3 3.0 3.0 4 4.0 4.0}
# A rollback that does not affect the rtree table because
# the rtree table has not been written to does not cause
# a query abort.
#
do_test 1.7 {
set res {}
db eval {
BEGIN;
INSERT INTO t2(x) VALUES(12345);
}
db eval { SELECT * FROM t1 } {
if {$id==1} {
db eval { ROLLBACK }
}
lappend res $id $x1 $x2
}
set res
} {1 1.0 1.0 2 2.0 2.0 3 3.0 3.0 4 4.0 4.0}
# ROLLBACK TO that affects the RTREE does cause a query abort.
#
do_test 1.8 {
db eval {
DELETE FROM t1 WHERE rowid>1;
BEGIN;
DELETE FROM t2;
INSERT INTO t2(x) VALUES(23456);
SAVEPOINT 'one';
INSERT INTO t1 VALUES(2,2,2),(3,3,3);
}
set rc [catch {
db eval { SELECT * FROM t1 } {
if {$id==1} {
db eval { ROLLBACK TO 'one'; }
}
lappend res $id $x1 $x2
}
} msg]
list $rc $msg
} {1 {abort due to ROLLBACK}}
do_execsql_test 1.9 {
COMMIT;
SELECT * FROM t1;
} {1 1.0 1.0}
# ROLLBACK TO that does not affect the RTREE does not cause a query abort.
#
do_execsql_test 1.10 {
DELETE FROM t1;
INSERT INTO t1 VALUES(1,1,1),(2,2,2),(3,3,3);
BEGIN;
DELETE FROM t2;
INSERT INTO t2(x) VALUES(34567);
SAVEPOINT 'one';
INSERT INTO t2(x) VALUES('a string');
SELECT * FROM t1;
} {1 1.0 1.0 2 2.0 2.0 3 3.0 3.0}
do_test 1.11 {
set rc [catch {
set res {}
db eval { SELECT * FROM t1 } {
if {$id==2} {
# db eval { ROLLBACK TO 'one'; }
}
lappend res $id $x1 $x2
}
set res
} msg]
list $rc $msg
} {0 {1 1.0 1.0 2 2.0 2.0 3 3.0 3.0}}
do_execsql_test 1.12 {
COMMIT;
SELECT * FROM t1;
} {1 1.0 1.0 2 2.0 2.0 3 3.0 3.0}
finish_test

View File

@ -1,5 +1,5 @@
C More\sextensive\suse\sof\sSQLITE_CORRUPT_PGNO. C Following\sa\sROLLBACK\sthat\sreverts\schanges\sto\san\sRTREE,\sany\spending\squeries\nagainst\sthat\ssame\sRTREE\sabort\swith\scode\sSQLITE_ABORT_ROLLBACK.\ndbsqlfuzz\sde7d17b72d0e842352c998dd86a47b7d0f707be9.
D 2024-02-02T18:42:09.735 D 2024-02-03T19:19:18.306
F .fossil-settings/empty-dirs dbb81e8fc0401ac46a1491ab34a7f2c7c0452f2f06b54ebb845d024ca8283ef1 F .fossil-settings/empty-dirs dbb81e8fc0401ac46a1491ab34a7f2c7c0452f2f06b54ebb845d024ca8283ef1
F .fossil-settings/ignore-glob 35175cdfcf539b2318cb04a9901442804be81cd677d8b889fcc9149c21f239ea F .fossil-settings/ignore-glob 35175cdfcf539b2318cb04a9901442804be81cd677d8b889fcc9149c21f239ea
F LICENSE.md df5091916dbb40e6e9686186587125e1b2ff51f022cc334e886c19a0e9982724 F LICENSE.md df5091916dbb40e6e9686186587125e1b2ff51f022cc334e886c19a0e9982724
@ -495,7 +495,7 @@ F ext/repair/test/checkindex01.test b530f141413b587c9eb78ff734de6bb79bc3515c3350
F ext/repair/test/test.tcl 686d76d888dffd021f64260abf29a55c57b2cedfa7fc69150b42b1d6119aac3c F ext/repair/test/test.tcl 686d76d888dffd021f64260abf29a55c57b2cedfa7fc69150b42b1d6119aac3c
F ext/rtree/README 6315c0d73ebf0ec40dedb5aa0e942bc8b54e3761 F ext/rtree/README 6315c0d73ebf0ec40dedb5aa0e942bc8b54e3761
F ext/rtree/geopoly.c 0dd4775e896cee6067979d67aff7c998e75c2c9d9cd8d62a1a790c09cde7adca F ext/rtree/geopoly.c 0dd4775e896cee6067979d67aff7c998e75c2c9d9cd8d62a1a790c09cde7adca
F ext/rtree/rtree.c d0134bb75bc92b18a1dc011ec10419642f055c67af8ff44fc4a07c5fa9f189cb F ext/rtree/rtree.c 32e67b122e37138694d9f499cdc72241e372058560ac2d4577cfd68ccd692953
F ext/rtree/rtree.h 4a690463901cb5e6127cf05eb8e642f127012fd5003830dbc974eca5802d9412 F ext/rtree/rtree.h 4a690463901cb5e6127cf05eb8e642f127012fd5003830dbc974eca5802d9412
F ext/rtree/rtree1.test 2b5b8c719c6a4abe377f57766f428a49af36a93061cb146cccfdc3b30000c0a4 F ext/rtree/rtree1.test 2b5b8c719c6a4abe377f57766f428a49af36a93061cb146cccfdc3b30000c0a4
F ext/rtree/rtree2.test 9d9deddbb16fd0c30c36e6b4fdc3ee3132d765567f0f9432ee71e1303d32603d F ext/rtree/rtree2.test 9d9deddbb16fd0c30c36e6b4fdc3ee3132d765567f0f9432ee71e1303d32603d
@ -515,6 +515,7 @@ F ext/rtree/rtreeF.test 81ffa7ef51c4e4618d497a57328c265bf576990c7070633b623b23cd
F ext/rtree/rtreeG.test 1b9ca6e3effb48f4161edaa463ddeaa8fca4b2526d084f9cbf5dbe4e0184939c F ext/rtree/rtreeG.test 1b9ca6e3effb48f4161edaa463ddeaa8fca4b2526d084f9cbf5dbe4e0184939c
F ext/rtree/rtreeH.test 0885151ee8429242625600ae47142cca935332c70a06737f35af53a7bd7aaf90 F ext/rtree/rtreeH.test 0885151ee8429242625600ae47142cca935332c70a06737f35af53a7bd7aaf90
F ext/rtree/rtreeI.test 608e77f7fde9be5a12eae316baef640fffaafcfa90a3d67443e78123e19c4ca4 F ext/rtree/rtreeI.test 608e77f7fde9be5a12eae316baef640fffaafcfa90a3d67443e78123e19c4ca4
F ext/rtree/rtreeJ.test bafa7616d6b29448bf19132ce4963a69b629b9ca6ab29f4b76889c8d542b8dfc
F ext/rtree/rtree_perf.tcl 6c18c1f23cd48e0f948930c98dfdd37dfccb5195 F ext/rtree/rtree_perf.tcl 6c18c1f23cd48e0f948930c98dfdd37dfccb5195
F ext/rtree/rtree_util.tcl 202ca70df1f0645ef9d5a2170e62d378a28098d9407f0569e85c9c1cf1bd020a F ext/rtree/rtree_util.tcl 202ca70df1f0645ef9d5a2170e62d378a28098d9407f0569e85c9c1cf1bd020a
F ext/rtree/rtreecheck.test 934546ad9b563e090ee0c5cbdc69ad014189ad76e5df7320526797a9a345661f F ext/rtree/rtreecheck.test 934546ad9b563e090ee0c5cbdc69ad014189ad76e5df7320526797a9a345661f
@ -2161,8 +2162,8 @@ F vsixtest/vsixtest.tcl 6a9a6ab600c25a91a7acc6293828957a386a8a93
F vsixtest/vsixtest.vcxproj.data 2ed517e100c66dc455b492e1a33350c1b20fbcdc F vsixtest/vsixtest.vcxproj.data 2ed517e100c66dc455b492e1a33350c1b20fbcdc
F vsixtest/vsixtest.vcxproj.filters 37e51ffedcdb064aad6ff33b6148725226cd608e F vsixtest/vsixtest.vcxproj.filters 37e51ffedcdb064aad6ff33b6148725226cd608e
F vsixtest/vsixtest_TemporaryKey.pfx e5b1b036facdb453873e7084e1cae9102ccc67a0 F vsixtest/vsixtest_TemporaryKey.pfx e5b1b036facdb453873e7084e1cae9102ccc67a0
P 62010ba488f65aec3c6bd17b05f64c5b3e885dee4221e6016c4eb1053e19284d P 3838332cffb87f77a7c6b357066a59b9dc4f65ef4b3496767ad7ad7c14a77be2
R ce828c7f8969aa9dd36f079e0d3d1a31 R 11b673cea59b4f2c437d0cc603cd3823
U drh U drh
Z a60787a93d78c5cb2066cabdbdc63c36 Z 6e995078df88af9da02ef42e8af5819d
# Remove this line to create a well-formed Fossil manifest. # Remove this line to create a well-formed Fossil manifest.

View File

@ -1 +1 @@
3838332cffb87f77a7c6b357066a59b9dc4f65ef4b3496767ad7ad7c14a77be2 af5c425114f32c2f84aea20edd4fa46eb1bfdeb3747fce357540e15978a070c8