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

Improvements to query invariants in fuzzcheck.

FossilOrigin-Name: 3a461f61b47e6ba6d5dcc2b7470ebde512b57bc68086f65050e07b06f42b7351
This commit is contained in:
drh
2022-06-15 20:18:44 +00:00
parent e3bf2c8e9b
commit 8f9261a8e9
4 changed files with 26 additions and 11 deletions

View File

@ -949,12 +949,15 @@ static int runDbSql(sqlite3 *db, const char *zSql, unsigned int *pBtsFlags){
int nRow = 0;
while( (rc = sqlite3_step(pStmt))==SQLITE_ROW ){
nRow++;
if( (*pBtsFlags)==BTS_SELECT && g.doInvariantChecks ){
if( (*pBtsFlags)==BTS_SELECT
&& g.doInvariantChecks
&& !sqlite3_stmt_isexplain(pStmt)
){
int iCnt = 0;
for(iCnt=0; iCnt<99999; iCnt++){
rc = fuzz_invariant(db, pStmt, iCnt, nRow, &bCorrupt, eVerbosity);
if( rc==SQLITE_DONE ) break;
g.nInvariant++;
if( rc!=SQLITE_ERROR ) g.nInvariant++;
if( eVerbosity>0 ){
if( rc==SQLITE_OK ){
printf("invariant-check: ok\n");

View File

@ -154,7 +154,15 @@ static char *fuzz_invariant_sql(sqlite3_stmt *pStmt, int iCnt){
int rc;
int nCol = sqlite3_column_count(pStmt);
int mxCnt;
int bDistinct = 0;
int bOrderBy = 0;
switch( iCnt % 4 ){
case 1: bDistinct = 1; break;
case 2: bOrderBy = 1; break;
case 3: bDistinct = bOrderBy = 1; break;
}
iCnt /= 4;
if( nCol==1 ){
mxCnt = 0;
}else{
@ -167,7 +175,8 @@ static char *fuzz_invariant_sql(sqlite3_stmt *pStmt, int iCnt){
while( nIn>0 && (isspace(zIn[nIn-1]) || zIn[nIn-1]==';') ) nIn--;
if( strchr(zIn, '?') ) return 0;
pTest = sqlite3_str_new(0);
sqlite3_str_appendf(pTest, "SELECT * FROM (%.*s)", (int)nIn, zIn);
sqlite3_str_appendf(pTest, "SELECT %s* FROM (%.*s)",
bDistinct ? "DISTINCT " : "", (int)nIn, zIn);
rc = sqlite3_prepare_v2(db, sqlite3_str_value(pTest), -1, &pBase, 0);
if( rc ){
sqlite3_finalize(pBase);
@ -193,6 +202,9 @@ static char *fuzz_invariant_sql(sqlite3_stmt *pStmt, int iCnt){
zAnd = "AND";
}
if( pBase!=pStmt ) sqlite3_finalize(pBase);
if( bOrderBy ){
sqlite3_str_appendf(pTest, " ORDER BY 1");
}
return sqlite3_str_finish(pTest);
}