From 5c62486cd822dbc4410b31e011e73170d6646643 Mon Sep 17 00:00:00 2001 From: drh Date: Thu, 22 Sep 2011 18:46:34 +0000 Subject: [PATCH] Fix an issue in ANALYZE when STAT3 is disabled but both sqlite_stat2 and sqlite_stat3 tables exist. Also add testability tweaks to the STAT3 code. FossilOrigin-Name: 3ca7e449e2e20d95e516cf7fe87bfa0b51c07086 --- manifest | 16 ++++++++-------- manifest.uuid | 2 +- src/analyze.c | 22 ++++++++++++---------- src/sqliteInt.h | 2 +- src/where.c | 4 +++- 5 files changed, 25 insertions(+), 21 deletions(-) diff --git a/manifest b/manifest index 0e783c5618..d4e2d30b7b 100644 --- a/manifest +++ b/manifest @@ -1,5 +1,5 @@ -C Fix\san\suninitialized\svariable\sand\sa\smisuse\sof\smemcpy(). -D 2011-09-22T00:28:55.958 +C Fix\san\sissue\sin\sANALYZE\swhen\sSTAT3\sis\sdisabled\sbut\sboth\ssqlite_stat2\sand\nsqlite_stat3\stables\sexist.\s\sAlso\sadd\stestability\stweaks\sto\sthe\sSTAT3\scode. +D 2011-09-22T18:46:34.844 F Makefile.arm-wince-mingw32ce-gcc d6df77f1f48d690bd73162294bbba7f59507c72f F Makefile.in d314143fa6be24828021d3f583ad37d9afdce505 F Makefile.linux-gcc 91d710bdc4998cb015f39edf3cb314ec4f4d7e23 @@ -118,7 +118,7 @@ F sqlite.pc.in 42b7bf0d02e08b9e77734a47798d1a55a9e0716b F sqlite3.1 6be1ad09113570e1fc8dcaff84c9b0b337db5ffc F sqlite3.pc.in ae6f59a76e862f5c561eb32a380228a02afc3cad F src/alter.c ac80a0f31189f8b4a524ebf661e47e84536ee7f5 -F src/analyze.c 78a513d6229689c40252aa5d820565bcba90634c +F src/analyze.c 12c096a7e2383559c451c7111938a2682661795a F src/attach.c 12c6957996908edc31c96d7c68d4942c2474405f F src/auth.c 523da7fb4979469955d822ff9298352d6b31de34 F src/backup.c 28a4fe55327ff708bfaf9d4326d02686f7a553c3 @@ -183,7 +183,7 @@ F src/select.c d9b7d20b0365f80761846f00ef3638d4b33eeaf2 F src/shell.c 13fe2aeddc3cc90d6a273831d1f63736d1596f81 F src/sqlite.h.in 3f531daa04457e83bc13765c98c06c7d71c27fa5 F src/sqlite3ext.h 1a1a4f784aa9c3b00edd287940197de52487cd93 -F src/sqliteInt.h 6cb04e5216d68becf18fd4a4a6d47ec07f33219c +F src/sqliteInt.h 40bc71ab0840027d0b4e452b09467dc7274828e2 F src/sqliteLimit.h 164b0e6749d31e0daa1a4589a169d31c0dec7b3d F src/status.c 7ac64842c86cec2fc1a1d0e5c16d3beb8ad332bf F src/table.c 2cd62736f845d82200acfa1287e33feb3c15d62e @@ -251,7 +251,7 @@ F src/vtab.c 901791a47318c0562cd0c676a2c6ff1bc530e582 F src/wal.c 3154756177d6219e233d84291d5b05f4e06ff5e9 F src/wal.h 66b40bd91bc29a5be1c88ddd1f5ade8f3f48728a F src/walker.c 3112bb3afe1d85dc52317cb1d752055e9a781f8f -F src/where.c bd9ca1bb5ab909b04bdba6da2fffdfe07207c655 +F src/where.c 432a42cf4392bafe80ff06affb62cc24c8eea3fb F test/8_3_names.test 631ea964a3edb091cf73c3b540f6bcfdb36ce823 F test/aggerror.test a867e273ef9e3d7919f03ef4f0e8c0d2767944f2 F test/alias.test 4529fbc152f190268a15f9384a5651bbbabc9d87 @@ -965,7 +965,7 @@ F tool/symbols.sh caaf6ccc7300fd43353318b44524853e222557d5 F tool/tostr.awk e75472c2f98dd76e06b8c9c1367f4ab07e122d06 F tool/vdbe-compress.tcl d70ea6d8a19e3571d7ab8c9b75cba86d1173ff0f F tool/warnings.sh fbc018d67fd7395f440c28f33ef0f94420226381 -P 63fc3e4bea6a7f5dc34b2af83f30458eb10b8fe0 -R 21208cbb3e90ead0385f6f34c9d78ada +P ee110d5a4a6f29400bb632a9a18c7dcd04638657 +R ed357b44bde952eee8192d464b59f2eb U drh -Z 173e771ada64f444efa1679d0416f2ab +Z b3542caa8ec714ec974a1ed2d471f84c diff --git a/manifest.uuid b/manifest.uuid index bcbf1257fb..353f35070c 100644 --- a/manifest.uuid +++ b/manifest.uuid @@ -1 +1 @@ -ee110d5a4a6f29400bb632a9a18c7dcd04638657 \ No newline at end of file +3ca7e449e2e20d95e516cf7fe87bfa0b51c07086 \ No newline at end of file diff --git a/src/analyze.c b/src/analyze.c index 251560f14e..b1af3bf70a 100644 --- a/src/analyze.c +++ b/src/analyze.c @@ -172,7 +172,10 @@ static void openStatTable( */ for(i=0; izName); - if( pTab ) sqlite3CodeDropTable(pParse, pTab, iDb, 0); + if( pTab ){ + sqlite3CodeDropTable(pParse, pTab, iDb, 0); + break; + } } /* Create new statistic tables if they do not exist, or clear them @@ -338,9 +341,8 @@ static void stat3Push( } if( !doInsert ) return; if( p->nSample==p->mxSample ){ - if( iMinnSample ){ - memmove(&p->a[iMin], &p->a[iMin+1], sizeof(p->a[0])*(p->nSample-iMin-1)); - } + assert( p->nSample - iMin - 1 >= 0 ); + memmove(&p->a[iMin], &p->a[iMin+1], sizeof(p->a[0])*(p->nSample-iMin-1)); pSample = &p->a[p->nSample-1]; }else{ pSample = &p->a[p->nSample++]; @@ -413,10 +415,10 @@ static void stat3Get( assert( p!=0 ); if( p->nSample<=n ) return; switch( argc ){ - case 2: sqlite3_result_int64(context, p->a[n].iRowid); break; - case 3: sqlite3_result_int64(context, p->a[n].nEq); break; - case 4: sqlite3_result_int64(context, p->a[n].nLt); break; - case 5: sqlite3_result_int64(context, p->a[n].nDLt); break; + case 2: sqlite3_result_int64(context, p->a[n].iRowid); break; + case 3: sqlite3_result_int64(context, p->a[n].nEq); break; + case 4: sqlite3_result_int64(context, p->a[n].nLt); break; + default: sqlite3_result_int64(context, p->a[n].nDLt); break; } } static const FuncDef stat3GetFuncdef = { @@ -972,6 +974,7 @@ static int loadStat3(sqlite3 *db, const char *zDb){ pIdx = sqlite3FindIndex(db, zIndex, zDb); if( pIdx==0 ) continue; assert( pIdx->nSample==0 ); + testcase( nSample==255 ); pIdx->nSample = (u8)nSample; pIdx->aSample = sqlite3MallocZero( nSample*sizeof(IndexSample) ); pIdx->avgEq = pIdx->aiRowEst[1]; @@ -1042,8 +1045,7 @@ static int loadStat3(sqlite3 *db, const char *zDb){ sqlite3_column_text(pStmt, 4) ); int n = z ? sqlite3_column_bytes(pStmt, 4) : 0; - if( n>0xffff ) n = 0xffff; - pSample->nByte = (u16)n; + pSample->nByte = n; if( n < 1){ pSample->u.z = 0; }else{ diff --git a/src/sqliteInt.h b/src/sqliteInt.h index 75bafe81a6..6f92a1a41e 100644 --- a/src/sqliteInt.h +++ b/src/sqliteInt.h @@ -1524,7 +1524,7 @@ struct IndexSample { i64 i; /* Value if eType is SQLITE_INTEGER */ } u; u8 eType; /* SQLITE_NULL, SQLITE_INTEGER ... etc. */ - u16 nByte; /* Size in byte of text or blob. */ + int nByte; /* Size in byte of text or blob. */ tRowcnt nEq; /* Est. number of rows where the key equals this sample */ tRowcnt nLt; /* Est. number of rows where key is less than this sample */ tRowcnt nDLt; /* Est. number of distinct keys less than this sample */ diff --git a/src/where.c b/src/where.c index db076ebaf0..bf6b52752f 100644 --- a/src/where.c +++ b/src/where.c @@ -2452,6 +2452,7 @@ static int whereKeyStats( double r, rS; assert( roundUp==0 || roundUp==1 ); + assert( pIdx->nSample>0 ); if( pVal==0 ) return SQLITE_ERROR; n = pIdx->aiRowEst[0]; aSample = pIdx->aSample; @@ -2494,7 +2495,7 @@ static int whereKeyStats( } }else if( eType==SQLITE_NULL ){ i = 0; - if( pIdx->nSample>=1 && aSample[0].eType==SQLITE_NULL ) isEq = 1; + if( aSample[0].eType==SQLITE_NULL ) isEq = 1; }else{ assert( eType==SQLITE_TEXT || eType==SQLITE_BLOB ); for(i=0; inSample; i++){ @@ -2760,6 +2761,7 @@ static int whereEqualScanEst( tRowcnt a[2]; /* Statistics */ assert( p->aSample!=0 ); + assert( p->nSample>0 ); aff = p->pTable->aCol[p->aiColumn[0]].affinity; if( pExpr ){ rc = valueFromExpr(pParse, pExpr, aff, &pRhs);