drh | a1f79da | 2022-06-14 19:12:25 +0000 | [diff] [blame] | 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); |
drh | 2d75c1a | 2022-10-24 12:38:32 +0000 | [diff] [blame] | 32 | static int sameValue(sqlite3_stmt*,int,sqlite3_stmt*,int,sqlite3_stmt*); |
drh | a1f79da | 2022-06-14 19:12:25 +0000 | [diff] [blame] | 33 | static void reportInvariantFailed(sqlite3_stmt*,sqlite3_stmt*,int); |
| 34 | |
| 35 | /* |
| 36 | ** Do an invariant check on pStmt. iCnt determines which invariant check to |
| 37 | ** perform. The first check is iCnt==0. |
| 38 | ** |
| 39 | ** *pbCorrupt is a flag that, if true, indicates that the database file |
| 40 | ** is known to be corrupt. A value of non-zero means "yes, the database |
| 41 | ** is corrupt". A zero value means "we do not know whether or not the |
| 42 | ** database is corrupt". The value might be set prior to entry, or this |
| 43 | ** routine might set the value. |
| 44 | ** |
| 45 | ** Return values: |
| 46 | ** |
| 47 | ** SQLITE_OK This check was successful. |
| 48 | ** |
drh | 1ffb6be | 2022-10-12 18:40:25 +0000 | [diff] [blame] | 49 | ** SQLITE_DONE iCnt is out of range. The caller typically sets |
| 50 | ** up a loop on iCnt starting with zero, and increments |
| 51 | ** iCnt until this code is returned. |
drh | a1f79da | 2022-06-14 19:12:25 +0000 | [diff] [blame] | 52 | ** |
| 53 | ** SQLITE_CORRUPT The invariant failed, but the underlying database |
| 54 | ** file is indicating that it is corrupt, which might |
drh | 1ffb6be | 2022-10-12 18:40:25 +0000 | [diff] [blame] | 55 | ** be the cause of the malfunction. The *pCorrupt |
| 56 | ** value will also be set. |
drh | a1f79da | 2022-06-14 19:12:25 +0000 | [diff] [blame] | 57 | ** |
| 58 | ** SQLITE_INTERNAL The invariant failed, and the database file is not |
| 59 | ** corrupt. (This never happens because this function |
| 60 | ** will call abort() following an invariant failure.) |
| 61 | ** |
| 62 | ** (other) Some other kind of error occurred. |
| 63 | */ |
| 64 | int fuzz_invariant( |
| 65 | sqlite3 *db, /* The database connection */ |
| 66 | sqlite3_stmt *pStmt, /* Test statement stopped on an SQLITE_ROW */ |
| 67 | int iCnt, /* Invariant sequence number, starting at 0 */ |
drh | 6388036 | 2022-06-17 16:09:47 +0000 | [diff] [blame] | 68 | int iRow, /* Current row number */ |
| 69 | int nRow, /* Number of output rows from pStmt */ |
drh | e3bf2c8 | 2022-06-15 16:26:37 +0000 | [diff] [blame] | 70 | int *pbCorrupt, /* IN/OUT: Flag indicating a corrupt database file */ |
| 71 | int eVerbosity /* How much debugging output */ |
drh | a1f79da | 2022-06-14 19:12:25 +0000 | [diff] [blame] | 72 | ){ |
| 73 | char *zTest; |
| 74 | sqlite3_stmt *pTestStmt = 0; |
| 75 | int rc; |
| 76 | int i; |
| 77 | int nCol; |
drh | 36f904f | 2022-06-17 15:11:31 +0000 | [diff] [blame] | 78 | int nParam; |
drh | a1f79da | 2022-06-14 19:12:25 +0000 | [diff] [blame] | 79 | |
| 80 | if( *pbCorrupt ) return SQLITE_DONE; |
drh | 36f904f | 2022-06-17 15:11:31 +0000 | [diff] [blame] | 81 | nParam = sqlite3_bind_parameter_count(pStmt); |
| 82 | if( nParam>100 ) return SQLITE_DONE; |
drh | a1f79da | 2022-06-14 19:12:25 +0000 | [diff] [blame] | 83 | zTest = fuzz_invariant_sql(pStmt, iCnt); |
| 84 | if( zTest==0 ) return SQLITE_DONE; |
| 85 | rc = sqlite3_prepare_v2(db, zTest, -1, &pTestStmt, 0); |
drh | a1f79da | 2022-06-14 19:12:25 +0000 | [diff] [blame] | 86 | if( rc ){ |
drh | e3bf2c8 | 2022-06-15 16:26:37 +0000 | [diff] [blame] | 87 | if( eVerbosity ){ |
| 88 | printf("invariant compile failed: %s\n%s\n", |
| 89 | sqlite3_errmsg(db), zTest); |
| 90 | } |
| 91 | sqlite3_free(zTest); |
drh | a1f79da | 2022-06-14 19:12:25 +0000 | [diff] [blame] | 92 | sqlite3_finalize(pTestStmt); |
| 93 | return rc; |
| 94 | } |
drh | e3bf2c8 | 2022-06-15 16:26:37 +0000 | [diff] [blame] | 95 | sqlite3_free(zTest); |
drh | a1f79da | 2022-06-14 19:12:25 +0000 | [diff] [blame] | 96 | nCol = sqlite3_column_count(pStmt); |
| 97 | for(i=0; i<nCol; i++){ |
drh | d0d21f5 | 2022-06-18 14:50:43 +0000 | [diff] [blame] | 98 | rc = sqlite3_bind_value(pTestStmt,i+1+nParam,sqlite3_column_value(pStmt,i)); |
| 99 | if( rc!=SQLITE_OK && rc!=SQLITE_RANGE ){ |
| 100 | sqlite3_finalize(pTestStmt); |
| 101 | return rc; |
| 102 | } |
drh | a1f79da | 2022-06-14 19:12:25 +0000 | [diff] [blame] | 103 | } |
drh | e3bf2c8 | 2022-06-15 16:26:37 +0000 | [diff] [blame] | 104 | if( eVerbosity>=2 ){ |
| 105 | char *zSql = sqlite3_expanded_sql(pTestStmt); |
| 106 | printf("invariant-sql #%d:\n%s\n", iCnt, zSql); |
| 107 | sqlite3_free(zSql); |
| 108 | } |
drh | a1f79da | 2022-06-14 19:12:25 +0000 | [diff] [blame] | 109 | while( (rc = sqlite3_step(pTestStmt))==SQLITE_ROW ){ |
| 110 | for(i=0; i<nCol; i++){ |
drh | 2d75c1a | 2022-10-24 12:38:32 +0000 | [diff] [blame] | 111 | if( !sameValue(pStmt, i, pTestStmt, i, 0) ) break; |
drh | a1f79da | 2022-06-14 19:12:25 +0000 | [diff] [blame] | 112 | } |
| 113 | if( i>=nCol ) break; |
| 114 | } |
drh | 1373699 | 2022-06-18 20:20:30 +0000 | [diff] [blame] | 115 | if( rc==SQLITE_DONE ){ |
drh | a1f79da | 2022-06-14 19:12:25 +0000 | [diff] [blame] | 116 | /* No matching output row found */ |
| 117 | sqlite3_stmt *pCk = 0; |
drh | 2d75c1a | 2022-10-24 12:38:32 +0000 | [diff] [blame] | 118 | |
| 119 | /* This is not a fault if the database file is corrupt, because anything |
| 120 | ** can happen with a corrupt database file */ |
drh | a1f79da | 2022-06-14 19:12:25 +0000 | [diff] [blame] | 121 | rc = sqlite3_prepare_v2(db, "PRAGMA integrity_check", -1, &pCk, 0); |
| 122 | if( rc ){ |
| 123 | sqlite3_finalize(pCk); |
| 124 | sqlite3_finalize(pTestStmt); |
| 125 | return rc; |
| 126 | } |
| 127 | rc = sqlite3_step(pCk); |
| 128 | if( rc!=SQLITE_ROW |
| 129 | || sqlite3_column_text(pCk, 0)==0 |
| 130 | || strcmp((const char*)sqlite3_column_text(pCk,0),"ok")!=0 |
| 131 | ){ |
| 132 | *pbCorrupt = 1; |
| 133 | sqlite3_finalize(pCk); |
| 134 | sqlite3_finalize(pTestStmt); |
| 135 | return SQLITE_CORRUPT; |
| 136 | } |
| 137 | sqlite3_finalize(pCk); |
drh | 2d75c1a | 2022-10-24 12:38:32 +0000 | [diff] [blame] | 138 | |
drh | a9d18d8 | 2022-12-16 18:32:07 +0000 | [diff] [blame] | 139 | if( sqlite3_strlike("%group%by%",sqlite3_sql(pStmt),0)==0 ){ |
| 140 | /* |
| 141 | ** If there is a GROUP BY clause, it might not cover every term in the |
| 142 | ** output. And then non-covered terms can take on a value from any |
| 143 | ** row in the result set. This can cause differing answers. |
drh | 703c208 | 2022-09-20 17:21:54 +0000 | [diff] [blame] | 144 | */ |
| 145 | goto not_a_fault; |
| 146 | } |
drh | 2d75c1a | 2022-10-24 12:38:32 +0000 | [diff] [blame] | 147 | |
drh | 1ffb6be | 2022-10-12 18:40:25 +0000 | [diff] [blame] | 148 | if( sqlite3_strlike("%limit%)%order%by%", sqlite3_sql(pTestStmt),0)==0 ){ |
| 149 | /* crash-89bd6a6f8c6166e9a4c5f47b3e70b225f69b76c6 |
| 150 | ** Original statement is: |
| 151 | ** |
| 152 | ** SELECT a,b,c* FROM t1 LIMIT 1%5<4 |
| 153 | ** |
| 154 | ** When running: |
| 155 | ** |
| 156 | ** SELECT * FROM (...) ORDER BY 1 |
| 157 | ** |
| 158 | ** A different subset of the rows come out |
| 159 | */ |
| 160 | goto not_a_fault; |
| 161 | } |
drh | 2d75c1a | 2022-10-24 12:38:32 +0000 | [diff] [blame] | 162 | |
| 163 | /* The original sameValue() comparison assumed a collating sequence |
| 164 | ** of "binary". It can sometimes get an incorrect result for different |
| 165 | ** collating sequences. So rerun the test with no assumptions about |
| 166 | ** collations. |
| 167 | */ |
| 168 | rc = sqlite3_prepare_v2(db, |
| 169 | "SELECT ?1=?2 OR ?1=?2 COLLATE nocase OR ?1=?2 COLLATE rtrim", |
| 170 | -1, &pCk, 0); |
| 171 | if( rc==SQLITE_OK ){ |
| 172 | sqlite3_reset(pTestStmt); |
| 173 | while( (rc = sqlite3_step(pTestStmt))==SQLITE_ROW ){ |
| 174 | for(i=0; i<nCol; i++){ |
| 175 | if( !sameValue(pStmt, i, pTestStmt, i, pCk) ) break; |
| 176 | } |
| 177 | if( i>=nCol ){ |
| 178 | sqlite3_finalize(pCk); |
| 179 | goto not_a_fault; |
| 180 | } |
| 181 | } |
| 182 | } |
| 183 | sqlite3_finalize(pCk); |
| 184 | |
| 185 | /* Invariants do not necessarily work if there are virtual tables |
| 186 | ** involved in the query */ |
drh | a913f9b | 2022-06-15 10:37:16 +0000 | [diff] [blame] | 187 | rc = sqlite3_prepare_v2(db, |
| 188 | "SELECT 1 FROM bytecode(?1) WHERE opcode='VOpen'", -1, &pCk, 0); |
drh | 36f904f | 2022-06-17 15:11:31 +0000 | [diff] [blame] | 189 | if( rc==SQLITE_OK ){ |
| 190 | sqlite3_bind_pointer(pCk, 1, pStmt, "stmt-pointer", 0); |
| 191 | rc = sqlite3_step(pCk); |
| 192 | } |
drh | a913f9b | 2022-06-15 10:37:16 +0000 | [diff] [blame] | 193 | sqlite3_finalize(pCk); |
| 194 | if( rc==SQLITE_DONE ){ |
| 195 | reportInvariantFailed(pStmt, pTestStmt, iRow); |
| 196 | return SQLITE_INTERNAL; |
drh | e3bf2c8 | 2022-06-15 16:26:37 +0000 | [diff] [blame] | 197 | }else if( eVerbosity>0 ){ |
| 198 | printf("invariant-error ignored due to the use of virtual tables\n"); |
drh | a913f9b | 2022-06-15 10:37:16 +0000 | [diff] [blame] | 199 | } |
drh | a1f79da | 2022-06-14 19:12:25 +0000 | [diff] [blame] | 200 | } |
drh | 703c208 | 2022-09-20 17:21:54 +0000 | [diff] [blame] | 201 | not_a_fault: |
drh | a1f79da | 2022-06-14 19:12:25 +0000 | [diff] [blame] | 202 | sqlite3_finalize(pTestStmt); |
| 203 | return SQLITE_OK; |
| 204 | } |
| 205 | |
| 206 | |
| 207 | /* |
| 208 | ** Generate SQL used to test a statement invariant. |
| 209 | ** |
| 210 | ** Return 0 if the iCnt is out of range. |
drh | 1ffb6be | 2022-10-12 18:40:25 +0000 | [diff] [blame] | 211 | ** |
| 212 | ** iCnt meanings: |
| 213 | ** |
| 214 | ** 0 SELECT * FROM (<query>) |
| 215 | ** 1 SELECT DISTINCT * FROM (<query>) |
| 216 | ** 2 SELECT * FROM (<query>) WHERE ORDER BY 1 |
| 217 | ** 3 SELECT DISTINCT * FROM (<query>) ORDER BY 1 |
| 218 | ** 4 SELECT * FROM (<query>) WHERE <all-columns>=<all-values> |
| 219 | ** 5 SELECT DISTINCT * FROM (<query>) WHERE <all-columns=<all-values |
| 220 | ** 6 SELECT * FROM (<query>) WHERE <all-column>=<all-value> ORDER BY 1 |
| 221 | ** 7 SELECT DISTINCT * FROM (<query>) WHERE <all-column>=<all-value> |
| 222 | ** ORDER BY 1 |
| 223 | ** N+0 SELECT * FROM (<query>) WHERE <nth-column>=<value> |
| 224 | ** N+1 SELECT DISTINCT * FROM (<query>) WHERE <Nth-column>=<value> |
| 225 | ** N+2 SELECT * FROM (<query>) WHERE <Nth-column>=<value> ORDER BY 1 |
| 226 | ** N+3 SELECT DISTINCT * FROM (<query>) WHERE <Nth-column>=<value> |
| 227 | ** ORDER BY N |
| 228 | ** |
drh | a1f79da | 2022-06-14 19:12:25 +0000 | [diff] [blame] | 229 | */ |
| 230 | static char *fuzz_invariant_sql(sqlite3_stmt *pStmt, int iCnt){ |
| 231 | const char *zIn; |
| 232 | size_t nIn; |
| 233 | const char *zAnd = "WHERE"; |
drh | d980442 | 2022-12-16 12:07:48 +0000 | [diff] [blame] | 234 | int i, j; |
drh | a1f79da | 2022-06-14 19:12:25 +0000 | [diff] [blame] | 235 | sqlite3_str *pTest; |
| 236 | sqlite3_stmt *pBase = 0; |
| 237 | sqlite3 *db = sqlite3_db_handle(pStmt); |
| 238 | int rc; |
drh | e3bf2c8 | 2022-06-15 16:26:37 +0000 | [diff] [blame] | 239 | int nCol = sqlite3_column_count(pStmt); |
| 240 | int mxCnt; |
drh | 8f9261a | 2022-06-15 20:18:44 +0000 | [diff] [blame] | 241 | int bDistinct = 0; |
| 242 | int bOrderBy = 0; |
drh | 36f904f | 2022-06-17 15:11:31 +0000 | [diff] [blame] | 243 | int nParam = sqlite3_bind_parameter_count(pStmt); |
drh | a1f79da | 2022-06-14 19:12:25 +0000 | [diff] [blame] | 244 | |
drh | 8f9261a | 2022-06-15 20:18:44 +0000 | [diff] [blame] | 245 | switch( iCnt % 4 ){ |
| 246 | case 1: bDistinct = 1; break; |
| 247 | case 2: bOrderBy = 1; break; |
| 248 | case 3: bDistinct = bOrderBy = 1; break; |
| 249 | } |
| 250 | iCnt /= 4; |
drh | 6388036 | 2022-06-17 16:09:47 +0000 | [diff] [blame] | 251 | mxCnt = nCol; |
drh | e3bf2c8 | 2022-06-15 16:26:37 +0000 | [diff] [blame] | 252 | if( iCnt<0 || iCnt>mxCnt ) return 0; |
drh | a1f79da | 2022-06-14 19:12:25 +0000 | [diff] [blame] | 253 | zIn = sqlite3_sql(pStmt); |
| 254 | if( zIn==0 ) return 0; |
| 255 | nIn = strlen(zIn); |
| 256 | while( nIn>0 && (isspace(zIn[nIn-1]) || zIn[nIn-1]==';') ) nIn--; |
| 257 | if( strchr(zIn, '?') ) return 0; |
| 258 | pTest = sqlite3_str_new(0); |
drh | 1ffb6be | 2022-10-12 18:40:25 +0000 | [diff] [blame] | 259 | sqlite3_str_appendf(pTest, "SELECT %s* FROM (", |
| 260 | bDistinct ? "DISTINCT " : ""); |
drh | 053bb22 | 2022-10-28 18:52:05 +0000 | [diff] [blame] | 261 | sqlite3_str_append(pTest, zIn, (int)nIn); |
drh | 1ffb6be | 2022-10-12 18:40:25 +0000 | [diff] [blame] | 262 | sqlite3_str_append(pTest, ")", 1); |
drh | a1f79da | 2022-06-14 19:12:25 +0000 | [diff] [blame] | 263 | rc = sqlite3_prepare_v2(db, sqlite3_str_value(pTest), -1, &pBase, 0); |
| 264 | if( rc ){ |
| 265 | sqlite3_finalize(pBase); |
| 266 | pBase = pStmt; |
| 267 | } |
| 268 | for(i=0; i<sqlite3_column_count(pStmt); i++){ |
drh | a913f9b | 2022-06-15 10:37:16 +0000 | [diff] [blame] | 269 | const char *zColName = sqlite3_column_name(pBase,i); |
drh | 2a40a88 | 2022-06-24 11:02:42 +0000 | [diff] [blame] | 270 | const char *zSuffix = zColName ? strrchr(zColName, ':') : 0; |
drh | a913f9b | 2022-06-15 10:37:16 +0000 | [diff] [blame] | 271 | if( zSuffix |
| 272 | && isdigit(zSuffix[1]) |
| 273 | && (zSuffix[1]>'3' || isdigit(zSuffix[2])) |
| 274 | ){ |
| 275 | /* This is a randomized column name and so cannot be used in the |
| 276 | ** WHERE clause. */ |
| 277 | continue; |
| 278 | } |
drh | d980442 | 2022-12-16 12:07:48 +0000 | [diff] [blame] | 279 | for(j=0; j<i; j++){ |
| 280 | const char *zPrior = sqlite3_column_name(pBase, j); |
| 281 | if( sqlite3_stricmp(zPrior, zColName)==0 ) break; |
| 282 | } |
| 283 | if( j<i ){ |
| 284 | /* Duplicate column name */ |
| 285 | continue; |
| 286 | } |
drh | 1ffb6be | 2022-10-12 18:40:25 +0000 | [diff] [blame] | 287 | if( iCnt==0 ) continue; |
| 288 | if( iCnt>1 && i+2!=iCnt ) continue; |
drh | 8fa6206 | 2022-06-18 10:26:12 +0000 | [diff] [blame] | 289 | if( zColName==0 ) continue; |
drh | a1f79da | 2022-06-14 19:12:25 +0000 | [diff] [blame] | 290 | if( sqlite3_column_type(pStmt, i)==SQLITE_NULL ){ |
drh | a913f9b | 2022-06-15 10:37:16 +0000 | [diff] [blame] | 291 | sqlite3_str_appendf(pTest, " %s \"%w\" ISNULL", zAnd, zColName); |
drh | a1f79da | 2022-06-14 19:12:25 +0000 | [diff] [blame] | 292 | }else{ |
drh | 36f904f | 2022-06-17 15:11:31 +0000 | [diff] [blame] | 293 | sqlite3_str_appendf(pTest, " %s \"%w\"=?%d", zAnd, zColName, |
| 294 | i+1+nParam); |
drh | a1f79da | 2022-06-14 19:12:25 +0000 | [diff] [blame] | 295 | } |
| 296 | zAnd = "AND"; |
| 297 | } |
| 298 | if( pBase!=pStmt ) sqlite3_finalize(pBase); |
drh | 8f9261a | 2022-06-15 20:18:44 +0000 | [diff] [blame] | 299 | if( bOrderBy ){ |
drh | 1ffb6be | 2022-10-12 18:40:25 +0000 | [diff] [blame] | 300 | sqlite3_str_appendf(pTest, " ORDER BY %d", iCnt>2 ? iCnt-1 : 1); |
drh | 8f9261a | 2022-06-15 20:18:44 +0000 | [diff] [blame] | 301 | } |
drh | a1f79da | 2022-06-14 19:12:25 +0000 | [diff] [blame] | 302 | return sqlite3_str_finish(pTest); |
| 303 | } |
| 304 | |
| 305 | /* |
| 306 | ** Return true if and only if v1 and is the same as v2. |
| 307 | */ |
drh | 2d75c1a | 2022-10-24 12:38:32 +0000 | [diff] [blame] | 308 | static int sameValue( |
| 309 | sqlite3_stmt *pS1, int i1, /* Value to text on the left */ |
| 310 | sqlite3_stmt *pS2, int i2, /* Value to test on the right */ |
| 311 | sqlite3_stmt *pTestCompare /* COLLATE comparison statement or NULL */ |
| 312 | ){ |
drh | a1f79da | 2022-06-14 19:12:25 +0000 | [diff] [blame] | 313 | int x = 1; |
drh | ea64cb3 | 2022-06-17 16:32:21 +0000 | [diff] [blame] | 314 | int t1 = sqlite3_column_type(pS1,i1); |
| 315 | int t2 = sqlite3_column_type(pS2,i2); |
| 316 | if( t1!=t2 ){ |
| 317 | if( (t1==SQLITE_INTEGER && t2==SQLITE_FLOAT) |
| 318 | || (t1==SQLITE_FLOAT && t2==SQLITE_INTEGER) |
| 319 | ){ |
| 320 | /* Comparison of numerics is ok */ |
| 321 | }else{ |
| 322 | return 0; |
| 323 | } |
| 324 | } |
drh | 6efabd6 | 2022-06-17 12:25:33 +0000 | [diff] [blame] | 325 | switch( sqlite3_column_type(pS1,i1) ){ |
drh | a1f79da | 2022-06-14 19:12:25 +0000 | [diff] [blame] | 326 | case SQLITE_INTEGER: { |
drh | 6efabd6 | 2022-06-17 12:25:33 +0000 | [diff] [blame] | 327 | x = sqlite3_column_int64(pS1,i1)==sqlite3_column_int64(pS2,i2); |
drh | a1f79da | 2022-06-14 19:12:25 +0000 | [diff] [blame] | 328 | break; |
| 329 | } |
| 330 | case SQLITE_FLOAT: { |
drh | 6efabd6 | 2022-06-17 12:25:33 +0000 | [diff] [blame] | 331 | x = sqlite3_column_double(pS1,i1)==sqlite3_column_double(pS2,i2); |
drh | a1f79da | 2022-06-14 19:12:25 +0000 | [diff] [blame] | 332 | break; |
| 333 | } |
| 334 | case SQLITE_TEXT: { |
drh | 1ffb6be | 2022-10-12 18:40:25 +0000 | [diff] [blame] | 335 | int e1 = sqlite3_value_encoding(sqlite3_column_value(pS1,i1)); |
| 336 | int e2 = sqlite3_value_encoding(sqlite3_column_value(pS2,i2)); |
| 337 | if( e1!=e2 ){ |
| 338 | const char *z1 = (const char*)sqlite3_column_text(pS1,i1); |
| 339 | const char *z2 = (const char*)sqlite3_column_text(pS2,i2); |
| 340 | x = ((z1==0 && z2==0) || (z1!=0 && z2!=0 && strcmp(z1,z1)==0)); |
| 341 | printf("Encodings differ. %d on left and %d on right\n", e1, e2); |
drh | 2d75c1a | 2022-10-24 12:38:32 +0000 | [diff] [blame] | 342 | abort(); |
| 343 | } |
| 344 | if( pTestCompare ){ |
| 345 | sqlite3_bind_value(pTestCompare, 1, sqlite3_column_value(pS1,i1)); |
| 346 | sqlite3_bind_value(pTestCompare, 2, sqlite3_column_value(pS2,i2)); |
| 347 | x = sqlite3_step(pTestCompare)==SQLITE_ROW |
| 348 | && sqlite3_column_int(pTestCompare,0)!=0; |
| 349 | sqlite3_reset(pTestCompare); |
drh | 1ffb6be | 2022-10-12 18:40:25 +0000 | [diff] [blame] | 350 | break; |
| 351 | } |
| 352 | if( e1!=SQLITE_UTF8 ){ |
| 353 | int len1 = sqlite3_column_bytes16(pS1,i1); |
| 354 | const unsigned char *b1 = sqlite3_column_blob(pS1,i1); |
| 355 | int len2 = sqlite3_column_bytes16(pS2,i2); |
| 356 | const unsigned char *b2 = sqlite3_column_blob(pS2,i2); |
| 357 | if( len1!=len2 ){ |
| 358 | x = 0; |
| 359 | }else if( len1==0 ){ |
| 360 | x = 1; |
| 361 | }else{ |
| 362 | x = (b1!=0 && b2!=0 && memcmp(b1,b2,len1)==0); |
| 363 | } |
| 364 | break; |
| 365 | } |
| 366 | /* Fall through into the SQLITE_BLOB case */ |
drh | a1f79da | 2022-06-14 19:12:25 +0000 | [diff] [blame] | 367 | } |
| 368 | case SQLITE_BLOB: { |
drh | 6efabd6 | 2022-06-17 12:25:33 +0000 | [diff] [blame] | 369 | int len1 = sqlite3_column_bytes(pS1,i1); |
| 370 | const unsigned char *b1 = sqlite3_column_blob(pS1,i1); |
| 371 | int len2 = sqlite3_column_bytes(pS2,i2); |
| 372 | const unsigned char *b2 = sqlite3_column_blob(pS2,i2); |
drh | a1f79da | 2022-06-14 19:12:25 +0000 | [diff] [blame] | 373 | if( len1!=len2 ){ |
| 374 | x = 0; |
| 375 | }else if( len1==0 ){ |
| 376 | x = 1; |
| 377 | }else{ |
| 378 | x = (b1!=0 && b2!=0 && memcmp(b1,b2,len1)==0); |
| 379 | } |
| 380 | break; |
| 381 | } |
| 382 | } |
| 383 | return x; |
| 384 | } |
| 385 | |
| 386 | /* |
drh | 1ffb6be | 2022-10-12 18:40:25 +0000 | [diff] [blame] | 387 | ** Print binary data as hex |
| 388 | */ |
| 389 | static void printHex(const unsigned char *a, int n, int mx){ |
| 390 | int j; |
| 391 | for(j=0; j<mx && j<n; j++){ |
| 392 | printf("%02x", a[j]); |
| 393 | } |
| 394 | if( j<n ) printf("..."); |
| 395 | } |
| 396 | |
| 397 | /* |
drh | a1f79da | 2022-06-14 19:12:25 +0000 | [diff] [blame] | 398 | ** Print a single row from the prepared statement |
| 399 | */ |
| 400 | static void printRow(sqlite3_stmt *pStmt, int iRow){ |
drh | 1ffb6be | 2022-10-12 18:40:25 +0000 | [diff] [blame] | 401 | int i, n, nCol; |
| 402 | unsigned const char *data; |
drh | a1f79da | 2022-06-14 19:12:25 +0000 | [diff] [blame] | 403 | nCol = sqlite3_column_count(pStmt); |
| 404 | for(i=0; i<nCol; i++){ |
drh | 36f904f | 2022-06-17 15:11:31 +0000 | [diff] [blame] | 405 | printf("row%d.col%d = ", iRow, i); |
drh | a1f79da | 2022-06-14 19:12:25 +0000 | [diff] [blame] | 406 | switch( sqlite3_column_type(pStmt, i) ){ |
| 407 | case SQLITE_NULL: { |
| 408 | printf("NULL\n"); |
| 409 | break; |
| 410 | } |
| 411 | case SQLITE_INTEGER: { |
| 412 | printf("(integer) %lld\n", sqlite3_column_int64(pStmt, i)); |
| 413 | break; |
| 414 | } |
| 415 | case SQLITE_FLOAT: { |
| 416 | printf("(float) %f\n", sqlite3_column_double(pStmt, i)); |
| 417 | break; |
| 418 | } |
| 419 | case SQLITE_TEXT: { |
drh | 1ffb6be | 2022-10-12 18:40:25 +0000 | [diff] [blame] | 420 | switch( sqlite3_value_encoding(sqlite3_column_value(pStmt,i)) ){ |
| 421 | case SQLITE_UTF8: { |
| 422 | printf("(utf8) x'"); |
| 423 | n = sqlite3_column_bytes(pStmt, i); |
| 424 | data = sqlite3_column_blob(pStmt, i); |
| 425 | printHex(data, n, 35); |
| 426 | printf("'\n"); |
| 427 | break; |
| 428 | } |
| 429 | case SQLITE_UTF16BE: { |
| 430 | printf("(utf16be) x'"); |
| 431 | n = sqlite3_column_bytes16(pStmt, i); |
| 432 | data = sqlite3_column_blob(pStmt, i); |
| 433 | printHex(data, n, 35); |
| 434 | printf("'\n"); |
| 435 | break; |
| 436 | } |
| 437 | case SQLITE_UTF16LE: { |
| 438 | printf("(utf16le) x'"); |
| 439 | n = sqlite3_column_bytes16(pStmt, i); |
| 440 | data = sqlite3_column_blob(pStmt, i); |
| 441 | printHex(data, n, 35); |
| 442 | printf("'\n"); |
| 443 | break; |
| 444 | } |
| 445 | default: { |
| 446 | printf("Illegal return from sqlite3_value_encoding(): %d\n", |
| 447 | sqlite3_value_encoding(sqlite3_column_value(pStmt,i))); |
| 448 | abort(); |
| 449 | } |
| 450 | } |
drh | a1f79da | 2022-06-14 19:12:25 +0000 | [diff] [blame] | 451 | break; |
| 452 | } |
| 453 | case SQLITE_BLOB: { |
drh | 1ffb6be | 2022-10-12 18:40:25 +0000 | [diff] [blame] | 454 | n = sqlite3_column_bytes(pStmt, i); |
| 455 | data = sqlite3_column_blob(pStmt, i); |
drh | a1f79da | 2022-06-14 19:12:25 +0000 | [diff] [blame] | 456 | printf("(blob %d bytes) x'", n); |
drh | 1ffb6be | 2022-10-12 18:40:25 +0000 | [diff] [blame] | 457 | printHex(data, n, 35); |
drh | a1f79da | 2022-06-14 19:12:25 +0000 | [diff] [blame] | 458 | printf("'\n"); |
| 459 | break; |
| 460 | } |
| 461 | } |
| 462 | } |
| 463 | } |
| 464 | |
| 465 | /* |
| 466 | ** Report a failure of the invariant: The current output row of pOrig |
| 467 | ** does not appear in any row of the output from pTest. |
| 468 | */ |
| 469 | static void reportInvariantFailed( |
| 470 | sqlite3_stmt *pOrig, /* The original query */ |
| 471 | sqlite3_stmt *pTest, /* The alternative test query with a missing row */ |
| 472 | int iRow /* Row number in pOrig */ |
| 473 | ){ |
| 474 | int iTestRow = 0; |
| 475 | printf("Invariant check failed on row %d.\n", iRow); |
| 476 | printf("Original query --------------------------------------------------\n"); |
| 477 | printf("%s\n", sqlite3_expanded_sql(pOrig)); |
| 478 | printf("Alternative query -----------------------------------------------\n"); |
| 479 | printf("%s\n", sqlite3_expanded_sql(pTest)); |
| 480 | printf("Result row that is missing from the alternative -----------------\n"); |
| 481 | printRow(pOrig, iRow); |
| 482 | printf("Complete results from the alternative query ---------------------\n"); |
| 483 | sqlite3_reset(pTest); |
| 484 | while( sqlite3_step(pTest)==SQLITE_ROW ){ |
| 485 | iTestRow++; |
| 486 | printRow(pTest, iTestRow); |
| 487 | } |
| 488 | sqlite3_finalize(pTest); |
| 489 | abort(); |
| 490 | } |