Many hyperlinks are disabled.
Use anonymous login
to enable hyperlinks.
Overview
Comment: | Fix a crash that can occur following an OOM fault. |
---|---|
Downloads: | Tarball | ZIP archive |
Timelines: | family | ancestors | descendants | both | sqlite_stat4 |
Files: | files | file ages | folders |
SHA1: |
9f80b2687012ab7c4d6d654fe19f4087 |
User & Date: | dan 2013-08-15 18:43:21.395 |
Context
2013-08-15
| ||
19:56 | Fix a crash that can occur if the sqlite_stat3 or sqlite_stat4 table is corrupt. (check-in: d51df8a8fc user: dan tags: sqlite_stat4) | |
18:43 | Fix a crash that can occur following an OOM fault. (check-in: 9f80b26870 user: dan tags: sqlite_stat4) | |
16:18 | Change some assert() statements in vdbe.c to ensure that a memory cell used to store a VdbeCursor object is not also used for some other purpose. (check-in: 71070c9fce user: dan tags: sqlite_stat4) | |
Changes
Changes to src/vdbemem.c.
︙ | ︙ | |||
1002 1003 1004 1005 1006 1007 1008 | p->type = SQLITE_NULL; p->db = db; } return p; } /* | > > > > > > > > > | > | > > > > > | | | > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > | > | > > | < > > > | | | | | < | | 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 | p->type = SQLITE_NULL; p->db = db; } return p; } /* ** Context object passed by sqlite3Stat4ProbeSetValue() through to ** valueNew(). See comments above valueNew() for details. */ struct ValueNewStat4Ctx { Parse *pParse; Index *pIdx; UnpackedRecord **ppRec; int iVal; }; /* ** Allocate and return a pointer to a new sqlite3_value object. If ** the second argument to this function is NULL, the object is allocated ** by calling sqlite3ValueNew(). ** ** Otherwise, if the second argument is non-zero, then this function is ** being called indirectly by sqlite3Stat4ProbeSetValue(). If it has not ** already been allocated, allocate the UnpackedRecord structure that ** that function will return to its caller here. Then return a pointer ** an sqlite3_value within the UnpackedRecord.a[] array. */ static sqlite3_value *valueNew(sqlite3 *db, struct ValueNewStat4Ctx *p){ #if defined(SQLITE_ENABLE_STAT4) || defined(SQLITE_ENABLE_STAT3) if( p ){ UnpackedRecord *pRec = p->ppRec[0]; if( pRec==0 ){ Index *pIdx = p->pIdx; /* Index being probed */ int nByte; /* Bytes of space to allocate */ int i; /* Counter variable */ int nCol = pIdx->nColumn+1; /* Number of index columns including rowid */ nByte = sizeof(Mem) * nCol + sizeof(UnpackedRecord); pRec = (UnpackedRecord*)sqlite3DbMallocZero(db, nByte); if( pRec ){ pRec->pKeyInfo = sqlite3IndexKeyinfo(p->pParse, pIdx); if( pRec->pKeyInfo ){ assert( pRec->pKeyInfo->nField+1==nCol ); pRec->pKeyInfo->enc = ENC(db); pRec->flags = UNPACKED_PREFIX_MATCH; pRec->aMem = (Mem *)&pRec[1]; for(i=0; i<nCol; i++){ pRec->aMem[i].flags = MEM_Null; pRec->aMem[i].type = SQLITE_NULL; pRec->aMem[i].db = db; } }else{ sqlite3DbFree(db, pRec); pRec = 0; } } if( pRec==0 ) return 0; p->ppRec[0] = pRec; } pRec->nField = p->iVal+1; return &pRec->aMem[p->iVal]; } #endif return sqlite3ValueNew(db); } /* ** Extract a value from the supplied expression in the manner described ** above sqlite3ValueFromExpr(). Allocate the sqlite3_value object ** using valueNew(). ** ** If pCtx is NULL and an error occurs after the sqlite3_value object ** has been allocated, it is freed before returning. Or, if pCtx is not ** NULL, it is assumed that the caller will free any allocated object ** in all cases. */ int valueFromExpr( sqlite3 *db, /* The database connection */ Expr *pExpr, /* The expression to evaluate */ u8 enc, /* Encoding to use */ u8 affinity, /* Affinity to use */ sqlite3_value **ppVal, /* Write the new value here */ struct ValueNewStat4Ctx *pCtx /* Second argument for valueNew() */ ){ int op; char *zVal = 0; sqlite3_value *pVal = 0; int negInt = 1; const char *zNeg = ""; |
︙ | ︙ | |||
1060 1061 1062 1063 1064 1065 1066 | pExpr = pExpr->pLeft; op = pExpr->op; negInt = -1; zNeg = "-"; } if( op==TK_STRING || op==TK_FLOAT || op==TK_INTEGER ){ | | | 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 | pExpr = pExpr->pLeft; op = pExpr->op; negInt = -1; zNeg = "-"; } if( op==TK_STRING || op==TK_FLOAT || op==TK_INTEGER ){ pVal = valueNew(db, pCtx); if( pVal==0 ) goto no_mem; if( ExprHasProperty(pExpr, EP_IntValue) ){ sqlite3VdbeMemSetInt64(pVal, (i64)pExpr->u.iValue*negInt); }else{ zVal = sqlite3MPrintf(db, "%s%s", zNeg, pExpr->u.zToken); if( zVal==0 ) goto no_mem; sqlite3ValueSetStr(pVal, -1, zVal, SQLITE_UTF8, SQLITE_DYNAMIC); |
︙ | ︙ | |||
1096 1097 1098 1099 1100 1101 1102 | }else{ pVal->u.i = -pVal->u.i; } pVal->r = -pVal->r; sqlite3ValueApplyAffinity(pVal, affinity, enc); } }else if( op==TK_NULL ){ | | | > | < | | 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 | }else{ pVal->u.i = -pVal->u.i; } pVal->r = -pVal->r; sqlite3ValueApplyAffinity(pVal, affinity, enc); } }else if( op==TK_NULL ){ pVal = valueNew(db, pCtx); if( pVal==0 ) goto no_mem; } #ifndef SQLITE_OMIT_BLOB_LITERAL else if( op==TK_BLOB ){ int nVal; assert( pExpr->u.zToken[0]=='x' || pExpr->u.zToken[0]=='X' ); assert( pExpr->u.zToken[1]=='\'' ); pVal = valueNew(db, pCtx); if( !pVal ) goto no_mem; zVal = &pExpr->u.zToken[2]; nVal = sqlite3Strlen30(zVal)-1; assert( zVal[nVal]=='\'' ); sqlite3VdbeMemSetStr(pVal, sqlite3HexToBlob(db, zVal, nVal), nVal/2, 0, SQLITE_DYNAMIC); } #endif if( pVal ){ sqlite3VdbeMemStoreType(pVal); } *ppVal = pVal; return SQLITE_OK; no_mem: db->mallocFailed = 1; sqlite3DbFree(db, zVal); assert( *ppVal==0 ); if( pCtx==0 ) sqlite3ValueFree(pVal); return SQLITE_NOMEM; } /* ** Create a new sqlite3_value object, containing the value of pExpr. ** ** This only works for very simple expressions that consist of one constant ** token (i.e. "5", "5.1", "'a string'"). If the expression can ** be converted directly into a value, then the value is allocated and ** a pointer written to *ppVal. The caller is responsible for deallocating ** the value by passing it to sqlite3ValueFree() later on. If the expression ** cannot be converted to a value, then *ppVal is set to NULL. */ int sqlite3ValueFromExpr( sqlite3 *db, /* The database connection */ Expr *pExpr, /* The expression to evaluate */ u8 enc, /* Encoding to use */ u8 affinity, /* Affinity to use */ sqlite3_value **ppVal /* Write the new value here */ ){ return valueFromExpr(db, pExpr, enc, affinity, ppVal, 0); } #if defined(SQLITE_ENABLE_STAT4) || defined(SQLITE_ENABLE_STAT3) /* ** The implementation of the sqlite_record() function. This function accepts ** a single argument of any type. The return value is a formatted database ** record (a blob) containing the argument value. |
︙ | ︙ | |||
1203 1204 1205 1206 1207 1208 1209 | FuncDefHash *pHash = &GLOBAL(FuncDefHash, sqlite3GlobalFunctions); FuncDef *aFunc = (FuncDef*)&GLOBAL(FuncDef, aAnalyzeTableFuncs); for(i=0; i<ArraySize(aAnalyzeTableFuncs); i++){ sqlite3FuncDefInsert(pHash, &aFunc[i]); } } | < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < | 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 | FuncDefHash *pHash = &GLOBAL(FuncDefHash, sqlite3GlobalFunctions); FuncDef *aFunc = (FuncDef*)&GLOBAL(FuncDef, aAnalyzeTableFuncs); for(i=0; i<ArraySize(aAnalyzeTableFuncs); i++){ sqlite3FuncDefInsert(pHash, &aFunc[i]); } } /* ** This function is used to allocate and populate UnpackedRecord ** structures intended to be compared against sample index keys stored ** in the sqlite_stat4 table. ** ** A single call to this function attempts to populates field iVal (leftmost ** is 0 etc.) of the unpacked record with a value extracted from expression |
︙ | ︙ | |||
1309 1310 1311 1312 1313 1314 1315 | struct ValueNewStat4Ctx alloc; alloc.pParse = pParse; alloc.pIdx = pIdx; alloc.ppRec = ppRec; alloc.iVal = iVal; | < < < < | | | < < | 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 | struct ValueNewStat4Ctx alloc; alloc.pParse = pParse; alloc.pIdx = pIdx; alloc.ppRec = ppRec; alloc.iVal = iVal; if( !pExpr ){ pVal = valueNew(pParse->db, &alloc); if( pVal ){ sqlite3VdbeMemSetNull((Mem*)pVal); *pbOk = 1; } }else if( pExpr->op==TK_VARIABLE || (pExpr->op==TK_REGISTER && pExpr->op2==TK_VARIABLE) ){ Vdbe *v; int iVar = pExpr->iColumn; sqlite3VdbeSetVarmask(pParse->pVdbe, iVar); if( (v = pParse->pReprepare) ){ pVal = valueNew(pParse->db, &alloc); if( pVal ){ rc = sqlite3VdbeMemCopy((Mem*)pVal, &v->aVar[iVal-1]); if( rc==SQLITE_OK ){ sqlite3ValueApplyAffinity(pVal, affinity, SQLITE_UTF8); } pVal->db = pParse->db; *pbOk = 1; sqlite3VdbeMemStoreType((Mem*)pVal); } }else{ *pbOk = 0; } }else{ sqlite3 *db = pParse->db; rc = valueFromExpr(db, pExpr, ENC(db), affinity, &pVal, &alloc); *pbOk = (pVal!=0); } assert( pVal==0 || pVal->db==pParse->db ); return rc; } |
︙ | ︙ |
Changes to test/analyze9.test.
︙ | ︙ | |||
251 252 253 254 255 256 257 258 259 260 261 262 263 264 | # Check that the perioidic samples are present. do_execsql_test 4.6 { SELECT count(*) FROM sqlite_stat4 WHERE lindex(test_decode(sample), 3) IN ('34', '68', '102', '136', '170', '204', '238', '272') } {8} #------------------------------------------------------------------------- # The following would cause a crash at one point. # reset_db do_execsql_test 5.1 { PRAGMA encoding = 'utf-16'; | > > > > > > > > > > > > > > > > > > > > > > > > | 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 | # Check that the perioidic samples are present. do_execsql_test 4.6 { SELECT count(*) FROM sqlite_stat4 WHERE lindex(test_decode(sample), 3) IN ('34', '68', '102', '136', '170', '204', '238', '272') } {8} reset_db do_test 4.7 { execsql { BEGIN; CREATE TABLE t1(o,t INTEGER PRIMARY KEY); CREATE INDEX i1 ON t1(o); } for {set i 0} {$i<10000} {incr i [expr (($i<1000)?1:10)]} { execsql { INSERT INTO t1 VALUES('x', $i) } } execsql { COMMIT; ANALYZE; SELECT count(*) FROM sqlite_stat4; } } {8} do_execsql_test 4.8 { SELECT test_decode(sample) FROM sqlite_stat4; } { {x 211} {x 423} {x 635} {x 847} {x 1590} {x 3710} {x 5830} {x 7950} } #------------------------------------------------------------------------- # The following would cause a crash at one point. # reset_db do_execsql_test 5.1 { PRAGMA encoding = 'utf-16'; |
︙ | ︙ |
Changes to test/mallocA.test.
︙ | ︙ | |||
11 12 13 14 15 16 17 18 19 20 21 22 23 24 | # This file contains additional out-of-memory checks (see malloc.tcl). # # $Id: mallocA.test,v 1.8 2008/02/18 22:24:58 drh Exp $ set testdir [file dirname $argv0] source $testdir/tester.tcl source $testdir/malloc_common.tcl # Only run these tests if memory debugging is turned on. # if {!$MEMDEBUG} { puts "Skipping mallocA tests: not compiled with -DSQLITE_MEMDEBUG..." finish_test return | > | 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | # This file contains additional out-of-memory checks (see malloc.tcl). # # $Id: mallocA.test,v 1.8 2008/02/18 22:24:58 drh Exp $ set testdir [file dirname $argv0] source $testdir/tester.tcl source $testdir/malloc_common.tcl set testprefix mallocA # Only run these tests if memory debugging is turned on. # if {!$MEMDEBUG} { puts "Skipping mallocA tests: not compiled with -DSQLITE_MEMDEBUG..." finish_test return |
︙ | ︙ | |||
36 37 38 39 40 41 42 | CREATE INDEX t1i1 ON t1(a); CREATE INDEX t1i2 ON t1(b,c); CREATE TABLE t2(x,y,z); } db close copy_file test.db test.db.bu | < > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > | 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 | CREATE INDEX t1i1 ON t1(a); CREATE INDEX t1i2 ON t1(b,c); CREATE TABLE t2(x,y,z); } db close copy_file test.db test.db.bu do_malloc_test mallocA-1 -testdb test.db.bu -sqlbody { ANALYZE } do_malloc_test mallocA-1.1 -testdb test.db.bu -sqlbody { ANALYZE t1 } do_malloc_test mallocA-1.2 -testdb test.db.bu -sqlbody { ANALYZE main } do_malloc_test mallocA-1.3 -testdb test.db.bu -sqlbody { ANALYZE main.t1 } ifcapable reindex { do_malloc_test mallocA-2 -testdb test.db.bu -sqlbody { REINDEX; } do_malloc_test mallocA-3 -testdb test.db.bu -sqlbody { REINDEX t1; } do_malloc_test mallocA-4 -testdb test.db.bu -sqlbody { REINDEX main.t1; } do_malloc_test mallocA-5 -testdb test.db.bu -sqlbody { REINDEX nocase; } } reset_db sqlite3_db_config_lookaside db 0 0 0 do_execsql_test 6-prep { CREATE TABLE t1(a, b); CREATE INDEX i1 ON t1(a, b); INSERT INTO t1 VALUES('abc', 'w'); -- rowid=1 INSERT INTO t1 VALUES('abc', 'x'); -- rowid=2 INSERT INTO t1 VALUES('abc', 'y'); -- rowid=3 INSERT INTO t1 VALUES('abc', 'z'); -- rowid=4 INSERT INTO t1 VALUES('def', 'w'); -- rowid=5 INSERT INTO t1 VALUES('def', 'x'); -- rowid=6 INSERT INTO t1 VALUES('def', 'y'); -- rowid=7 INSERT INTO t1 VALUES('def', 'z'); -- rowid=8 ANALYZE; } do_faultsim_test 6.1 -faults oom* -body { execsql { SELECT rowid FROM t1 WHERE a='abc' AND b='x' } } -test { faultsim_test_result [list 0 2] } do_faultsim_test 6.2 -faults oom* -body { execsql { SELECT rowid FROM t1 WHERE a='abc' AND b<'y' } } -test { faultsim_test_result [list 0 {1 2}] } # Ensure that no file descriptors were leaked. do_test malloc-99.X { catch {db close} set sqlite_open_file_count } {0} forcedelete test.db.bu finish_test |