| 1 |
/* |
| 2 |
** 2022-06-14 |
| 3 |
** |
| 4 |
** The author disclaims copyright to this source code. In place of |
| 5 |
** a legal notice, here is a blessing: |
| 6 |
** |
| 7 |
** May you do good and not evil. |
| 8 |
** May you find forgiveness for yourself and forgive others. |
| 9 |
** May you share freely, never taking more than you give. |
| 10 |
** |
| 11 |
************************************************************************* |
| 12 |
** |
| 13 |
** This library is used by fuzzcheck to test query invariants. |
| 14 |
** |
| 15 |
** An sqlite3_stmt is passed in that has just returned SQLITE_ROW. This |
| 16 |
** routine does: |
| 17 |
** |
| 18 |
** * Record the output of the current row |
| 19 |
** * Construct an alternative query that should return the same row |
| 20 |
** * Run the alternative query and verify that it does in fact return |
| 21 |
** the same row |
| 22 |
** |
| 23 |
*/ |
| 24 |
#include "sqlite3.h" |
| 25 |
#include <stdio.h> |
| 26 |
#include <stdlib.h> |
| 27 |
#include <string.h> |
| 28 |
#include <ctype.h> |
| 29 |
|
| 30 |
/* Forward references */ |
| 31 |
static char *fuzz_invariant_sql(sqlite3_stmt*, int); |
| 32 |
static int sameValue(sqlite3_stmt*,int,sqlite3_stmt*,int,sqlite3_stmt*); |
| 33 |
static void reportInvariantFailed( |
| 34 |
sqlite3_stmt *pOrig, /* The original query */ |
| 35 |
sqlite3_stmt *pTest, /* The alternative test query with a missing row */ |
| 36 |
int iRow, /* Row number in pOrig */ |
| 37 |
unsigned int dbOpt, /* Optimization flags on pOrig */ |
| 38 |
int noOpt /* True if opt flags inverted for pTest */ |
| 39 |
); |
| 40 |
|
| 41 |
/* |
| 42 |
** Special parameter binding, for testing and debugging purposes. |
| 43 |
** |
| 44 |
** $int_NNN -> integer value NNN |
| 45 |
** $text_TTTT -> floating point value TTT with destructor |
| 46 |
** $carray_clr -> First argument to carray() for color names |
| 47 |
** $carray_primes -> First argument to carray() for prime numbers |
| 48 |
*/ |
| 49 |
static void bindDebugParameters(sqlite3_stmt *pStmt){ |
| 50 |
int nVar = sqlite3_bind_parameter_count(pStmt); |
| 51 |
int i; |
| 52 |
for(i=1; i<=nVar; i++){ |
| 53 |
const char *zVar = sqlite3_bind_parameter_name(pStmt, i); |
| 54 |
if( zVar==0 ) continue; |
| 55 |
#ifdef SQLITE_ENABLE_CARRAY |
| 56 |
if( strcmp(zVar,"$carray_clr")==0 ){ |
| 57 |
static char *azColorNames[] = { |
| 58 |
"azure", "black", "blue", "brown", "cyan", "fuchsia", "gold", |
| 59 |
"gray", "green", "indigo", "khaki", "lime", "magenta", "maroon", |
| 60 |
"navy", "olive", "orange", "pink", "purple", "red", "silver", |
| 61 |
"tan", "teal", "violet", "white", "yellow" |
| 62 |
}; |
| 63 |
sqlite3_carray_bind(pStmt,i,azColorNames,26,SQLITE_CARRAY_TEXT,0); |
| 64 |
}else |
| 65 |
if( strcmp(zVar,"$carray_primes")==0 ){ |
| 66 |
static int aPrimes[] = { |
| 67 |
1, 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, |
| 68 |
53, 59, 61, 67, 71, 73, 79, 83, 89, 97 |
| 69 |
}; |
| 70 |
sqlite3_carray_bind(pStmt,i,aPrimes,26,SQLITE_CARRAY_INT32,0); |
| 71 |
}else |
| 72 |
#endif |
| 73 |
if( strncmp(zVar, "$int_", 5)==0 ){ |
| 74 |
sqlite3_bind_int(pStmt, i, atoi(&zVar[5])); |
| 75 |
}else |
| 76 |
if( strncmp(zVar, "$text_", 6)==0 ){ |
| 77 |
size_t szVar = strlen(zVar); |
| 78 |
char *zBuf = sqlite3_malloc64( szVar-5 ); |
| 79 |
if( zBuf ){ |
| 80 |
memcpy(zBuf, &zVar[6], szVar-5); |
| 81 |
sqlite3_bind_text64(pStmt, i, zBuf, szVar-6, sqlite3_free, SQLITE_UTF8); |
| 82 |
} |
| 83 |
} |
| 84 |
} |
| 85 |
} |
| 86 |
|
| 87 |
/* |
| 88 |
** Do an invariant check on pStmt. iCnt determines which invariant check to |
| 89 |
** perform. The first check is iCnt==0. |
| 90 |
** |
| 91 |
** *pbCorrupt is a flag that, if true, indicates that the database file |
| 92 |
** is known to be corrupt. A value of non-zero means "yes, the database |
| 93 |
** is corrupt". A zero value means "we do not know whether or not the |
| 94 |
** database is corrupt". The value might be set prior to entry, or this |
| 95 |
** routine might set the value. |
| 96 |
** |
| 97 |
** Return values: |
| 98 |
** |
| 99 |
** SQLITE_OK This check was successful. |
| 100 |
** |
| 101 |
** SQLITE_DONE iCnt is out of range. The caller typically sets |
| 102 |
** up a loop on iCnt starting with zero, and increments |
| 103 |
** iCnt until this code is returned. |
| 104 |
** |
| 105 |
** SQLITE_CORRUPT The invariant failed, but the underlying database |
| 106 |
** file is indicating that it is corrupt, which might |
| 107 |
** be the cause of the malfunction. The *pCorrupt |
| 108 |
** value will also be set. |
| 109 |
** |
| 110 |
** SQLITE_INTERNAL The invariant failed, and the database file is not |
| 111 |
** corrupt. (This never happens because this function |
| 112 |
** will call abort() following an invariant failure.) |
| 113 |
** |
| 114 |
** (other) Some other kind of error occurred. |
| 115 |
*/ |
| 116 |
int fuzz_invariant( |
| 117 |
sqlite3 *db, /* The database connection */ |
| 118 |
sqlite3_stmt *pStmt, /* Test statement stopped on an SQLITE_ROW */ |
| 119 |
int iCnt, /* Invariant sequence number, starting at 0 */ |
| 120 |
int iRow, /* Current row number */ |
| 121 |
int nRow, /* Number of output rows from pStmt */ |
| 122 |
int *pbCorrupt, /* IN/OUT: Flag indicating a corrupt database file */ |
| 123 |
int eVerbosity, /* How much debugging output */ |
| 124 |
unsigned int dbOpt /* Default optimization flags */ |
| 125 |
){ |
| 126 |
char *zTest; |
| 127 |
sqlite3_stmt *pTestStmt = 0; |
| 128 |
int rc; |
| 129 |
int i; |
| 130 |
int nCol; |
| 131 |
int nParam; |
| 132 |
int noOpt = (iCnt%3)==0; |
| 133 |
|
| 134 |
if( *pbCorrupt ) return SQLITE_DONE; |
| 135 |
nParam = sqlite3_bind_parameter_count(pStmt); |
| 136 |
if( nParam>100 ) return SQLITE_DONE; |
| 137 |
zTest = fuzz_invariant_sql(pStmt, iCnt); |
| 138 |
if( zTest==0 ) return SQLITE_DONE; |
| 139 |
if( noOpt ){ |
| 140 |
sqlite3_test_control(SQLITE_TESTCTRL_OPTIMIZATIONS, db, ~dbOpt); |
| 141 |
} |
| 142 |
rc = sqlite3_prepare_v2(db, zTest, -1, &pTestStmt, 0); |
| 143 |
if( noOpt ){ |
| 144 |
sqlite3_test_control(SQLITE_TESTCTRL_OPTIMIZATIONS, db, dbOpt); |
| 145 |
} |
| 146 |
if( rc ){ |
| 147 |
if( eVerbosity ){ |
| 148 |
printf("invariant compile failed: %s\n%s\n", |
| 149 |
sqlite3_errmsg(db), zTest); |
| 150 |
} |
| 151 |
sqlite3_free(zTest); |
| 152 |
sqlite3_finalize(pTestStmt); |
| 153 |
return rc; |
| 154 |
} |
| 155 |
sqlite3_free(zTest); |
| 156 |
bindDebugParameters(pTestStmt); |
| 157 |
nCol = sqlite3_column_count(pStmt); |
| 158 |
for(i=0; i<nCol; i++){ |
| 159 |
rc = sqlite3_bind_value(pTestStmt,i+1+nParam,sqlite3_column_value(pStmt,i)); |
| 160 |
if( rc!=SQLITE_OK && rc!=SQLITE_RANGE ){ |
| 161 |
sqlite3_finalize(pTestStmt); |
| 162 |
return rc; |
| 163 |
} |
| 164 |
} |
| 165 |
if( eVerbosity>=2 ){ |
| 166 |
char *zSql = sqlite3_expanded_sql(pTestStmt); |
| 167 |
printf("invariant-sql row=%d #%d:\n%s\n", iRow, iCnt, zSql); |
| 168 |
sqlite3_free(zSql); |
| 169 |
} |
| 170 |
while( (rc = sqlite3_step(pTestStmt))==SQLITE_ROW ){ |
| 171 |
for(i=0; i<nCol; i++){ |
| 172 |
if( !sameValue(pStmt, i, pTestStmt, i, 0) ) break; |
| 173 |
} |
| 174 |
if( i>=nCol ) break; |
| 175 |
} |
| 176 |
if( rc==SQLITE_DONE ){ |
| 177 |
/* No matching output row found */ |
| 178 |
sqlite3_stmt *pCk = 0; |
| 179 |
int iOrigRSO; |
| 180 |
|
| 181 |
|
| 182 |
/* This is not a fault if the database file is corrupt, because anything |
| 183 |
** can happen with a corrupt database file */ |
| 184 |
rc = sqlite3_prepare_v2(db, "PRAGMA integrity_check", -1, &pCk, 0); |
| 185 |
if( rc ){ |
| 186 |
sqlite3_finalize(pCk); |
| 187 |
sqlite3_finalize(pTestStmt); |
| 188 |
return rc; |
| 189 |
} |
| 190 |
if( eVerbosity>=2 ){ |
| 191 |
char *zSql = sqlite3_expanded_sql(pCk); |
| 192 |
printf("invariant-validity-check #1:\n%s\n", zSql); |
| 193 |
sqlite3_free(zSql); |
| 194 |
} |
| 195 |
|
| 196 |
rc = sqlite3_step(pCk); |
| 197 |
if( rc!=SQLITE_ROW |
| 198 |
|| sqlite3_column_text(pCk, 0)==0 |
| 199 |
|| strcmp((const char*)sqlite3_column_text(pCk,0),"ok")!=0 |
| 200 |
){ |
| 201 |
*pbCorrupt = 1; |
| 202 |
sqlite3_finalize(pCk); |
| 203 |
sqlite3_finalize(pTestStmt); |
| 204 |
return SQLITE_CORRUPT; |
| 205 |
} |
| 206 |
sqlite3_finalize(pCk); |
| 207 |
|
| 208 |
/* |
| 209 |
** If inverting the scan order also results in a miss, assume that the |
| 210 |
** query is ambiguous and do not report a fault. |
| 211 |
*/ |
| 212 |
sqlite3_db_config(db, SQLITE_DBCONFIG_REVERSE_SCANORDER, -1, &iOrigRSO); |
| 213 |
sqlite3_db_config(db, SQLITE_DBCONFIG_REVERSE_SCANORDER, !iOrigRSO, 0); |
| 214 |
sqlite3_prepare_v2(db, sqlite3_sql(pStmt), -1, &pCk, 0); |
| 215 |
sqlite3_db_config(db, SQLITE_DBCONFIG_REVERSE_SCANORDER, iOrigRSO, 0); |
| 216 |
if( eVerbosity>=2 ){ |
| 217 |
char *zSql = sqlite3_expanded_sql(pCk); |
| 218 |
printf("invariant-validity-check #2:\n%s\n", zSql); |
| 219 |
sqlite3_free(zSql); |
| 220 |
} |
| 221 |
bindDebugParameters(pCk); |
| 222 |
while( (rc = sqlite3_step(pCk))==SQLITE_ROW ){ |
| 223 |
for(i=0; i<nCol; i++){ |
| 224 |
if( !sameValue(pStmt, i, pTestStmt, i, 0) ) break; |
| 225 |
} |
| 226 |
if( i>=nCol ) break; |
| 227 |
} |
| 228 |
sqlite3_finalize(pCk); |
| 229 |
if( rc==SQLITE_DONE ){ |
| 230 |
sqlite3_finalize(pTestStmt); |
| 231 |
return SQLITE_DONE; |
| 232 |
} |
| 233 |
|
| 234 |
/* The original sameValue() comparison assumed a collating sequence |
| 235 |
** of "binary". It can sometimes get an incorrect result for different |
| 236 |
** collating sequences. So rerun the test with no assumptions about |
| 237 |
** collations. |
| 238 |
*/ |
| 239 |
rc = sqlite3_prepare_v2(db, |
| 240 |
"SELECT ?1=?2 OR ?1=?2 COLLATE nocase OR ?1=?2 COLLATE rtrim", |
| 241 |
-1, &pCk, 0); |
| 242 |
if( rc==SQLITE_OK ){ |
| 243 |
if( eVerbosity>=2 ){ |
| 244 |
char *zSql = sqlite3_expanded_sql(pCk); |
| 245 |
printf("invariant-validity-check #3:\n%s\n", zSql); |
| 246 |
sqlite3_free(zSql); |
| 247 |
} |
| 248 |
|
| 249 |
sqlite3_reset(pTestStmt); |
| 250 |
bindDebugParameters(pCk); |
| 251 |
while( (rc = sqlite3_step(pTestStmt))==SQLITE_ROW ){ |
| 252 |
for(i=0; i<nCol; i++){ |
| 253 |
if( !sameValue(pStmt, i, pTestStmt, i, pCk) ) break; |
| 254 |
} |
| 255 |
if( i>=nCol ){ |
| 256 |
sqlite3_finalize(pCk); |
| 257 |
goto not_a_fault; |
| 258 |
} |
| 259 |
} |
| 260 |
} |
| 261 |
sqlite3_finalize(pCk); |
| 262 |
|
| 263 |
/* Invariants do not necessarily work if there are virtual tables |
| 264 |
** or scalar subqueries involved in the query */ |
| 265 |
rc = sqlite3_prepare_v2(db, |
| 266 |
"SELECT 1 FROM bytecode(?1)" |
| 267 |
" WHERE opcode='VOpen' OR" |
| 268 |
" (opcode='Explain' AND p4 GLOB 'SCALAR SUBQUERY*')", |
| 269 |
-1, &pCk, 0); |
| 270 |
if( rc==SQLITE_OK ){ |
| 271 |
if( eVerbosity>=2 ){ |
| 272 |
char *zSql = sqlite3_expanded_sql(pCk); |
| 273 |
printf("invariant-validity-check #4:\n%s\n", zSql); |
| 274 |
sqlite3_free(zSql); |
| 275 |
} |
| 276 |
sqlite3_bind_pointer(pCk, 1, pStmt, "stmt-pointer", 0); |
| 277 |
rc = sqlite3_step(pCk); |
| 278 |
} |
| 279 |
sqlite3_finalize(pCk); |
| 280 |
if( rc==SQLITE_DONE ){ |
| 281 |
reportInvariantFailed(pStmt, pTestStmt, iRow, dbOpt, noOpt); |
| 282 |
return SQLITE_INTERNAL; |
| 283 |
}else if( eVerbosity>0 ){ |
| 284 |
printf("invariant-error ignored due to the use of virtual tables\n"); |
| 285 |
} |
| 286 |
} |
| 287 |
not_a_fault: |
| 288 |
sqlite3_finalize(pTestStmt); |
| 289 |
return SQLITE_OK; |
| 290 |
} |
| 291 |
|
| 292 |
/* |
| 293 |
** Generate SQL used to test a statement invariant. |
| 294 |
** |
| 295 |
** Return 0 if the iCnt is out of range. |
| 296 |
** |
| 297 |
** iCnt meanings: |
| 298 |
** |
| 299 |
** 0 SELECT * FROM (<query>) |
| 300 |
** 1 SELECT DISTINCT * FROM (<query>) |
| 301 |
** 2 SELECT * FROM (<query>) WHERE ORDER BY 1 |
| 302 |
** 3 SELECT DISTINCT * FROM (<query>) ORDER BY 1 |
| 303 |
** 4 SELECT * FROM (<query>) WHERE <all-columns>=<all-values> |
| 304 |
** 5 SELECT DISTINCT * FROM (<query>) WHERE <all-columns=<all-values |
| 305 |
** 6 SELECT * FROM (<query>) WHERE <all-column>=<all-value> ORDER BY 1 |
| 306 |
** 7 SELECT DISTINCT * FROM (<query>) WHERE <all-column>=<all-value> |
| 307 |
** ORDER BY 1 |
| 308 |
** N+0 SELECT * FROM (<query>) WHERE <nth-column>=<value> |
| 309 |
** N+1 SELECT DISTINCT * FROM (<query>) WHERE <Nth-column>=<value> |
| 310 |
** N+2 SELECT * FROM (<query>) WHERE <Nth-column>=<value> ORDER BY 1 |
| 311 |
** N+3 SELECT DISTINCT * FROM (<query>) WHERE <Nth-column>=<value> |
| 312 |
** ORDER BY N |
| 313 |
** |
| 314 |
*/ |
| 315 |
static char *fuzz_invariant_sql(sqlite3_stmt *pStmt, int iCnt){ |
| 316 |
const char *zIn; |
| 317 |
size_t nIn; |
| 318 |
const char *zAnd = "WHERE"; |
| 319 |
int i, j; |
| 320 |
sqlite3_str *pTest; |
| 321 |
sqlite3_stmt *pBase = 0; |
| 322 |
sqlite3 *db = sqlite3_db_handle(pStmt); |
| 323 |
int rc; |
| 324 |
int nCol = sqlite3_column_count(pStmt); |
| 325 |
int mxCnt; |
| 326 |
int bDistinct = 0; |
| 327 |
int bOrderBy = 0; |
| 328 |
int nParam = sqlite3_bind_parameter_count(pStmt); |
| 329 |
int hasGroupBy = 0; |
| 330 |
|
| 331 |
switch( iCnt % 4 ){ |
| 332 |
case 1: bDistinct = 1; break; |
| 333 |
case 2: bOrderBy = 1; break; |
| 334 |
case 3: bDistinct = bOrderBy = 1; break; |
| 335 |
} |
| 336 |
iCnt /= 4; |
| 337 |
mxCnt = nCol; |
| 338 |
if( iCnt<0 || iCnt>mxCnt ) return 0; |
| 339 |
zIn = sqlite3_sql(pStmt); |
| 340 |
if( zIn==0 ) return 0; |
| 341 |
nIn = strlen(zIn); |
| 342 |
while( nIn>0 && (isspace(zIn[nIn-1]) || zIn[nIn-1]==';') ) nIn--; |
| 343 |
if( strchr(zIn, '?') ) return 0; |
| 344 |
pTest = sqlite3_str_new(0); |
| 345 |
sqlite3_str_appendf(pTest, "SELECT %s* FROM (", |
| 346 |
bDistinct ? "DISTINCT " : ""); |
| 347 |
sqlite3_str_append(pTest, zIn, (int)nIn); |
| 348 |
sqlite3_str_append(pTest, ")", 1); |
| 349 |
rc = sqlite3_prepare_v2(db, sqlite3_str_value(pTest), -1, &pBase, 0); |
| 350 |
if( rc ){ |
| 351 |
sqlite3_finalize(pBase); |
| 352 |
pBase = pStmt; |
| 353 |
} |
| 354 |
hasGroupBy = sqlite3_strlike("%GROUP BY%",zIn,0)==0; |
| 355 |
bindDebugParameters(pBase); |
| 356 |
for(i=0; i<sqlite3_column_count(pStmt); i++){ |
| 357 |
const char *zColName = sqlite3_column_name(pBase,i); |
| 358 |
const char *zSuffix = zColName ? strrchr(zColName, ':') : 0; |
| 359 |
if( zSuffix |
| 360 |
&& isdigit(zSuffix[1]) |
| 361 |
&& (zSuffix[1]>'3' || isdigit(zSuffix[2])) |
| 362 |
){ |
| 363 |
/* This is a randomized column name and so cannot be used in the |
| 364 |
** WHERE clause. */ |
| 365 |
continue; |
| 366 |
} |
| 367 |
for(j=0; j<i; j++){ |
| 368 |
const char *zPrior = sqlite3_column_name(pBase, j); |
| 369 |
if( sqlite3_stricmp(zPrior, zColName)==0 ) break; |
| 370 |
} |
| 371 |
if( j<i ){ |
| 372 |
/* Duplicate column name */ |
| 373 |
continue; |
| 374 |
} |
| 375 |
if( iCnt==0 ) continue; |
| 376 |
if( iCnt>1 && i+2!=iCnt ) continue; |
| 377 |
if( zColName==0 ) continue; |
| 378 |
if( sqlite3_column_type(pStmt, i)==SQLITE_NULL ){ |
| 379 |
const char *zPlus = hasGroupBy ? "+" : ""; |
| 380 |
sqlite3_str_appendf(pTest, " %s %s\"%w\" ISNULL", zAnd, zPlus, zColName); |
| 381 |
}else{ |
| 382 |
sqlite3_str_appendf(pTest, " %s \"%w\"=?%d", zAnd, zColName, |
| 383 |
i+1+nParam); |
| 384 |
} |
| 385 |
zAnd = "AND"; |
| 386 |
} |
| 387 |
if( pBase!=pStmt ) sqlite3_finalize(pBase); |
| 388 |
if( bOrderBy ){ |
| 389 |
sqlite3_str_appendf(pTest, " ORDER BY %d", iCnt>2 ? iCnt-1 : 1); |
| 390 |
} |
| 391 |
return sqlite3_str_finish(pTest); |
| 392 |
} |
| 393 |
|
| 394 |
/* |
| 395 |
** Return true if and only if v1 and is the same as v2. |
| 396 |
*/ |
| 397 |
static int sameValue( |
| 398 |
sqlite3_stmt *pS1, int i1, /* Value to text on the left */ |
| 399 |
sqlite3_stmt *pS2, int i2, /* Value to test on the right */ |
| 400 |
sqlite3_stmt *pTestCompare /* COLLATE comparison statement or NULL */ |
| 401 |
){ |
| 402 |
int x = 1; |
| 403 |
int t1 = sqlite3_column_type(pS1,i1); |
| 404 |
int t2 = sqlite3_column_type(pS2,i2); |
| 405 |
if( t1!=t2 ){ |
| 406 |
if( (t1==SQLITE_INTEGER && t2==SQLITE_FLOAT) |
| 407 |
|| (t1==SQLITE_FLOAT && t2==SQLITE_INTEGER) |
| 408 |
){ |
| 409 |
/* Comparison of numerics is ok */ |
| 410 |
}else{ |
| 411 |
return 0; |
| 412 |
} |
| 413 |
} |
| 414 |
switch( sqlite3_column_type(pS1,i1) ){ |
| 415 |
case SQLITE_INTEGER: { |
| 416 |
x = sqlite3_column_int64(pS1,i1)==sqlite3_column_int64(pS2,i2); |
| 417 |
break; |
| 418 |
} |
| 419 |
case SQLITE_FLOAT: { |
| 420 |
x = sqlite3_column_double(pS1,i1)==sqlite3_column_double(pS2,i2); |
| 421 |
break; |
| 422 |
} |
| 423 |
case SQLITE_TEXT: { |
| 424 |
int e1 = sqlite3_value_encoding(sqlite3_column_value(pS1,i1)); |
| 425 |
int e2 = sqlite3_value_encoding(sqlite3_column_value(pS2,i2)); |
| 426 |
if( e1!=e2 ){ |
| 427 |
const char *z1 = (const char*)sqlite3_column_text(pS1,i1); |
| 428 |
const char *z2 = (const char*)sqlite3_column_text(pS2,i2); |
| 429 |
x = ((z1==0 && z2==0) || (z1!=0 && z2!=0 && strcmp(z1,z1)==0)); |
| 430 |
printf("Encodings differ. %d on left and %d on right\n", e1, e2); |
| 431 |
abort(); |
| 432 |
} |
| 433 |
if( pTestCompare ){ |
| 434 |
sqlite3_bind_value(pTestCompare, 1, sqlite3_column_value(pS1,i1)); |
| 435 |
sqlite3_bind_value(pTestCompare, 2, sqlite3_column_value(pS2,i2)); |
| 436 |
x = sqlite3_step(pTestCompare)==SQLITE_ROW |
| 437 |
&& sqlite3_column_int(pTestCompare,0)!=0; |
| 438 |
sqlite3_reset(pTestCompare); |
| 439 |
break; |
| 440 |
} |
| 441 |
if( e1!=SQLITE_UTF8 ){ |
| 442 |
int len1 = sqlite3_column_bytes16(pS1,i1); |
| 443 |
const unsigned char *b1 = sqlite3_column_blob(pS1,i1); |
| 444 |
int len2 = sqlite3_column_bytes16(pS2,i2); |
| 445 |
const unsigned char *b2 = sqlite3_column_blob(pS2,i2); |
| 446 |
if( len1!=len2 ){ |
| 447 |
x = 0; |
| 448 |
}else if( len1==0 ){ |
| 449 |
x = 1; |
| 450 |
}else{ |
| 451 |
x = (b1!=0 && b2!=0 && memcmp(b1,b2,len1)==0); |
| 452 |
} |
| 453 |
break; |
| 454 |
} |
| 455 |
/* Fall through into the SQLITE_BLOB case */ |
| 456 |
} |
| 457 |
case SQLITE_BLOB: { |
| 458 |
int len1 = sqlite3_column_bytes(pS1,i1); |
| 459 |
const unsigned char *b1 = sqlite3_column_blob(pS1,i1); |
| 460 |
int len2 = sqlite3_column_bytes(pS2,i2); |
| 461 |
const unsigned char *b2 = sqlite3_column_blob(pS2,i2); |
| 462 |
if( len1!=len2 ){ |
| 463 |
x = 0; |
| 464 |
}else if( len1==0 ){ |
| 465 |
x = 1; |
| 466 |
}else{ |
| 467 |
x = (b1!=0 && b2!=0 && memcmp(b1,b2,len1)==0); |
| 468 |
} |
| 469 |
break; |
| 470 |
} |
| 471 |
} |
| 472 |
return x; |
| 473 |
} |
| 474 |
|
| 475 |
/* |
| 476 |
** Print binary data as hex |
| 477 |
*/ |
| 478 |
static void printHex(const unsigned char *a, int n, int mx){ |
| 479 |
int j; |
| 480 |
for(j=0; j<mx && j<n; j++){ |
| 481 |
printf("%02x", a[j]); |
| 482 |
} |
| 483 |
if( j<n ) printf("..."); |
| 484 |
} |
| 485 |
|
| 486 |
/* |
| 487 |
** Print a single row from the prepared statement |
| 488 |
*/ |
| 489 |
static void printRow(sqlite3_stmt *pStmt, int iRow){ |
| 490 |
int i, n, nCol; |
| 491 |
unsigned const char *data; |
| 492 |
nCol = sqlite3_column_count(pStmt); |
| 493 |
for(i=0; i<nCol; i++){ |
| 494 |
printf("row%d.col%d = ", iRow, i); |
| 495 |
switch( sqlite3_column_type(pStmt, i) ){ |
| 496 |
case SQLITE_NULL: { |
| 497 |
printf("NULL\n"); |
| 498 |
break; |
| 499 |
} |
| 500 |
case SQLITE_INTEGER: { |
| 501 |
printf("(integer) %lld\n", sqlite3_column_int64(pStmt, i)); |
| 502 |
break; |
| 503 |
} |
| 504 |
case SQLITE_FLOAT: { |
| 505 |
printf("(float) %f\n", sqlite3_column_double(pStmt, i)); |
| 506 |
break; |
| 507 |
} |
| 508 |
case SQLITE_TEXT: { |
| 509 |
switch( sqlite3_value_encoding(sqlite3_column_value(pStmt,i)) ){ |
| 510 |
case SQLITE_UTF8: { |
| 511 |
printf("(utf8) x'"); |
| 512 |
n = sqlite3_column_bytes(pStmt, i); |
| 513 |
data = sqlite3_column_blob(pStmt, i); |
| 514 |
printHex(data, n, 35); |
| 515 |
printf("'\n"); |
| 516 |
break; |
| 517 |
} |
| 518 |
case SQLITE_UTF16BE: { |
| 519 |
printf("(utf16be) x'"); |
| 520 |
n = sqlite3_column_bytes16(pStmt, i); |
| 521 |
data = sqlite3_column_blob(pStmt, i); |
| 522 |
printHex(data, n, 35); |
| 523 |
printf("'\n"); |
| 524 |
break; |
| 525 |
} |
| 526 |
case SQLITE_UTF16LE: { |
| 527 |
printf("(utf16le) x'"); |
| 528 |
n = sqlite3_column_bytes16(pStmt, i); |
| 529 |
data = sqlite3_column_blob(pStmt, i); |
| 530 |
printHex(data, n, 35); |
| 531 |
printf("'\n"); |
| 532 |
break; |
| 533 |
} |
| 534 |
default: { |
| 535 |
printf("Illegal return from sqlite3_value_encoding(): %d\n", |
| 536 |
sqlite3_value_encoding(sqlite3_column_value(pStmt,i))); |
| 537 |
abort(); |
| 538 |
} |
| 539 |
} |
| 540 |
break; |
| 541 |
} |
| 542 |
case SQLITE_BLOB: { |
| 543 |
n = sqlite3_column_bytes(pStmt, i); |
| 544 |
data = sqlite3_column_blob(pStmt, i); |
| 545 |
printf("(blob %d bytes) x'", n); |
| 546 |
printHex(data, n, 35); |
| 547 |
printf("'\n"); |
| 548 |
break; |
| 549 |
} |
| 550 |
} |
| 551 |
} |
| 552 |
} |
| 553 |
|
| 554 |
/* |
| 555 |
** Report a failure of the invariant: The current output row of pOrig |
| 556 |
** does not appear in any row of the output from pTest. |
| 557 |
*/ |
| 558 |
static void reportInvariantFailed( |
| 559 |
sqlite3_stmt *pOrig, /* The original query */ |
| 560 |
sqlite3_stmt *pTest, /* The alternative test query with a missing row */ |
| 561 |
int iRow, /* Row number in pOrig */ |
| 562 |
unsigned int dbOpt, /* Optimization flags on pOrig */ |
| 563 |
int noOpt /* True if opt flags inverted for pTest */ |
| 564 |
){ |
| 565 |
int iTestRow = 0; |
| 566 |
printf("Invariant check failed on row %d.\n", iRow); |
| 567 |
printf("Original query (opt-flags: 0x%08x) --------------------------\n", |
| 568 |
dbOpt); |
| 569 |
printf("%s\n", sqlite3_expanded_sql(pOrig)); |
| 570 |
printf("Alternative query (opt-flags: 0x%08x) -----------------------\n", |
| 571 |
noOpt ? ~dbOpt : dbOpt); |
| 572 |
printf("%s\n", sqlite3_expanded_sql(pTest)); |
| 573 |
printf("Result row that is missing from the alternative -----------------\n"); |
| 574 |
printRow(pOrig, iRow); |
| 575 |
printf("Complete results from the alternative query ---------------------\n"); |
| 576 |
sqlite3_reset(pTest); |
| 577 |
while( sqlite3_step(pTest)==SQLITE_ROW ){ |
| 578 |
iTestRow++; |
| 579 |
printRow(pTest, iTestRow); |
| 580 |
} |
| 581 |
sqlite3_finalize(pTest); |
| 582 |
abort(); |
| 583 |
} |