| | @@ -1,8 +1,8 @@ |
| 1 | 1 | /****************************************************************************** |
| 2 | 2 | ** This file is an amalgamation of many separate C source files from SQLite |
| 3 | | -** version 3.27.0. By combining all the individual C code files into this |
| 3 | +** version 3.27.1. By combining all the individual C code files into this |
| 4 | 4 | ** single large file, the entire code can be compiled as a single translation |
| 5 | 5 | ** unit. This allows many compilers to do optimizations that would not be |
| 6 | 6 | ** possible if the files were compiled separately. Performance improvements |
| 7 | 7 | ** of 5% or more are commonly seen when SQLite is compiled as a single |
| 8 | 8 | ** translation unit. |
| | @@ -1160,13 +1160,13 @@ |
| 1160 | 1160 | ** |
| 1161 | 1161 | ** See also: [sqlite3_libversion()], |
| 1162 | 1162 | ** [sqlite3_libversion_number()], [sqlite3_sourceid()], |
| 1163 | 1163 | ** [sqlite_version()] and [sqlite_source_id()]. |
| 1164 | 1164 | */ |
| 1165 | | -#define SQLITE_VERSION "3.27.0" |
| 1166 | | -#define SQLITE_VERSION_NUMBER 3027000 |
| 1167 | | -#define SQLITE_SOURCE_ID "2019-01-27 02:45:32 9cf8ebd141aa2eb661d457624c76433bd9e4abfdef04aa52e28bc169172calt1" |
| 1165 | +#define SQLITE_VERSION "3.27.1" |
| 1166 | +#define SQLITE_VERSION_NUMBER 3027001 |
| 1167 | +#define SQLITE_SOURCE_ID "2019-02-08 13:17:39 0eca3dd3d38b31c92b49ca2d311128b74584714d9e7de895b1a6286ef959a1dd" |
| 1168 | 1168 | |
| 1169 | 1169 | /* |
| 1170 | 1170 | ** CAPI3REF: Run-Time Library Version Numbers |
| 1171 | 1171 | ** KEYWORDS: sqlite3_version sqlite3_sourceid |
| 1172 | 1172 | ** |
| | @@ -3019,10 +3019,21 @@ |
| 3019 | 3019 | ** from the database as records are returned in sorted order. The default |
| 3020 | 3020 | ** value for this option is to never use this optimization. Specifying a |
| 3021 | 3021 | ** negative value for this option restores the default behaviour. |
| 3022 | 3022 | ** This option is only available if SQLite is compiled with the |
| 3023 | 3023 | ** [SQLITE_ENABLE_SORTER_REFERENCES] compile-time option. |
| 3024 | +** |
| 3025 | +** [[SQLITE_CONFIG_MEMDB_MAXSIZE]] |
| 3026 | +** <dt>SQLITE_CONFIG_MEMDB_MAXSIZE |
| 3027 | +** <dd>The SQLITE_CONFIG_MEMDB_MAXSIZE option accepts a single parameter |
| 3028 | +** [sqlite3_int64] parameter which is the default maximum size for an in-memory |
| 3029 | +** database created using [sqlite3_deserialize()]. This default maximum |
| 3030 | +** size can be adjusted up or down for individual databases using the |
| 3031 | +** [SQLITE_FCNTL_SIZE_LIMIT] [sqlite3_file_control|file-control]. If this |
| 3032 | +** configuration setting is never used, then the default maximum is determined |
| 3033 | +** by the [SQLITE_MEMDB_DEFAULT_MAXSIZE] compile-time option. If that |
| 3034 | +** compile-time option is not set, then the default maximum is 1073741824. |
| 3024 | 3035 | ** </dl> |
| 3025 | 3036 | */ |
| 3026 | 3037 | #define SQLITE_CONFIG_SINGLETHREAD 1 /* nil */ |
| 3027 | 3038 | #define SQLITE_CONFIG_MULTITHREAD 2 /* nil */ |
| 3028 | 3039 | #define SQLITE_CONFIG_SERIALIZED 3 /* nil */ |
| | @@ -3049,10 +3060,11 @@ |
| 3049 | 3060 | #define SQLITE_CONFIG_PCACHE_HDRSZ 24 /* int *psz */ |
| 3050 | 3061 | #define SQLITE_CONFIG_PMASZ 25 /* unsigned int szPma */ |
| 3051 | 3062 | #define SQLITE_CONFIG_STMTJRNL_SPILL 26 /* int nByte */ |
| 3052 | 3063 | #define SQLITE_CONFIG_SMALL_MALLOC 27 /* boolean */ |
| 3053 | 3064 | #define SQLITE_CONFIG_SORTERREF_SIZE 28 /* int nByte */ |
| 3065 | +#define SQLITE_CONFIG_MEMDB_MAXSIZE 29 /* sqlite3_int64 */ |
| 3054 | 3066 | |
| 3055 | 3067 | /* |
| 3056 | 3068 | ** CAPI3REF: Database Connection Configuration Options |
| 3057 | 3069 | ** |
| 3058 | 3070 | ** These constants are the available integer configuration options that |
| | @@ -4454,10 +4466,12 @@ |
| 4454 | 4466 | ** If F is a NULL pointer, then sqlite3_uri_parameter(F,P) returns NULL and |
| 4455 | 4467 | ** sqlite3_uri_boolean(F,P,B) returns B. If F is not a NULL pointer and |
| 4456 | 4468 | ** is not a database file pathname pointer that SQLite passed into the xOpen |
| 4457 | 4469 | ** VFS method, then the behavior of this routine is undefined and probably |
| 4458 | 4470 | ** undesirable. |
| 4471 | +** |
| 4472 | +** See the [URI filename] documentation for additional information. |
| 4459 | 4473 | */ |
| 4460 | 4474 | SQLITE_API const char *sqlite3_uri_parameter(const char *zFilename, const char *zParam); |
| 4461 | 4475 | SQLITE_API int sqlite3_uri_boolean(const char *zFile, const char *zParam, int bDefault); |
| 4462 | 4476 | SQLITE_API sqlite3_int64 sqlite3_uri_int64(const char*, const char*, sqlite3_int64); |
| 4463 | 4477 | |
| | @@ -14544,10 +14558,11 @@ |
| 14544 | 14558 | SQLITE_PRIVATE i64 sqlite3BtreeOffset(BtCursor*); |
| 14545 | 14559 | #endif |
| 14546 | 14560 | SQLITE_PRIVATE int sqlite3BtreePayload(BtCursor*, u32 offset, u32 amt, void*); |
| 14547 | 14561 | SQLITE_PRIVATE const void *sqlite3BtreePayloadFetch(BtCursor*, u32 *pAmt); |
| 14548 | 14562 | SQLITE_PRIVATE u32 sqlite3BtreePayloadSize(BtCursor*); |
| 14563 | +SQLITE_PRIVATE sqlite3_int64 sqlite3BtreeMaxRecordSize(BtCursor*); |
| 14549 | 14564 | |
| 14550 | 14565 | SQLITE_PRIVATE char *sqlite3BtreeIntegrityCheck(Btree*, int *aRoot, int nRoot, int, int*); |
| 14551 | 14566 | SQLITE_PRIVATE struct Pager *sqlite3BtreePager(Btree*); |
| 14552 | 14567 | SQLITE_PRIVATE i64 sqlite3BtreeRowCountEst(BtCursor*); |
| 14553 | 14568 | |
| | @@ -16433,10 +16448,11 @@ |
| 16433 | 16448 | #define SQLITE_SqlTrace HI(0x0001) /* Debug print SQL as it executes */ |
| 16434 | 16449 | #define SQLITE_VdbeListing HI(0x0002) /* Debug listings of VDBE progs */ |
| 16435 | 16450 | #define SQLITE_VdbeTrace HI(0x0004) /* True to trace VDBE execution */ |
| 16436 | 16451 | #define SQLITE_VdbeAddopTrace HI(0x0008) /* Trace sqlite3VdbeAddOp() calls */ |
| 16437 | 16452 | #define SQLITE_VdbeEQP HI(0x0010) /* Debug EXPLAIN QUERY PLAN */ |
| 16453 | +#define SQLITE_ParserTrace HI(0x0020) /* PRAGMA parser_trace=ON */ |
| 16438 | 16454 | #endif |
| 16439 | 16455 | |
| 16440 | 16456 | /* |
| 16441 | 16457 | ** Allowed values for sqlite3.mDbFlags |
| 16442 | 16458 | */ |
| | @@ -18305,10 +18321,13 @@ |
| 18305 | 18321 | ** operation. Set the callback using SQLITE_TESTCTRL_VDBE_COVERAGE. |
| 18306 | 18322 | */ |
| 18307 | 18323 | void (*xVdbeBranch)(void*,unsigned iSrcLine,u8 eThis,u8 eMx); /* Callback */ |
| 18308 | 18324 | void *pVdbeBranchArg; /* 1st argument */ |
| 18309 | 18325 | #endif |
| 18326 | +#ifdef SQLITE_ENABLE_DESERIALIZE |
| 18327 | + sqlite3_int64 mxMemdbSize; /* Default max memdb size */ |
| 18328 | +#endif |
| 18310 | 18329 | #ifndef SQLITE_UNTESTABLE |
| 18311 | 18330 | int (*xTestCallback)(int); /* Invoked by sqlite3FaultSim() */ |
| 18312 | 18331 | #endif |
| 18313 | 18332 | int bLocaltimeFault; /* True to fail localtime() calls */ |
| 18314 | 18333 | int bInternalFunctions; /* Internal SQL functions are visible */ |
| | @@ -18722,10 +18741,11 @@ |
| 18722 | 18741 | SQLITE_PRIVATE void sqlite3ExprListSetSortOrder(ExprList*,int); |
| 18723 | 18742 | SQLITE_PRIVATE void sqlite3ExprListSetName(Parse*,ExprList*,Token*,int); |
| 18724 | 18743 | SQLITE_PRIVATE void sqlite3ExprListSetSpan(Parse*,ExprList*,const char*,const char*); |
| 18725 | 18744 | SQLITE_PRIVATE void sqlite3ExprListDelete(sqlite3*, ExprList*); |
| 18726 | 18745 | SQLITE_PRIVATE u32 sqlite3ExprListFlags(const ExprList*); |
| 18746 | +SQLITE_PRIVATE int sqlite3IndexHasDuplicateRootPage(Index*); |
| 18727 | 18747 | SQLITE_PRIVATE int sqlite3Init(sqlite3*, char**); |
| 18728 | 18748 | SQLITE_PRIVATE int sqlite3InitCallback(void*, int, char**, char**); |
| 18729 | 18749 | SQLITE_PRIVATE int sqlite3InitOne(sqlite3*, int, char**, u32); |
| 18730 | 18750 | SQLITE_PRIVATE void sqlite3Pragma(Parse*,Token*,Token*,Token*,int); |
| 18731 | 18751 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
| | @@ -18755,10 +18775,15 @@ |
| 18755 | 18775 | SQLITE_PRIVATE void sqlite3AddDefaultValue(Parse*,Expr*,const char*,const char*); |
| 18756 | 18776 | SQLITE_PRIVATE void sqlite3AddCollateType(Parse*, Token*); |
| 18757 | 18777 | SQLITE_PRIVATE void sqlite3EndTable(Parse*,Token*,Token*,u8,Select*); |
| 18758 | 18778 | SQLITE_PRIVATE int sqlite3ParseUri(const char*,const char*,unsigned int*, |
| 18759 | 18779 | sqlite3_vfs**,char**,char **); |
| 18780 | +#ifdef SQLITE_HAS_CODEC |
| 18781 | +SQLITE_PRIVATE int sqlite3CodecQueryParameters(sqlite3*,const char*,const char*); |
| 18782 | +#else |
| 18783 | +# define sqlite3CodecQueryParameters(A,B,C) 0 |
| 18784 | +#endif |
| 18760 | 18785 | SQLITE_PRIVATE Btree *sqlite3DbNameToBtree(sqlite3*,const char*); |
| 18761 | 18786 | |
| 18762 | 18787 | #ifdef SQLITE_UNTESTABLE |
| 18763 | 18788 | # define sqlite3FaultSim(X) SQLITE_OK |
| 18764 | 18789 | #else |
| | @@ -19716,10 +19741,17 @@ |
| 19716 | 19741 | #ifndef SQLITE_DEFAULT_LOOKASIDE |
| 19717 | 19742 | # define SQLITE_DEFAULT_LOOKASIDE 1200,100 |
| 19718 | 19743 | #endif |
| 19719 | 19744 | |
| 19720 | 19745 | |
| 19746 | +/* The default maximum size of an in-memory database created using |
| 19747 | +** sqlite3_deserialize() |
| 19748 | +*/ |
| 19749 | +#ifndef SQLITE_MEMDB_DEFAULT_MAXSIZE |
| 19750 | +# define SQLITE_MEMDB_DEFAULT_MAXSIZE 1073741824 |
| 19751 | +#endif |
| 19752 | + |
| 19721 | 19753 | /* |
| 19722 | 19754 | ** The following singleton contains the global configuration for |
| 19723 | 19755 | ** the SQLite library. |
| 19724 | 19756 | */ |
| 19725 | 19757 | SQLITE_PRIVATE SQLITE_WSD struct Sqlite3Config sqlite3Config = { |
| | @@ -19763,17 +19795,20 @@ |
| 19763 | 19795 | #endif |
| 19764 | 19796 | #ifdef SQLITE_VDBE_COVERAGE |
| 19765 | 19797 | 0, /* xVdbeBranch */ |
| 19766 | 19798 | 0, /* pVbeBranchArg */ |
| 19767 | 19799 | #endif |
| 19800 | +#ifdef SQLITE_ENABLE_DESERIALIZE |
| 19801 | + SQLITE_MEMDB_DEFAULT_MAXSIZE, /* mxMemdbSize */ |
| 19802 | +#endif |
| 19768 | 19803 | #ifndef SQLITE_UNTESTABLE |
| 19769 | 19804 | 0, /* xTestCallback */ |
| 19770 | 19805 | #endif |
| 19771 | 19806 | 0, /* bLocaltimeFault */ |
| 19772 | 19807 | 0, /* bInternalFunctions */ |
| 19773 | 19808 | 0x7ffffffe, /* iOnceResetThreshold */ |
| 19774 | | - SQLITE_DEFAULT_SORTERREF_SIZE /* szSorterRef */ |
| 19809 | + SQLITE_DEFAULT_SORTERREF_SIZE, /* szSorterRef */ |
| 19775 | 19810 | }; |
| 19776 | 19811 | |
| 19777 | 19812 | /* |
| 19778 | 19813 | ** Hash table for global functions - functions common to all |
| 19779 | 19814 | ** database connections. After initialization, this table is |
| | @@ -27296,10 +27331,31 @@ |
| 27296 | 27331 | static char *getTextArg(PrintfArguments *p){ |
| 27297 | 27332 | if( p->nArg<=p->nUsed ) return 0; |
| 27298 | 27333 | return (char*)sqlite3_value_text(p->apArg[p->nUsed++]); |
| 27299 | 27334 | } |
| 27300 | 27335 | |
| 27336 | +/* |
| 27337 | +** Allocate memory for a temporary buffer needed for printf rendering. |
| 27338 | +** |
| 27339 | +** If the requested size of the temp buffer is larger than the size |
| 27340 | +** of the output buffer in pAccum, then cause an SQLITE_TOOBIG error. |
| 27341 | +** Do the size check before the memory allocation to prevent rogue |
| 27342 | +** SQL from requesting large allocations using the precision or width |
| 27343 | +** field of the printf() function. |
| 27344 | +*/ |
| 27345 | +static char *printfTempBuf(sqlite3_str *pAccum, sqlite3_int64 n){ |
| 27346 | + char *z; |
| 27347 | + if( n>pAccum->nAlloc && n>pAccum->mxAlloc ){ |
| 27348 | + setStrAccumError(pAccum, SQLITE_TOOBIG); |
| 27349 | + return 0; |
| 27350 | + } |
| 27351 | + z = sqlite3DbMallocRaw(pAccum->db, n); |
| 27352 | + if( z==0 ){ |
| 27353 | + setStrAccumError(pAccum, SQLITE_NOMEM); |
| 27354 | + } |
| 27355 | + return z; |
| 27356 | +} |
| 27301 | 27357 | |
| 27302 | 27358 | /* |
| 27303 | 27359 | ** On machines with a small stack size, you can redefine the |
| 27304 | 27360 | ** SQLITE_PRINT_BUF_SIZE to be something smaller, if desired. |
| 27305 | 27361 | */ |
| | @@ -27378,10 +27434,13 @@ |
| 27378 | 27434 | } |
| 27379 | 27435 | /* Find out what flags are present */ |
| 27380 | 27436 | flag_leftjustify = flag_prefix = cThousand = |
| 27381 | 27437 | flag_alternateform = flag_altform2 = flag_zeropad = 0; |
| 27382 | 27438 | done = 0; |
| 27439 | + width = 0; |
| 27440 | + flag_long = 0; |
| 27441 | + precision = -1; |
| 27383 | 27442 | do{ |
| 27384 | 27443 | switch( c ){ |
| 27385 | 27444 | case '-': flag_leftjustify = 1; break; |
| 27386 | 27445 | case '+': flag_prefix = '+'; break; |
| 27387 | 27446 | case ' ': flag_prefix = ' '; break; |
| | @@ -27388,84 +27447,97 @@ |
| 27388 | 27447 | case '#': flag_alternateform = 1; break; |
| 27389 | 27448 | case '!': flag_altform2 = 1; break; |
| 27390 | 27449 | case '0': flag_zeropad = 1; break; |
| 27391 | 27450 | case ',': cThousand = ','; break; |
| 27392 | 27451 | default: done = 1; break; |
| 27452 | + case 'l': { |
| 27453 | + flag_long = 1; |
| 27454 | + c = *++fmt; |
| 27455 | + if( c=='l' ){ |
| 27456 | + c = *++fmt; |
| 27457 | + flag_long = 2; |
| 27458 | + } |
| 27459 | + done = 1; |
| 27460 | + break; |
| 27461 | + } |
| 27462 | + case '1': case '2': case '3': case '4': case '5': |
| 27463 | + case '6': case '7': case '8': case '9': { |
| 27464 | + unsigned wx = c - '0'; |
| 27465 | + while( (c = *++fmt)>='0' && c<='9' ){ |
| 27466 | + wx = wx*10 + c - '0'; |
| 27467 | + } |
| 27468 | + testcase( wx>0x7fffffff ); |
| 27469 | + width = wx & 0x7fffffff; |
| 27470 | +#ifdef SQLITE_PRINTF_PRECISION_LIMIT |
| 27471 | + if( width>SQLITE_PRINTF_PRECISION_LIMIT ){ |
| 27472 | + width = SQLITE_PRINTF_PRECISION_LIMIT; |
| 27473 | + } |
| 27474 | +#endif |
| 27475 | + if( c!='.' && c!='l' ){ |
| 27476 | + done = 1; |
| 27477 | + }else{ |
| 27478 | + fmt--; |
| 27479 | + } |
| 27480 | + break; |
| 27481 | + } |
| 27482 | + case '*': { |
| 27483 | + if( bArgList ){ |
| 27484 | + width = (int)getIntArg(pArgList); |
| 27485 | + }else{ |
| 27486 | + width = va_arg(ap,int); |
| 27487 | + } |
| 27488 | + if( width<0 ){ |
| 27489 | + flag_leftjustify = 1; |
| 27490 | + width = width >= -2147483647 ? -width : 0; |
| 27491 | + } |
| 27492 | +#ifdef SQLITE_PRINTF_PRECISION_LIMIT |
| 27493 | + if( width>SQLITE_PRINTF_PRECISION_LIMIT ){ |
| 27494 | + width = SQLITE_PRINTF_PRECISION_LIMIT; |
| 27495 | + } |
| 27496 | +#endif |
| 27497 | + if( (c = fmt[1])!='.' && c!='l' ){ |
| 27498 | + c = *++fmt; |
| 27499 | + done = 1; |
| 27500 | + } |
| 27501 | + break; |
| 27502 | + } |
| 27503 | + case '.': { |
| 27504 | + c = *++fmt; |
| 27505 | + if( c=='*' ){ |
| 27506 | + if( bArgList ){ |
| 27507 | + precision = (int)getIntArg(pArgList); |
| 27508 | + }else{ |
| 27509 | + precision = va_arg(ap,int); |
| 27510 | + } |
| 27511 | + if( precision<0 ){ |
| 27512 | + precision = precision >= -2147483647 ? -precision : -1; |
| 27513 | + } |
| 27514 | + c = *++fmt; |
| 27515 | + }else{ |
| 27516 | + unsigned px = 0; |
| 27517 | + while( c>='0' && c<='9' ){ |
| 27518 | + px = px*10 + c - '0'; |
| 27519 | + c = *++fmt; |
| 27520 | + } |
| 27521 | + testcase( px>0x7fffffff ); |
| 27522 | + precision = px & 0x7fffffff; |
| 27523 | + } |
| 27524 | +#ifdef SQLITE_PRINTF_PRECISION_LIMIT |
| 27525 | + if( precision>SQLITE_PRINTF_PRECISION_LIMIT ){ |
| 27526 | + precision = SQLITE_PRINTF_PRECISION_LIMIT; |
| 27527 | + } |
| 27528 | +#endif |
| 27529 | + if( c=='l' ){ |
| 27530 | + --fmt; |
| 27531 | + }else{ |
| 27532 | + done = 1; |
| 27533 | + } |
| 27534 | + break; |
| 27535 | + } |
| 27393 | 27536 | } |
| 27394 | 27537 | }while( !done && (c=(*++fmt))!=0 ); |
| 27395 | | - /* Get the field width */ |
| 27396 | | - if( c=='*' ){ |
| 27397 | | - if( bArgList ){ |
| 27398 | | - width = (int)getIntArg(pArgList); |
| 27399 | | - }else{ |
| 27400 | | - width = va_arg(ap,int); |
| 27401 | | - } |
| 27402 | | - if( width<0 ){ |
| 27403 | | - flag_leftjustify = 1; |
| 27404 | | - width = width >= -2147483647 ? -width : 0; |
| 27405 | | - } |
| 27406 | | - c = *++fmt; |
| 27407 | | - }else{ |
| 27408 | | - unsigned wx = 0; |
| 27409 | | - while( c>='0' && c<='9' ){ |
| 27410 | | - wx = wx*10 + c - '0'; |
| 27411 | | - c = *++fmt; |
| 27412 | | - } |
| 27413 | | - testcase( wx>0x7fffffff ); |
| 27414 | | - width = wx & 0x7fffffff; |
| 27415 | | - } |
| 27416 | | - assert( width>=0 ); |
| 27417 | | -#ifdef SQLITE_PRINTF_PRECISION_LIMIT |
| 27418 | | - if( width>SQLITE_PRINTF_PRECISION_LIMIT ){ |
| 27419 | | - width = SQLITE_PRINTF_PRECISION_LIMIT; |
| 27420 | | - } |
| 27421 | | -#endif |
| 27422 | | - |
| 27423 | | - /* Get the precision */ |
| 27424 | | - if( c=='.' ){ |
| 27425 | | - c = *++fmt; |
| 27426 | | - if( c=='*' ){ |
| 27427 | | - if( bArgList ){ |
| 27428 | | - precision = (int)getIntArg(pArgList); |
| 27429 | | - }else{ |
| 27430 | | - precision = va_arg(ap,int); |
| 27431 | | - } |
| 27432 | | - c = *++fmt; |
| 27433 | | - if( precision<0 ){ |
| 27434 | | - precision = precision >= -2147483647 ? -precision : -1; |
| 27435 | | - } |
| 27436 | | - }else{ |
| 27437 | | - unsigned px = 0; |
| 27438 | | - while( c>='0' && c<='9' ){ |
| 27439 | | - px = px*10 + c - '0'; |
| 27440 | | - c = *++fmt; |
| 27441 | | - } |
| 27442 | | - testcase( px>0x7fffffff ); |
| 27443 | | - precision = px & 0x7fffffff; |
| 27444 | | - } |
| 27445 | | - }else{ |
| 27446 | | - precision = -1; |
| 27447 | | - } |
| 27448 | | - assert( precision>=(-1) ); |
| 27449 | | -#ifdef SQLITE_PRINTF_PRECISION_LIMIT |
| 27450 | | - if( precision>SQLITE_PRINTF_PRECISION_LIMIT ){ |
| 27451 | | - precision = SQLITE_PRINTF_PRECISION_LIMIT; |
| 27452 | | - } |
| 27453 | | -#endif |
| 27454 | | - |
| 27455 | | - |
| 27456 | | - /* Get the conversion type modifier */ |
| 27457 | | - if( c=='l' ){ |
| 27458 | | - flag_long = 1; |
| 27459 | | - c = *++fmt; |
| 27460 | | - if( c=='l' ){ |
| 27461 | | - flag_long = 2; |
| 27462 | | - c = *++fmt; |
| 27463 | | - } |
| 27464 | | - }else{ |
| 27465 | | - flag_long = 0; |
| 27466 | | - } |
| 27538 | + |
| 27467 | 27539 | /* Fetch the info entry for the field */ |
| 27468 | 27540 | infop = &fmtinfo[0]; |
| 27469 | 27541 | xtype = etINVALID; |
| 27470 | 27542 | for(idx=0; idx<ArraySize(fmtinfo); idx++){ |
| 27471 | 27543 | if( c==fmtinfo[idx].fmttype ){ |
| | @@ -27546,16 +27618,15 @@ |
| 27546 | 27618 | } |
| 27547 | 27619 | if( precision<etBUFSIZE-10-etBUFSIZE/3 ){ |
| 27548 | 27620 | nOut = etBUFSIZE; |
| 27549 | 27621 | zOut = buf; |
| 27550 | 27622 | }else{ |
| 27551 | | - u64 n = (u64)precision + 10 + precision/3; |
| 27552 | | - zOut = zExtra = sqlite3Malloc( n ); |
| 27553 | | - if( zOut==0 ){ |
| 27554 | | - setStrAccumError(pAccum, SQLITE_NOMEM); |
| 27555 | | - return; |
| 27556 | | - } |
| 27623 | + u64 n; |
| 27624 | + n = (u64)precision + 10; |
| 27625 | + if( cThousand ) n += precision/3; |
| 27626 | + zOut = zExtra = printfTempBuf(pAccum, n); |
| 27627 | + if( zOut==0 ) return; |
| 27557 | 27628 | nOut = (int)n; |
| 27558 | 27629 | } |
| 27559 | 27630 | bufpt = &zOut[nOut-1]; |
| 27560 | 27631 | if( xtype==etORDINAL ){ |
| 27561 | 27632 | static const char zOrd[] = "thstndrd"; |
| | @@ -27670,16 +27741,16 @@ |
| 27670 | 27741 | if( xtype==etEXP ){ |
| 27671 | 27742 | e2 = 0; |
| 27672 | 27743 | }else{ |
| 27673 | 27744 | e2 = exp; |
| 27674 | 27745 | } |
| 27675 | | - if( MAX(e2,0)+(i64)precision+(i64)width > etBUFSIZE - 15 ){ |
| 27676 | | - bufpt = zExtra |
| 27677 | | - = sqlite3Malloc( MAX(e2,0)+(i64)precision+(i64)width+15 ); |
| 27678 | | - if( bufpt==0 ){ |
| 27679 | | - setStrAccumError(pAccum, SQLITE_NOMEM); |
| 27680 | | - return; |
| 27746 | + { |
| 27747 | + i64 szBufNeeded; /* Size of a temporary buffer needed */ |
| 27748 | + szBufNeeded = MAX(e2,0)+(i64)precision+(i64)width+15; |
| 27749 | + if( szBufNeeded > etBUFSIZE ){ |
| 27750 | + bufpt = zExtra = printfTempBuf(pAccum, szBufNeeded); |
| 27751 | + if( bufpt==0 ) return; |
| 27681 | 27752 | } |
| 27682 | 27753 | } |
| 27683 | 27754 | zOut = bufpt; |
| 27684 | 27755 | nsd = 16 + flag_altform2*10; |
| 27685 | 27756 | flag_dp = (precision>0 ?1:0) | flag_alternateform | flag_altform2; |
| | @@ -27899,15 +27970,12 @@ |
| 27899 | 27970 | } |
| 27900 | 27971 | } |
| 27901 | 27972 | needQuote = !isnull && xtype==etSQLESCAPE2; |
| 27902 | 27973 | n += i + 3; |
| 27903 | 27974 | if( n>etBUFSIZE ){ |
| 27904 | | - bufpt = zExtra = sqlite3Malloc( n ); |
| 27905 | | - if( bufpt==0 ){ |
| 27906 | | - setStrAccumError(pAccum, SQLITE_NOMEM); |
| 27907 | | - return; |
| 27908 | | - } |
| 27975 | + bufpt = zExtra = printfTempBuf(pAccum, n); |
| 27976 | + if( bufpt==0 ) return; |
| 27909 | 27977 | }else{ |
| 27910 | 27978 | bufpt = buf; |
| 27911 | 27979 | } |
| 27912 | 27980 | j = 0; |
| 27913 | 27981 | if( needQuote ) bufpt[j++] = q; |
| | @@ -46578,15 +46646,10 @@ |
| 46578 | 46646 | int nMmap; /* Number of memory mapped pages */ |
| 46579 | 46647 | unsigned mFlags; /* Flags */ |
| 46580 | 46648 | int eLock; /* Most recent lock against this file */ |
| 46581 | 46649 | }; |
| 46582 | 46650 | |
| 46583 | | -/* The default maximum size of an in-memory database */ |
| 46584 | | -#ifndef SQLITE_MEMDB_DEFAULT_MAXSIZE |
| 46585 | | -# define SQLITE_MEMDB_DEFAULT_MAXSIZE 1073741824 |
| 46586 | | -#endif |
| 46587 | | - |
| 46588 | 46651 | /* |
| 46589 | 46652 | ** Methods for MemFile |
| 46590 | 46653 | */ |
| 46591 | 46654 | static int memdbClose(sqlite3_file*); |
| 46592 | 46655 | static int memdbRead(sqlite3_file*, void*, int iAmt, sqlite3_int64 iOfst); |
| | @@ -46847,11 +46910,10 @@ |
| 46847 | 46910 | int iAmt, |
| 46848 | 46911 | void **pp |
| 46849 | 46912 | ){ |
| 46850 | 46913 | MemFile *p = (MemFile *)pFile; |
| 46851 | 46914 | if( iOfst+iAmt>p->sz ){ |
| 46852 | | - assert( CORRUPT_DB ); |
| 46853 | 46915 | *pp = 0; |
| 46854 | 46916 | }else{ |
| 46855 | 46917 | p->nMmap++; |
| 46856 | 46918 | *pp = (void*)(p->aData + iOfst); |
| 46857 | 46919 | } |
| | @@ -46882,11 +46944,11 @@ |
| 46882 | 46944 | memset(p, 0, sizeof(*p)); |
| 46883 | 46945 | p->mFlags = SQLITE_DESERIALIZE_RESIZEABLE | SQLITE_DESERIALIZE_FREEONCLOSE; |
| 46884 | 46946 | assert( pOutFlags!=0 ); /* True because flags==SQLITE_OPEN_MAIN_DB */ |
| 46885 | 46947 | *pOutFlags = flags | SQLITE_OPEN_MEMORY; |
| 46886 | 46948 | p->base.pMethods = &memdb_io_methods; |
| 46887 | | - p->szMax = SQLITE_MEMDB_DEFAULT_MAXSIZE; |
| 46949 | + p->szMax = sqlite3GlobalConfig.mxMemdbSize; |
| 46888 | 46950 | return SQLITE_OK; |
| 46889 | 46951 | } |
| 46890 | 46952 | |
| 46891 | 46953 | #if 0 /* Only used to delete rollback journals, master journals, and WAL |
| 46892 | 46954 | ** files, none of which exist in memdb. So this routine is never used */ |
| | @@ -47134,12 +47196,12 @@ |
| 47134 | 47196 | }else{ |
| 47135 | 47197 | p->aData = pData; |
| 47136 | 47198 | p->sz = szDb; |
| 47137 | 47199 | p->szAlloc = szBuf; |
| 47138 | 47200 | p->szMax = szBuf; |
| 47139 | | - if( p->szMax<SQLITE_MEMDB_DEFAULT_MAXSIZE ){ |
| 47140 | | - p->szMax = SQLITE_MEMDB_DEFAULT_MAXSIZE; |
| 47201 | + if( p->szMax<sqlite3GlobalConfig.mxMemdbSize ){ |
| 47202 | + p->szMax = sqlite3GlobalConfig.mxMemdbSize; |
| 47141 | 47203 | } |
| 47142 | 47204 | p->mFlags = mFlags; |
| 47143 | 47205 | rc = SQLITE_OK; |
| 47144 | 47206 | } |
| 47145 | 47207 | |
| | @@ -48557,16 +48619,26 @@ |
| 48557 | 48619 | /* |
| 48558 | 48620 | ** Each cache entry is represented by an instance of the following |
| 48559 | 48621 | ** structure. Unless SQLITE_PCACHE_SEPARATE_HEADER is defined, a buffer of |
| 48560 | 48622 | ** PgHdr1.pCache->szPage bytes is allocated directly before this structure |
| 48561 | 48623 | ** in memory. |
| 48624 | +** |
| 48625 | +** Note: Variables isBulkLocal and isAnchor were once type "u8". That works, |
| 48626 | +** but causes a 2-byte gap in the structure for most architectures (since |
| 48627 | +** pointers must be either 4 or 8-byte aligned). As this structure is located |
| 48628 | +** in memory directly after the associated page data, if the database is |
| 48629 | +** corrupt, code at the b-tree layer may overread the page buffer and |
| 48630 | +** read part of this structure before the corruption is detected. This |
| 48631 | +** can cause a valgrind error if the unitialized gap is accessed. Using u16 |
| 48632 | +** ensures there is no such gap, and therefore no bytes of unitialized memory |
| 48633 | +** in the structure. |
| 48562 | 48634 | */ |
| 48563 | 48635 | struct PgHdr1 { |
| 48564 | 48636 | sqlite3_pcache_page page; /* Base class. Must be first. pBuf & pExtra */ |
| 48565 | 48637 | unsigned int iKey; /* Key value (page number) */ |
| 48566 | | - u8 isBulkLocal; /* This page from bulk local storage */ |
| 48567 | | - u8 isAnchor; /* This is the PGroup.lru element */ |
| 48638 | + u16 isBulkLocal; /* This page from bulk local storage */ |
| 48639 | + u16 isAnchor; /* This is the PGroup.lru element */ |
| 48568 | 48640 | PgHdr1 *pNext; /* Next in hash table chain */ |
| 48569 | 48641 | PCache1 *pCache; /* Cache that currently owns this page */ |
| 48570 | 48642 | PgHdr1 *pLruNext; /* Next in LRU list of unpinned pages */ |
| 48571 | 48643 | PgHdr1 *pLruPrev; /* Previous in LRU list of unpinned pages */ |
| 48572 | 48644 | /* NB: pLruPrev is only valid if pLruNext!=0 */ |
| | @@ -48768,10 +48840,11 @@ |
| 48768 | 48840 | pX->page.pBuf = zBulk; |
| 48769 | 48841 | pX->page.pExtra = &pX[1]; |
| 48770 | 48842 | pX->isBulkLocal = 1; |
| 48771 | 48843 | pX->isAnchor = 0; |
| 48772 | 48844 | pX->pNext = pCache->pFree; |
| 48845 | + pX->pLruPrev = 0; /* Initializing this saves a valgrind error */ |
| 48773 | 48846 | pCache->pFree = pX; |
| 48774 | 48847 | zBulk += pCache->szAlloc; |
| 48775 | 48848 | }while( --nBulk ); |
| 48776 | 48849 | } |
| 48777 | 48850 | return pCache->pFree!=0; |
| | @@ -62473,13 +62546,20 @@ |
| 62473 | 62546 | ** |
| 62474 | 62547 | ** Fields in this structure are accessed under the BtShared.mutex |
| 62475 | 62548 | ** found at self->pBt->mutex. |
| 62476 | 62549 | ** |
| 62477 | 62550 | ** skipNext meaning: |
| 62478 | | -** eState==SKIPNEXT && skipNext>0: Next sqlite3BtreeNext() is no-op. |
| 62479 | | -** eState==SKIPNEXT && skipNext<0: Next sqlite3BtreePrevious() is no-op. |
| 62480 | | -** eState==FAULT: Cursor fault with skipNext as error code. |
| 62551 | +** The meaning of skipNext depends on the value of eState: |
| 62552 | +** |
| 62553 | +** eState Meaning of skipNext |
| 62554 | +** VALID skipNext is meaningless and is ignored |
| 62555 | +** INVALID skipNext is meaningless and is ignored |
| 62556 | +** SKIPNEXT sqlite3BtreeNext() is a no-op if skipNext>0 and |
| 62557 | +** sqlite3BtreePrevious() is no-op if skipNext<0. |
| 62558 | +** REQUIRESEEK restoreCursorPosition() restores the cursor to |
| 62559 | +** eState=SKIPNEXT if skipNext!=0 |
| 62560 | +** FAULT skipNext holds the cursor fault error code. |
| 62481 | 62561 | */ |
| 62482 | 62562 | struct BtCursor { |
| 62483 | 62563 | u8 eState; /* One of the CURSOR_XXX constants (see below) */ |
| 62484 | 62564 | u8 curFlags; /* zero or more BTCF_* flags defined below */ |
| 62485 | 62565 | u8 curPagerFlags; /* Flags to send to sqlite3PagerGet() */ |
| | @@ -63824,11 +63904,11 @@ |
| 63824 | 63904 | rc = btreeMoveto(pCur, pCur->pKey, pCur->nKey, 0, &skipNext); |
| 63825 | 63905 | if( rc==SQLITE_OK ){ |
| 63826 | 63906 | sqlite3_free(pCur->pKey); |
| 63827 | 63907 | pCur->pKey = 0; |
| 63828 | 63908 | assert( pCur->eState==CURSOR_VALID || pCur->eState==CURSOR_INVALID ); |
| 63829 | | - pCur->skipNext |= skipNext; |
| 63909 | + if( skipNext ) pCur->skipNext = skipNext; |
| 63830 | 63910 | if( pCur->skipNext && pCur->eState==CURSOR_VALID ){ |
| 63831 | 63911 | pCur->eState = CURSOR_SKIPNEXT; |
| 63832 | 63912 | } |
| 63833 | 63913 | } |
| 63834 | 63914 | return rc; |
| | @@ -63894,11 +63974,10 @@ |
| 63894 | 63974 | return rc; |
| 63895 | 63975 | } |
| 63896 | 63976 | if( pCur->eState!=CURSOR_VALID ){ |
| 63897 | 63977 | *pDifferentRow = 1; |
| 63898 | 63978 | }else{ |
| 63899 | | - assert( pCur->skipNext==0 ); |
| 63900 | 63979 | *pDifferentRow = 0; |
| 63901 | 63980 | } |
| 63902 | 63981 | return SQLITE_OK; |
| 63903 | 63982 | } |
| 63904 | 63983 | |
| | @@ -67498,10 +67577,29 @@ |
| 67498 | 67577 | assert( cursorHoldsMutex(pCur) ); |
| 67499 | 67578 | assert( pCur->eState==CURSOR_VALID ); |
| 67500 | 67579 | getCellInfo(pCur); |
| 67501 | 67580 | return pCur->info.nPayload; |
| 67502 | 67581 | } |
| 67582 | + |
| 67583 | +/* |
| 67584 | +** Return an upper bound on the size of any record for the table |
| 67585 | +** that the cursor is pointing into. |
| 67586 | +** |
| 67587 | +** This is an optimization. Everything will still work if this |
| 67588 | +** routine always returns 2147483647 (which is the largest record |
| 67589 | +** that SQLite can handle) or more. But returning a smaller value might |
| 67590 | +** prevent large memory allocations when trying to interpret a |
| 67591 | +** corrupt datrabase. |
| 67592 | +** |
| 67593 | +** The current implementation merely returns the size of the underlying |
| 67594 | +** database file. |
| 67595 | +*/ |
| 67596 | +SQLITE_PRIVATE sqlite3_int64 sqlite3BtreeMaxRecordSize(BtCursor *pCur){ |
| 67597 | + assert( cursorHoldsMutex(pCur) ); |
| 67598 | + assert( pCur->eState==CURSOR_VALID ); |
| 67599 | + return pCur->pBt->pageSize * (sqlite3_int64)pCur->pBt->nPage; |
| 67600 | +} |
| 67503 | 67601 | |
| 67504 | 67602 | /* |
| 67505 | 67603 | ** Given the page number of an overflow page in the database (parameter |
| 67506 | 67604 | ** ovfl), this function finds the page number of the next page in the |
| 67507 | 67605 | ** linked list of overflow pages. If possible, it uses the auto-vacuum |
| | @@ -68313,11 +68411,11 @@ |
| 68313 | 68411 | } |
| 68314 | 68412 | /* If the requested key is one more than the previous key, then |
| 68315 | 68413 | ** try to get there using sqlite3BtreeNext() rather than a full |
| 68316 | 68414 | ** binary search. This is an optimization only. The correct answer |
| 68317 | 68415 | ** is still obtained without this case, only a little more slowely */ |
| 68318 | | - if( pCur->info.nKey+1==intKey && !pCur->skipNext ){ |
| 68416 | + if( pCur->info.nKey+1==intKey ){ |
| 68319 | 68417 | *pRes = 0; |
| 68320 | 68418 | rc = sqlite3BtreeNext(pCur, 0); |
| 68321 | 68419 | if( rc==SQLITE_OK ){ |
| 68322 | 68420 | getCellInfo(pCur); |
| 68323 | 68421 | if( pCur->info.nKey==intKey ){ |
| | @@ -68587,28 +68685,22 @@ |
| 68587 | 68685 | int rc; |
| 68588 | 68686 | int idx; |
| 68589 | 68687 | MemPage *pPage; |
| 68590 | 68688 | |
| 68591 | 68689 | assert( cursorOwnsBtShared(pCur) ); |
| 68592 | | - assert( pCur->skipNext==0 || pCur->eState!=CURSOR_VALID ); |
| 68593 | 68690 | if( pCur->eState!=CURSOR_VALID ){ |
| 68594 | 68691 | assert( (pCur->curFlags & BTCF_ValidOvfl)==0 ); |
| 68595 | 68692 | rc = restoreCursorPosition(pCur); |
| 68596 | 68693 | if( rc!=SQLITE_OK ){ |
| 68597 | 68694 | return rc; |
| 68598 | 68695 | } |
| 68599 | 68696 | if( CURSOR_INVALID==pCur->eState ){ |
| 68600 | 68697 | return SQLITE_DONE; |
| 68601 | 68698 | } |
| 68602 | | - if( pCur->skipNext ){ |
| 68603 | | - assert( pCur->eState==CURSOR_VALID || pCur->eState==CURSOR_SKIPNEXT ); |
| 68699 | + if( pCur->eState==CURSOR_SKIPNEXT ){ |
| 68604 | 68700 | pCur->eState = CURSOR_VALID; |
| 68605 | | - if( pCur->skipNext>0 ){ |
| 68606 | | - pCur->skipNext = 0; |
| 68607 | | - return SQLITE_OK; |
| 68608 | | - } |
| 68609 | | - pCur->skipNext = 0; |
| 68701 | + if( pCur->skipNext>0 ) return SQLITE_OK; |
| 68610 | 68702 | } |
| 68611 | 68703 | } |
| 68612 | 68704 | |
| 68613 | 68705 | pPage = pCur->pPage; |
| 68614 | 68706 | idx = ++pCur->ix; |
| | @@ -68659,11 +68751,10 @@ |
| 68659 | 68751 | SQLITE_PRIVATE int sqlite3BtreeNext(BtCursor *pCur, int flags){ |
| 68660 | 68752 | MemPage *pPage; |
| 68661 | 68753 | UNUSED_PARAMETER( flags ); /* Used in COMDB2 but not native SQLite */ |
| 68662 | 68754 | assert( cursorOwnsBtShared(pCur) ); |
| 68663 | 68755 | assert( flags==0 || flags==1 ); |
| 68664 | | - assert( pCur->skipNext==0 || pCur->eState!=CURSOR_VALID ); |
| 68665 | 68756 | pCur->info.nSize = 0; |
| 68666 | 68757 | pCur->curFlags &= ~(BTCF_ValidNKey|BTCF_ValidOvfl); |
| 68667 | 68758 | if( pCur->eState!=CURSOR_VALID ) return btreeNext(pCur); |
| 68668 | 68759 | pPage = pCur->pPage; |
| 68669 | 68760 | if( (++pCur->ix)>=pPage->nCell ){ |
| | @@ -68700,11 +68791,10 @@ |
| 68700 | 68791 | static SQLITE_NOINLINE int btreePrevious(BtCursor *pCur){ |
| 68701 | 68792 | int rc; |
| 68702 | 68793 | MemPage *pPage; |
| 68703 | 68794 | |
| 68704 | 68795 | assert( cursorOwnsBtShared(pCur) ); |
| 68705 | | - assert( pCur->skipNext==0 || pCur->eState!=CURSOR_VALID ); |
| 68706 | 68796 | assert( (pCur->curFlags & (BTCF_AtLast|BTCF_ValidOvfl|BTCF_ValidNKey))==0 ); |
| 68707 | 68797 | assert( pCur->info.nSize==0 ); |
| 68708 | 68798 | if( pCur->eState!=CURSOR_VALID ){ |
| 68709 | 68799 | rc = restoreCursorPosition(pCur); |
| 68710 | 68800 | if( rc!=SQLITE_OK ){ |
| | @@ -68711,18 +68801,13 @@ |
| 68711 | 68801 | return rc; |
| 68712 | 68802 | } |
| 68713 | 68803 | if( CURSOR_INVALID==pCur->eState ){ |
| 68714 | 68804 | return SQLITE_DONE; |
| 68715 | 68805 | } |
| 68716 | | - if( pCur->skipNext ){ |
| 68717 | | - assert( pCur->eState==CURSOR_VALID || pCur->eState==CURSOR_SKIPNEXT ); |
| 68806 | + if( CURSOR_SKIPNEXT==pCur->eState ){ |
| 68718 | 68807 | pCur->eState = CURSOR_VALID; |
| 68719 | | - if( pCur->skipNext<0 ){ |
| 68720 | | - pCur->skipNext = 0; |
| 68721 | | - return SQLITE_OK; |
| 68722 | | - } |
| 68723 | | - pCur->skipNext = 0; |
| 68808 | + if( pCur->skipNext<0 ) return SQLITE_OK; |
| 68724 | 68809 | } |
| 68725 | 68810 | } |
| 68726 | 68811 | |
| 68727 | 68812 | pPage = pCur->pPage; |
| 68728 | 68813 | assert( pPage->isInit ); |
| | @@ -68753,11 +68838,10 @@ |
| 68753 | 68838 | return rc; |
| 68754 | 68839 | } |
| 68755 | 68840 | SQLITE_PRIVATE int sqlite3BtreePrevious(BtCursor *pCur, int flags){ |
| 68756 | 68841 | assert( cursorOwnsBtShared(pCur) ); |
| 68757 | 68842 | assert( flags==0 || flags==1 ); |
| 68758 | | - assert( pCur->skipNext==0 || pCur->eState!=CURSOR_VALID ); |
| 68759 | 68843 | UNUSED_PARAMETER( flags ); /* Used in COMDB2 but not native SQLite */ |
| 68760 | 68844 | pCur->curFlags &= ~(BTCF_AtLast|BTCF_ValidOvfl|BTCF_ValidNKey); |
| 68761 | 68845 | pCur->info.nSize = 0; |
| 68762 | 68846 | if( pCur->eState!=CURSOR_VALID |
| 68763 | 68847 | || pCur->ix==0 |
| | @@ -69722,27 +69806,38 @@ |
| 69722 | 69806 | ** / | \ |
| 69723 | 69807 | ** --------- --------- --------- |
| 69724 | 69808 | ** |Child-1| |Child-2| |Child-3| |
| 69725 | 69809 | ** --------- --------- --------- |
| 69726 | 69810 | ** |
| 69727 | | -** The order of cells is in the array is: |
| 69811 | +** The order of cells is in the array is for an index btree is: |
| 69728 | 69812 | ** |
| 69729 | 69813 | ** 1. All cells from Child-1 in order |
| 69730 | 69814 | ** 2. The first divider cell from Parent |
| 69731 | 69815 | ** 3. All cells from Child-2 in order |
| 69732 | 69816 | ** 4. The second divider cell from Parent |
| 69733 | 69817 | ** 5. All cells from Child-3 in order |
| 69734 | 69818 | ** |
| 69735 | | -** The apEnd[] array holds pointer to the end of page for Child-1, the |
| 69736 | | -** Parent, Child-2, the Parent (again), and Child-3. The ixNx[] array |
| 69737 | | -** holds the number of cells contained in each of these 5 stages, and |
| 69738 | | -** all stages to the left. Hence: |
| 69819 | +** For a table-btree (with rowids) the items 2 and 4 are empty because |
| 69820 | +** content exists only in leaves and there are no divider cells. |
| 69821 | +** |
| 69822 | +** For an index btree, the apEnd[] array holds pointer to the end of page |
| 69823 | +** for Child-1, the Parent, Child-2, the Parent (again), and Child-3, |
| 69824 | +** respectively. The ixNx[] array holds the number of cells contained in |
| 69825 | +** each of these 5 stages, and all stages to the left. Hence: |
| 69826 | +** |
| 69739 | 69827 | ** ixNx[0] = Number of cells in Child-1. |
| 69740 | 69828 | ** ixNx[1] = Number of cells in Child-1 plus 1 for first divider. |
| 69741 | 69829 | ** ixNx[2] = Number of cells in Child-1 and Child-2 + 1 for 1st divider. |
| 69742 | 69830 | ** ixNx[3] = Number of cells in Child-1 and Child-2 + both divider cells |
| 69743 | 69831 | ** ixNx[4] = Total number of cells. |
| 69832 | +** |
| 69833 | +** For a table-btree, the concept is similar, except only apEnd[0]..apEnd[2] |
| 69834 | +** are used and they point to the leaf pages only, and the ixNx value are: |
| 69835 | +** |
| 69836 | +** ixNx[0] = Number of cells in Child-1. |
| 69837 | +** ixNx[1] = Number of cells in Child-1 and Child-2 + 1 for 1st divider. |
| 69838 | +** ixNx[2] = Number of cells in Child-1 and Child-2 + both divider cells |
| 69744 | 69839 | */ |
| 69745 | 69840 | typedef struct CellArray CellArray; |
| 69746 | 69841 | struct CellArray { |
| 69747 | 69842 | int nCell; /* Number of cells in apCell[] */ |
| 69748 | 69843 | MemPage *pRef; /* Reference page */ |
| | @@ -69808,20 +69903,21 @@ |
| 69808 | 69903 | const int hdr = pPg->hdrOffset; /* Offset of header on pPg */ |
| 69809 | 69904 | u8 * const aData = pPg->aData; /* Pointer to data for pPg */ |
| 69810 | 69905 | const int usableSize = pPg->pBt->usableSize; |
| 69811 | 69906 | u8 * const pEnd = &aData[usableSize]; |
| 69812 | 69907 | int i = iFirst; /* Which cell to copy from pCArray*/ |
| 69813 | | - int j; /* Start of cell content area */ |
| 69908 | + u32 j; /* Start of cell content area */ |
| 69814 | 69909 | int iEnd = i+nCell; /* Loop terminator */ |
| 69815 | 69910 | u8 *pCellptr = pPg->aCellIdx; |
| 69816 | 69911 | u8 *pTmp = sqlite3PagerTempSpace(pPg->pBt->pPager); |
| 69817 | 69912 | u8 *pData; |
| 69818 | 69913 | int k; /* Current slot in pCArray->apEnd[] */ |
| 69819 | 69914 | u8 *pSrcEnd; /* Current pCArray->apEnd[k] value */ |
| 69820 | 69915 | |
| 69821 | 69916 | assert( i<iEnd ); |
| 69822 | 69917 | j = get2byte(&aData[hdr+5]); |
| 69918 | + if( NEVER(j>(u32)usableSize) ){ j = 0; } |
| 69823 | 69919 | memcpy(&pTmp[j], &aData[j], usableSize - j); |
| 69824 | 69920 | |
| 69825 | 69921 | for(k=0; pCArray->ixNx[k]<=i && ALWAYS(k<NB*2); k++){} |
| 69826 | 69922 | pSrcEnd = pCArray->apEnd[k]; |
| 69827 | 69923 | |
| | @@ -69997,11 +70093,11 @@ |
| 69997 | 70093 | } |
| 69998 | 70094 | return nRet; |
| 69999 | 70095 | } |
| 70000 | 70096 | |
| 70001 | 70097 | /* |
| 70002 | | -** pCArray contains pointers to and sizes of all cells in the pages being |
| 70098 | +** pCArray contains pointers to and sizes of all cells in the page being |
| 70003 | 70099 | ** balanced. The current page, pPg, has pPg->nCell cells starting with |
| 70004 | 70100 | ** pCArray->apCell[iOld]. After balancing, this page should hold nNew cells |
| 70005 | 70101 | ** starting at apCell[iNew]. |
| 70006 | 70102 | ** |
| 70007 | 70103 | ** This routine makes the necessary adjustments to pPg so that it contains |
| | @@ -70031,26 +70127,31 @@ |
| 70031 | 70127 | u8 *pTmp = sqlite3PagerTempSpace(pPg->pBt->pPager); |
| 70032 | 70128 | memcpy(pTmp, aData, pPg->pBt->usableSize); |
| 70033 | 70129 | #endif |
| 70034 | 70130 | |
| 70035 | 70131 | /* Remove cells from the start and end of the page */ |
| 70132 | + assert( nCell>=0 ); |
| 70036 | 70133 | if( iOld<iNew ){ |
| 70037 | 70134 | int nShift = pageFreeArray(pPg, iOld, iNew-iOld, pCArray); |
| 70135 | + if( nShift>nCell ) return SQLITE_CORRUPT_BKPT; |
| 70038 | 70136 | memmove(pPg->aCellIdx, &pPg->aCellIdx[nShift*2], nCell*2); |
| 70039 | 70137 | nCell -= nShift; |
| 70040 | 70138 | } |
| 70041 | 70139 | if( iNewEnd < iOldEnd ){ |
| 70042 | | - nCell -= pageFreeArray(pPg, iNewEnd, iOldEnd - iNewEnd, pCArray); |
| 70140 | + int nTail = pageFreeArray(pPg, iNewEnd, iOldEnd - iNewEnd, pCArray); |
| 70141 | + assert( nCell>=nTail ); |
| 70142 | + nCell -= nTail; |
| 70043 | 70143 | } |
| 70044 | 70144 | |
| 70045 | 70145 | pData = &aData[get2byteNotZero(&aData[hdr+5])]; |
| 70046 | 70146 | if( pData<pBegin ) goto editpage_fail; |
| 70047 | 70147 | |
| 70048 | 70148 | /* Add cells to the start of the page */ |
| 70049 | 70149 | if( iNew<iOld ){ |
| 70050 | 70150 | int nAdd = MIN(nNew,iOld-iNew); |
| 70051 | 70151 | assert( (iOld-iNew)<nNew || nCell==0 || CORRUPT_DB ); |
| 70152 | + assert( nAdd>=0 ); |
| 70052 | 70153 | pCellptr = pPg->aCellIdx; |
| 70053 | 70154 | memmove(&pCellptr[nAdd*2], pCellptr, nCell*2); |
| 70054 | 70155 | if( pageInsertArray( |
| 70055 | 70156 | pPg, pBegin, &pData, pCellptr, |
| 70056 | 70157 | iNew, nAdd, pCArray |
| | @@ -70061,10 +70162,11 @@ |
| 70061 | 70162 | /* Add any overflow cells */ |
| 70062 | 70163 | for(i=0; i<pPg->nOverflow; i++){ |
| 70063 | 70164 | int iCell = (iOld + pPg->aiOvfl[i]) - iNew; |
| 70064 | 70165 | if( iCell>=0 && iCell<nNew ){ |
| 70065 | 70166 | pCellptr = &pPg->aCellIdx[iCell * 2]; |
| 70167 | + assert( nCell>=iCell ); |
| 70066 | 70168 | memmove(&pCellptr[2], pCellptr, (nCell - iCell) * 2); |
| 70067 | 70169 | nCell++; |
| 70068 | 70170 | if( pageInsertArray( |
| 70069 | 70171 | pPg, pBegin, &pData, pCellptr, |
| 70070 | 70172 | iCell+iNew, 1, pCArray |
| | @@ -70071,10 +70173,11 @@ |
| 70071 | 70173 | ) ) goto editpage_fail; |
| 70072 | 70174 | } |
| 70073 | 70175 | } |
| 70074 | 70176 | |
| 70075 | 70177 | /* Append cells to the end of the page */ |
| 70178 | + assert( nCell>=0 ); |
| 70076 | 70179 | pCellptr = &pPg->aCellIdx[nCell*2]; |
| 70077 | 70180 | if( pageInsertArray( |
| 70078 | 70181 | pPg, pBegin, &pData, pCellptr, |
| 70079 | 70182 | iNew+nCell, nNew-nCell, pCArray |
| 70080 | 70183 | ) ) goto editpage_fail; |
| | @@ -70644,16 +70747,19 @@ |
| 70644 | 70747 | ** the right of the i-th sibling page. |
| 70645 | 70748 | ** usableSpace: Number of bytes of space available on each sibling. |
| 70646 | 70749 | ** |
| 70647 | 70750 | */ |
| 70648 | 70751 | usableSpace = pBt->usableSize - 12 + leafCorrection; |
| 70649 | | - for(i=0; i<nOld; i++){ |
| 70752 | + for(i=k=0; i<nOld; i++, k++){ |
| 70650 | 70753 | MemPage *p = apOld[i]; |
| 70651 | | - b.apEnd[i*2] = p->aDataEnd; |
| 70652 | | - b.apEnd[i*2+1] = pParent->aDataEnd; |
| 70653 | | - b.ixNx[i*2] = cntOld[i]; |
| 70654 | | - b.ixNx[i*2+1] = cntOld[i]+1; |
| 70754 | + b.apEnd[k] = p->aDataEnd; |
| 70755 | + b.ixNx[k] = cntOld[i]; |
| 70756 | + if( !leafData ){ |
| 70757 | + k++; |
| 70758 | + b.apEnd[k] = pParent->aDataEnd; |
| 70759 | + b.ixNx[k] = cntOld[i]+1; |
| 70760 | + } |
| 70655 | 70761 | szNew[i] = usableSpace - p->nFree; |
| 70656 | 70762 | for(j=0; j<p->nOverflow; j++){ |
| 70657 | 70763 | szNew[i] += 2 + p->xCellSize(p, p->apOvfl[j]); |
| 70658 | 70764 | } |
| 70659 | 70765 | cntNew[i] = cntOld[i]; |
| | @@ -71749,11 +71855,10 @@ |
| 71749 | 71855 | ** from the internal node. The 'previous' entry is used for this instead |
| 71750 | 71856 | ** of the 'next' entry, as the previous entry is always a part of the |
| 71751 | 71857 | ** sub-tree headed by the child page of the cell being deleted. This makes |
| 71752 | 71858 | ** balancing the tree following the delete operation easier. */ |
| 71753 | 71859 | if( !pPage->leaf ){ |
| 71754 | | - pCur->skipNext = 0; |
| 71755 | 71860 | rc = sqlite3BtreePrevious(pCur, 0); |
| 71756 | 71861 | assert( rc!=SQLITE_DONE ); |
| 71757 | 71862 | if( rc ) return rc; |
| 71758 | 71863 | } |
| 71759 | 71864 | |
| | @@ -75234,10 +75339,13 @@ |
| 75234 | 75339 | u32 amt, /* Number of bytes to return. */ |
| 75235 | 75340 | Mem *pMem /* OUT: Return data in this Mem structure. */ |
| 75236 | 75341 | ){ |
| 75237 | 75342 | int rc; |
| 75238 | 75343 | pMem->flags = MEM_Null; |
| 75344 | + if( sqlite3BtreeMaxRecordSize(pCur)<offset+amt ){ |
| 75345 | + return SQLITE_CORRUPT_BKPT; |
| 75346 | + } |
| 75239 | 75347 | if( SQLITE_OK==(rc = sqlite3VdbeMemClearAndResize(pMem, amt+1)) ){ |
| 75240 | 75348 | rc = sqlite3BtreePayload(pCur, offset, amt, pMem->z); |
| 75241 | 75349 | if( rc==SQLITE_OK ){ |
| 75242 | 75350 | pMem->z[amt] = 0; /* Overrun area used when reading malformed records */ |
| 75243 | 75351 | pMem->flags = MEM_Blob; |
| | @@ -97882,10 +97990,11 @@ |
| 97882 | 97990 | static void gatherSelectWindows(Select *p){ |
| 97883 | 97991 | Walker w; |
| 97884 | 97992 | w.xExprCallback = gatherSelectWindowsCallback; |
| 97885 | 97993 | w.xSelectCallback = gatherSelectWindowsSelectCallback; |
| 97886 | 97994 | w.xSelectCallback2 = 0; |
| 97995 | + w.pParse = 0; |
| 97887 | 97996 | w.u.pSelect = p; |
| 97888 | 97997 | sqlite3WalkSelect(&w, p); |
| 97889 | 97998 | } |
| 97890 | 97999 | #endif |
| 97891 | 98000 | |
| | @@ -101504,18 +101613,20 @@ |
| 101504 | 101613 | if( ExprHasProperty(pExpr, EP_FromJoin) ) return WRC_Prune; |
| 101505 | 101614 | switch( pExpr->op ){ |
| 101506 | 101615 | case TK_ISNOT: |
| 101507 | 101616 | case TK_NOT: |
| 101508 | 101617 | case TK_ISNULL: |
| 101618 | + case TK_NOTNULL: |
| 101509 | 101619 | case TK_IS: |
| 101510 | 101620 | case TK_OR: |
| 101511 | 101621 | case TK_CASE: |
| 101512 | 101622 | case TK_IN: |
| 101513 | 101623 | case TK_FUNCTION: |
| 101514 | 101624 | testcase( pExpr->op==TK_ISNOT ); |
| 101515 | 101625 | testcase( pExpr->op==TK_NOT ); |
| 101516 | 101626 | testcase( pExpr->op==TK_ISNULL ); |
| 101627 | + testcase( pExpr->op==TK_NOTNULL ); |
| 101517 | 101628 | testcase( pExpr->op==TK_IS ); |
| 101518 | 101629 | testcase( pExpr->op==TK_OR ); |
| 101519 | 101630 | testcase( pExpr->op==TK_CASE ); |
| 101520 | 101631 | testcase( pExpr->op==TK_IN ); |
| 101521 | 101632 | testcase( pExpr->op==TK_FUNCTION ); |
| | @@ -105728,12 +105839,12 @@ |
| 105728 | 105839 | return; |
| 105729 | 105840 | } |
| 105730 | 105841 | assert( pVfs ); |
| 105731 | 105842 | flags |= SQLITE_OPEN_MAIN_DB; |
| 105732 | 105843 | rc = sqlite3BtreeOpen(pVfs, zPath, db, &pNew->pBt, 0, flags); |
| 105733 | | - sqlite3_free( zPath ); |
| 105734 | 105844 | db->nDb++; |
| 105845 | + pNew->zDbSName = sqlite3DbStrDup(db, zName); |
| 105735 | 105846 | } |
| 105736 | 105847 | db->noSharedCache = 0; |
| 105737 | 105848 | if( rc==SQLITE_CONSTRAINT ){ |
| 105738 | 105849 | rc = SQLITE_ERROR; |
| 105739 | 105850 | zErrDyn = sqlite3MPrintf(db, "database is already attached"); |
| | @@ -105757,11 +105868,10 @@ |
| 105757 | 105868 | PAGER_SYNCHRONOUS_FULL | (db->flags & PAGER_FLAGS_MASK)); |
| 105758 | 105869 | #endif |
| 105759 | 105870 | sqlite3BtreeLeave(pNew->pBt); |
| 105760 | 105871 | } |
| 105761 | 105872 | pNew->safety_level = SQLITE_DEFAULT_SYNCHRONOUS+1; |
| 105762 | | - if( !REOPEN_AS_MEMDB(db) ) pNew->zDbSName = sqlite3DbStrDup(db, zName); |
| 105763 | 105873 | if( rc==SQLITE_OK && pNew->zDbSName==0 ){ |
| 105764 | 105874 | rc = SQLITE_NOMEM_BKPT; |
| 105765 | 105875 | } |
| 105766 | 105876 | |
| 105767 | 105877 | |
| | @@ -105785,19 +105895,23 @@ |
| 105785 | 105895 | zKey = (char *)sqlite3_value_blob(argv[2]); |
| 105786 | 105896 | rc = sqlite3CodecAttach(db, db->nDb-1, zKey, nKey); |
| 105787 | 105897 | break; |
| 105788 | 105898 | |
| 105789 | 105899 | case SQLITE_NULL: |
| 105790 | | - /* No key specified. Use the key from the main database */ |
| 105791 | | - sqlite3CodecGetKey(db, 0, (void**)&zKey, &nKey); |
| 105792 | | - if( nKey || sqlite3BtreeGetOptimalReserve(db->aDb[0].pBt)>0 ){ |
| 105793 | | - rc = sqlite3CodecAttach(db, db->nDb-1, zKey, nKey); |
| 105900 | + /* No key specified. Use the key from URI filename, or if none, |
| 105901 | + ** use the key from the main database. */ |
| 105902 | + if( sqlite3CodecQueryParameters(db, zName, zPath)==0 ){ |
| 105903 | + sqlite3CodecGetKey(db, 0, (void**)&zKey, &nKey); |
| 105904 | + if( nKey || sqlite3BtreeGetOptimalReserve(db->aDb[0].pBt)>0 ){ |
| 105905 | + rc = sqlite3CodecAttach(db, db->nDb-1, zKey, nKey); |
| 105906 | + } |
| 105794 | 105907 | } |
| 105795 | 105908 | break; |
| 105796 | 105909 | } |
| 105797 | 105910 | } |
| 105798 | 105911 | #endif |
| 105912 | + sqlite3_free( zPath ); |
| 105799 | 105913 | |
| 105800 | 105914 | /* If the file was opened successfully, read the schema for the new database. |
| 105801 | 105915 | ** If this fails, or if opening the file failed, then close the file and |
| 105802 | 105916 | ** remove the entry from the db->aDb[] array. i.e. put everything back the |
| 105803 | 105917 | ** way we found it. |
| | @@ -109965,21 +110079,26 @@ |
| 109965 | 110079 | assert( pParse->nErr==0 ); |
| 109966 | 110080 | if( db->init.busy ){ |
| 109967 | 110081 | Index *p; |
| 109968 | 110082 | assert( !IN_SPECIAL_PARSE ); |
| 109969 | 110083 | assert( sqlite3SchemaMutexHeld(db, 0, pIndex->pSchema) ); |
| 110084 | + if( pTblName!=0 ){ |
| 110085 | + pIndex->tnum = db->init.newTnum; |
| 110086 | + if( sqlite3IndexHasDuplicateRootPage(pIndex) ){ |
| 110087 | + sqlite3ErrorMsg(pParse, "invalid rootpage"); |
| 110088 | + pParse->rc = SQLITE_CORRUPT_BKPT; |
| 110089 | + goto exit_create_index; |
| 110090 | + } |
| 110091 | + } |
| 109970 | 110092 | p = sqlite3HashInsert(&pIndex->pSchema->idxHash, |
| 109971 | 110093 | pIndex->zName, pIndex); |
| 109972 | 110094 | if( p ){ |
| 109973 | 110095 | assert( p==pIndex ); /* Malloc must have failed */ |
| 109974 | 110096 | sqlite3OomFault(db); |
| 109975 | 110097 | goto exit_create_index; |
| 109976 | 110098 | } |
| 109977 | 110099 | db->mDbFlags |= DBFLAG_SchemaChange; |
| 109978 | | - if( pTblName!=0 ){ |
| 109979 | | - pIndex->tnum = db->init.newTnum; |
| 109980 | | - } |
| 109981 | 110100 | } |
| 109982 | 110101 | |
| 109983 | 110102 | /* If this is the initial CREATE INDEX statement (or CREATE TABLE if the |
| 109984 | 110103 | ** index is an implied index for a UNIQUE or PRIMARY KEY constraint) then |
| 109985 | 110104 | ** emit code to allocate the index rootpage on disk and make an entry for |
| | @@ -110351,11 +110470,10 @@ |
| 110351 | 110470 | |
| 110352 | 110471 | /* Allocate additional space if needed */ |
| 110353 | 110472 | if( (u32)pSrc->nSrc+nExtra>pSrc->nAlloc ){ |
| 110354 | 110473 | SrcList *pNew; |
| 110355 | 110474 | int nAlloc = pSrc->nSrc*2+nExtra; |
| 110356 | | - int nGot; |
| 110357 | 110475 | sqlite3 *db = pParse->db; |
| 110358 | 110476 | |
| 110359 | 110477 | if( pSrc->nSrc+nExtra>=SQLITE_MAX_SRCLIST ){ |
| 110360 | 110478 | sqlite3ErrorMsg(pParse, "too many FROM clause terms, max: %d", |
| 110361 | 110479 | SQLITE_MAX_SRCLIST); |
| | @@ -110367,12 +110485,11 @@ |
| 110367 | 110485 | if( pNew==0 ){ |
| 110368 | 110486 | assert( db->mallocFailed ); |
| 110369 | 110487 | return 0; |
| 110370 | 110488 | } |
| 110371 | 110489 | pSrc = pNew; |
| 110372 | | - nGot = (sqlite3DbMallocSize(db, pNew) - sizeof(*pSrc))/sizeof(pSrc->a[0])+1; |
| 110373 | | - pSrc->nAlloc = nGot; |
| 110490 | + pSrc->nAlloc = nAlloc; |
| 110374 | 110491 | } |
| 110375 | 110492 | |
| 110376 | 110493 | /* Move existing slots that come after the newly inserted slots |
| 110377 | 110494 | ** out of the way */ |
| 110378 | 110495 | for(i=pSrc->nSrc-1; i>=iStart; i--){ |
| | @@ -118554,11 +118671,11 @@ |
| 118554 | 118671 | if( zSql==0 ) zSql = ""; |
| 118555 | 118672 | |
| 118556 | 118673 | sqlite3_mutex_enter(db->mutex); |
| 118557 | 118674 | sqlite3Error(db, SQLITE_OK); |
| 118558 | 118675 | while( rc==SQLITE_OK && zSql[0] ){ |
| 118559 | | - int nCol; |
| 118676 | + int nCol = 0; |
| 118560 | 118677 | char **azVals = 0; |
| 118561 | 118678 | |
| 118562 | 118679 | pStmt = 0; |
| 118563 | 118680 | rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, &zLeftover); |
| 118564 | 118681 | assert( rc==SQLITE_OK || pStmt==0 ); |
| | @@ -118568,13 +118685,11 @@ |
| 118568 | 118685 | if( !pStmt ){ |
| 118569 | 118686 | /* this happens for a comment or white-space */ |
| 118570 | 118687 | zSql = zLeftover; |
| 118571 | 118688 | continue; |
| 118572 | 118689 | } |
| 118573 | | - |
| 118574 | 118690 | callbackIsInit = 0; |
| 118575 | | - nCol = sqlite3_column_count(pStmt); |
| 118576 | 118691 | |
| 118577 | 118692 | while( 1 ){ |
| 118578 | 118693 | int i; |
| 118579 | 118694 | rc = sqlite3_step(pStmt); |
| 118580 | 118695 | |
| | @@ -118581,10 +118696,11 @@ |
| 118581 | 118696 | /* Invoke the callback function if required */ |
| 118582 | 118697 | if( xCallback && (SQLITE_ROW==rc || |
| 118583 | 118698 | (SQLITE_DONE==rc && !callbackIsInit |
| 118584 | 118699 | && db->flags&SQLITE_NullCallback)) ){ |
| 118585 | 118700 | if( !callbackIsInit ){ |
| 118701 | + nCol = sqlite3_column_count(pStmt); |
| 118586 | 118702 | azCols = sqlite3DbMallocRaw(db, (2*nCol+1)*sizeof(const char*)); |
| 118587 | 118703 | if( azCols==0 ){ |
| 118588 | 118704 | goto exec_out; |
| 118589 | 118705 | } |
| 118590 | 118706 | for(i=0; i<nCol; i++){ |
| | @@ -120194,12 +120310,11 @@ |
| 120194 | 120310 | #define PragTyp_WAL_CHECKPOINT 39 |
| 120195 | 120311 | #define PragTyp_ACTIVATE_EXTENSIONS 40 |
| 120196 | 120312 | #define PragTyp_HEXKEY 41 |
| 120197 | 120313 | #define PragTyp_KEY 42 |
| 120198 | 120314 | #define PragTyp_LOCK_STATUS 43 |
| 120199 | | -#define PragTyp_PARSER_TRACE 44 |
| 120200 | | -#define PragTyp_STATS 45 |
| 120315 | +#define PragTyp_STATS 44 |
| 120201 | 120316 | |
| 120202 | 120317 | /* Property flags associated with various pragma. */ |
| 120203 | 120318 | #define PragFlg_NeedSchema 0x01 /* Force schema load before running */ |
| 120204 | 120319 | #define PragFlg_NoColumns 0x02 /* OP_ResultRow called with zero columns */ |
| 120205 | 120320 | #define PragFlg_NoColumns1 0x04 /* zero columns if RHS argument is present */ |
| | @@ -120356,26 +120471,22 @@ |
| 120356 | 120471 | /* ePragFlg: */ PragFlg_Result0, |
| 120357 | 120472 | /* ColNames: */ 0, 0, |
| 120358 | 120473 | /* iArg: */ 0 }, |
| 120359 | 120474 | #endif |
| 120360 | 120475 | #if !defined(SQLITE_OMIT_FLAG_PRAGMAS) |
| 120361 | | -#if !defined(SQLITE_OMIT_DEPRECATED) |
| 120362 | 120476 | {/* zName: */ "count_changes", |
| 120363 | 120477 | /* ePragTyp: */ PragTyp_FLAG, |
| 120364 | 120478 | /* ePragFlg: */ PragFlg_Result0|PragFlg_NoColumns1, |
| 120365 | 120479 | /* ColNames: */ 0, 0, |
| 120366 | 120480 | /* iArg: */ SQLITE_CountRows }, |
| 120367 | 120481 | #endif |
| 120368 | | -#endif |
| 120369 | 120482 | #if !defined(SQLITE_OMIT_PAGER_PRAGMAS) && SQLITE_OS_WIN |
| 120370 | | -#if !defined(SQLITE_OMIT_DEPRECATED) |
| 120371 | 120483 | {/* zName: */ "data_store_directory", |
| 120372 | 120484 | /* ePragTyp: */ PragTyp_DATA_STORE_DIRECTORY, |
| 120373 | 120485 | /* ePragFlg: */ PragFlg_NoColumns1, |
| 120374 | 120486 | /* ColNames: */ 0, 0, |
| 120375 | 120487 | /* iArg: */ 0 }, |
| 120376 | | -#endif |
| 120377 | 120488 | #endif |
| 120378 | 120489 | #if !defined(SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS) |
| 120379 | 120490 | {/* zName: */ "data_version", |
| 120380 | 120491 | /* ePragTyp: */ PragTyp_HEADER_VALUE, |
| 120381 | 120492 | /* ePragFlg: */ PragFlg_ReadOnly|PragFlg_Result0, |
| | @@ -120387,18 +120498,16 @@ |
| 120387 | 120498 | /* ePragTyp: */ PragTyp_DATABASE_LIST, |
| 120388 | 120499 | /* ePragFlg: */ PragFlg_NeedSchema|PragFlg_Result0, |
| 120389 | 120500 | /* ColNames: */ 35, 3, |
| 120390 | 120501 | /* iArg: */ 0 }, |
| 120391 | 120502 | #endif |
| 120392 | | -#if !defined(SQLITE_OMIT_PAGER_PRAGMAS) |
| 120393 | | -#if !defined(SQLITE_OMIT_DEPRECATED) |
| 120503 | +#if !defined(SQLITE_OMIT_PAGER_PRAGMAS) && !defined(SQLITE_OMIT_DEPRECATED) |
| 120394 | 120504 | {/* zName: */ "default_cache_size", |
| 120395 | 120505 | /* ePragTyp: */ PragTyp_DEFAULT_CACHE_SIZE, |
| 120396 | 120506 | /* ePragFlg: */ PragFlg_NeedSchema|PragFlg_Result0|PragFlg_SchemaReq|PragFlg_NoColumns1, |
| 120397 | 120507 | /* ColNames: */ 45, 1, |
| 120398 | 120508 | /* iArg: */ 0 }, |
| 120399 | | -#endif |
| 120400 | 120509 | #endif |
| 120401 | 120510 | #if !defined(SQLITE_OMIT_FLAG_PRAGMAS) |
| 120402 | 120511 | #if !defined(SQLITE_OMIT_FOREIGN_KEY) && !defined(SQLITE_OMIT_TRIGGER) |
| 120403 | 120512 | {/* zName: */ "defer_foreign_keys", |
| 120404 | 120513 | /* ePragTyp: */ PragTyp_FLAG, |
| | @@ -120406,17 +120515,15 @@ |
| 120406 | 120515 | /* ColNames: */ 0, 0, |
| 120407 | 120516 | /* iArg: */ SQLITE_DeferFKs }, |
| 120408 | 120517 | #endif |
| 120409 | 120518 | #endif |
| 120410 | 120519 | #if !defined(SQLITE_OMIT_FLAG_PRAGMAS) |
| 120411 | | -#if !defined(SQLITE_OMIT_DEPRECATED) |
| 120412 | 120520 | {/* zName: */ "empty_result_callbacks", |
| 120413 | 120521 | /* ePragTyp: */ PragTyp_FLAG, |
| 120414 | 120522 | /* ePragFlg: */ PragFlg_Result0|PragFlg_NoColumns1, |
| 120415 | 120523 | /* ColNames: */ 0, 0, |
| 120416 | 120524 | /* iArg: */ SQLITE_NullCallback }, |
| 120417 | | -#endif |
| 120418 | 120525 | #endif |
| 120419 | 120526 | #if !defined(SQLITE_OMIT_UTF16) |
| 120420 | 120527 | {/* zName: */ "encoding", |
| 120421 | 120528 | /* ePragTyp: */ PragTyp_ENCODING, |
| 120422 | 120529 | /* ePragFlg: */ PragFlg_Result0|PragFlg_NoColumns1, |
| | @@ -120452,19 +120559,15 @@ |
| 120452 | 120559 | /* ePragFlg: */ PragFlg_ReadOnly|PragFlg_Result0, |
| 120453 | 120560 | /* ColNames: */ 0, 0, |
| 120454 | 120561 | /* iArg: */ BTREE_FREE_PAGE_COUNT }, |
| 120455 | 120562 | #endif |
| 120456 | 120563 | #if !defined(SQLITE_OMIT_FLAG_PRAGMAS) |
| 120457 | | -#if !defined(SQLITE_OMIT_DEPRECATED) |
| 120458 | 120564 | {/* zName: */ "full_column_names", |
| 120459 | 120565 | /* ePragTyp: */ PragTyp_FLAG, |
| 120460 | 120566 | /* ePragFlg: */ PragFlg_Result0|PragFlg_NoColumns1, |
| 120461 | 120567 | /* ColNames: */ 0, 0, |
| 120462 | 120568 | /* iArg: */ SQLITE_FullColNames }, |
| 120463 | | -#endif |
| 120464 | | -#endif |
| 120465 | | -#if !defined(SQLITE_OMIT_FLAG_PRAGMAS) |
| 120466 | 120569 | {/* zName: */ "fullfsync", |
| 120467 | 120570 | /* ePragTyp: */ PragTyp_FLAG, |
| 120468 | 120571 | /* ePragFlg: */ PragFlg_Result0|PragFlg_NoColumns1, |
| 120469 | 120572 | /* ColNames: */ 0, 0, |
| 120470 | 120573 | /* iArg: */ SQLITE_FullFSync }, |
| | @@ -120618,16 +120721,18 @@ |
| 120618 | 120721 | /* ePragTyp: */ PragTyp_PAGE_SIZE, |
| 120619 | 120722 | /* ePragFlg: */ PragFlg_Result0|PragFlg_SchemaReq|PragFlg_NoColumns1, |
| 120620 | 120723 | /* ColNames: */ 0, 0, |
| 120621 | 120724 | /* iArg: */ 0 }, |
| 120622 | 120725 | #endif |
| 120623 | | -#if defined(SQLITE_DEBUG) && !defined(SQLITE_OMIT_PARSER_TRACE) |
| 120726 | +#if !defined(SQLITE_OMIT_FLAG_PRAGMAS) |
| 120727 | +#if defined(SQLITE_DEBUG) |
| 120624 | 120728 | {/* zName: */ "parser_trace", |
| 120625 | | - /* ePragTyp: */ PragTyp_PARSER_TRACE, |
| 120626 | | - /* ePragFlg: */ 0, |
| 120729 | + /* ePragTyp: */ PragTyp_FLAG, |
| 120730 | + /* ePragFlg: */ PragFlg_Result0|PragFlg_NoColumns1, |
| 120627 | 120731 | /* ColNames: */ 0, 0, |
| 120628 | | - /* iArg: */ 0 }, |
| 120732 | + /* iArg: */ SQLITE_ParserTrace }, |
| 120733 | +#endif |
| 120629 | 120734 | #endif |
| 120630 | 120735 | #if defined(SQLITE_INTROSPECTION_PRAGMAS) |
| 120631 | 120736 | {/* zName: */ "pragma_list", |
| 120632 | 120737 | /* ePragTyp: */ PragTyp_PRAGMA_LIST, |
| 120633 | 120738 | /* ePragFlg: */ PragFlg_Result0, |
| | @@ -120687,17 +120792,15 @@ |
| 120687 | 120792 | /* ePragFlg: */ PragFlg_Result0, |
| 120688 | 120793 | /* ColNames: */ 0, 0, |
| 120689 | 120794 | /* iArg: */ 0 }, |
| 120690 | 120795 | #endif |
| 120691 | 120796 | #if !defined(SQLITE_OMIT_FLAG_PRAGMAS) |
| 120692 | | -#if !defined(SQLITE_OMIT_DEPRECATED) |
| 120693 | 120797 | {/* zName: */ "short_column_names", |
| 120694 | 120798 | /* ePragTyp: */ PragTyp_FLAG, |
| 120695 | 120799 | /* ePragFlg: */ PragFlg_Result0|PragFlg_NoColumns1, |
| 120696 | 120800 | /* ColNames: */ 0, 0, |
| 120697 | 120801 | /* iArg: */ SQLITE_ShortColNames }, |
| 120698 | | -#endif |
| 120699 | 120802 | #endif |
| 120700 | 120803 | {/* zName: */ "shrink_memory", |
| 120701 | 120804 | /* ePragTyp: */ PragTyp_SHRINK_MEMORY, |
| 120702 | 120805 | /* ePragFlg: */ PragFlg_NoColumns, |
| 120703 | 120806 | /* ColNames: */ 0, 0, |
| | @@ -120746,19 +120849,15 @@ |
| 120746 | 120849 | {/* zName: */ "temp_store", |
| 120747 | 120850 | /* ePragTyp: */ PragTyp_TEMP_STORE, |
| 120748 | 120851 | /* ePragFlg: */ PragFlg_Result0|PragFlg_NoColumns1, |
| 120749 | 120852 | /* ColNames: */ 0, 0, |
| 120750 | 120853 | /* iArg: */ 0 }, |
| 120751 | | -#endif |
| 120752 | | -#if !defined(SQLITE_OMIT_PAGER_PRAGMAS) |
| 120753 | | -#if !defined(SQLITE_OMIT_DEPRECATED) |
| 120754 | 120854 | {/* zName: */ "temp_store_directory", |
| 120755 | 120855 | /* ePragTyp: */ PragTyp_TEMP_STORE_DIRECTORY, |
| 120756 | 120856 | /* ePragFlg: */ PragFlg_NoColumns1, |
| 120757 | 120857 | /* ColNames: */ 0, 0, |
| 120758 | 120858 | /* iArg: */ 0 }, |
| 120759 | | -#endif |
| 120760 | 120859 | #endif |
| 120761 | 120860 | #if defined(SQLITE_HAS_CODEC) |
| 120762 | 120861 | {/* zName: */ "textkey", |
| 120763 | 120862 | /* ePragTyp: */ PragTyp_KEY, |
| 120764 | 120863 | /* ePragFlg: */ 0, |
| | @@ -122225,23 +122324,10 @@ |
| 122225 | 122324 | } |
| 122226 | 122325 | break; |
| 122227 | 122326 | #endif /* !defined(SQLITE_OMIT_TRIGGER) */ |
| 122228 | 122327 | #endif /* !defined(SQLITE_OMIT_FOREIGN_KEY) */ |
| 122229 | 122328 | |
| 122230 | | -#ifndef NDEBUG |
| 122231 | | - case PragTyp_PARSER_TRACE: { |
| 122232 | | - if( zRight ){ |
| 122233 | | - if( sqlite3GetBoolean(zRight, 0) ){ |
| 122234 | | - sqlite3ParserTrace(stdout, "parser: "); |
| 122235 | | - }else{ |
| 122236 | | - sqlite3ParserTrace(0, 0); |
| 122237 | | - } |
| 122238 | | - } |
| 122239 | | - } |
| 122240 | | - break; |
| 122241 | | -#endif |
| 122242 | | - |
| 122243 | 122329 | /* Reinstall the LIKE and GLOB functions. The variant of LIKE |
| 122244 | 122330 | ** used will be case sensitive or not depending on the RHS. |
| 122245 | 122331 | */ |
| 122246 | 122332 | case PragTyp_CASE_SENSITIVE_LIKE: { |
| 122247 | 122333 | if( zRight ){ |
| | @@ -123358,10 +123444,23 @@ |
| 123358 | 123444 | if( zExtra && zExtra[0] ) z = sqlite3MPrintf(db, "%z - %s", z, zExtra); |
| 123359 | 123445 | *pData->pzErrMsg = z; |
| 123360 | 123446 | pData->rc = SQLITE_CORRUPT_BKPT; |
| 123361 | 123447 | } |
| 123362 | 123448 | } |
| 123449 | + |
| 123450 | +/* |
| 123451 | +** Check to see if any sibling index (another index on the same table) |
| 123452 | +** of pIndex has the same root page number, and if it does, return true. |
| 123453 | +** This would indicate a corrupt schema. |
| 123454 | +*/ |
| 123455 | +SQLITE_PRIVATE int sqlite3IndexHasDuplicateRootPage(Index *pIndex){ |
| 123456 | + Index *p; |
| 123457 | + for(p=pIndex->pTable->pIndex; p; p=p->pNext){ |
| 123458 | + if( p->tnum==pIndex->tnum && p!=pIndex ) return 1; |
| 123459 | + } |
| 123460 | + return 0; |
| 123461 | +} |
| 123363 | 123462 | |
| 123364 | 123463 | /* |
| 123365 | 123464 | ** This is the callback routine for the code that initializes the |
| 123366 | 123465 | ** database. See sqlite3Init() below for additional information. |
| 123367 | 123466 | ** This routine is also called from the OP_ParseSchema opcode of the VDBE. |
| | @@ -123437,10 +123536,11 @@ |
| 123437 | 123536 | Index *pIndex; |
| 123438 | 123537 | pIndex = sqlite3FindIndex(db, argv[0], db->aDb[iDb].zDbSName); |
| 123439 | 123538 | if( pIndex==0 |
| 123440 | 123539 | || sqlite3GetInt32(argv[1],&pIndex->tnum)==0 |
| 123441 | 123540 | || pIndex->tnum<2 |
| 123541 | + || sqlite3IndexHasDuplicateRootPage(pIndex) |
| 123442 | 123542 | ){ |
| 123443 | 123543 | corruptSchema(pData, argv[0], pIndex?"invalid rootpage":"orphan index"); |
| 123444 | 123544 | } |
| 123445 | 123545 | } |
| 123446 | 123546 | return 0; |
| | @@ -129976,10 +130076,11 @@ |
| 129976 | 130076 | ){ |
| 129977 | 130077 | continue; |
| 129978 | 130078 | } |
| 129979 | 130079 | |
| 129980 | 130080 | if( flattenSubquery(pParse, p, i, isAgg) ){ |
| 130081 | + if( pParse->nErr ) goto select_end; |
| 129981 | 130082 | /* This subquery can be absorbed into its parent. */ |
| 129982 | 130083 | i = -1; |
| 129983 | 130084 | } |
| 129984 | 130085 | pTabList = p->pSrc; |
| 129985 | 130086 | if( db->mallocFailed ) goto select_end; |
| | @@ -134903,10 +135004,11 @@ |
| 134903 | 135004 | for(i=0; rc==SQLITE_OK && i<db->nVTrans; i++){ |
| 134904 | 135005 | VTable *pVTab = db->aVTrans[i]; |
| 134905 | 135006 | const sqlite3_module *pMod = pVTab->pMod->pModule; |
| 134906 | 135007 | if( pVTab->pVtab && pMod->iVersion>=2 ){ |
| 134907 | 135008 | int (*xMethod)(sqlite3_vtab *, int); |
| 135009 | + sqlite3VtabLock(pVTab); |
| 134908 | 135010 | switch( op ){ |
| 134909 | 135011 | case SAVEPOINT_BEGIN: |
| 134910 | 135012 | xMethod = pMod->xSavepoint; |
| 134911 | 135013 | pVTab->iSavepoint = iSavepoint+1; |
| 134912 | 135014 | break; |
| | @@ -134918,10 +135020,11 @@ |
| 134918 | 135020 | break; |
| 134919 | 135021 | } |
| 134920 | 135022 | if( xMethod && pVTab->iSavepoint>iSavepoint ){ |
| 134921 | 135023 | rc = xMethod(pVTab->pVtab, iSavepoint); |
| 134922 | 135024 | } |
| 135025 | + sqlite3VtabUnlock(pVTab); |
| 134923 | 135026 | } |
| 134924 | 135027 | } |
| 134925 | 135028 | } |
| 134926 | 135029 | return rc; |
| 134927 | 135030 | } |
| | @@ -137496,12 +137599,17 @@ |
| 137496 | 137599 | ** Do not do this for the RHS of a LEFT JOIN. This is because the |
| 137497 | 137600 | ** expression may be evaluated after OP_NullRow has been executed on |
| 137498 | 137601 | ** the cursor. In this case it is important to do the full evaluation, |
| 137499 | 137602 | ** as the result of the expression may not be NULL, even if all table |
| 137500 | 137603 | ** column values are. https://www.sqlite.org/src/info/7fa8049685b50b5a |
| 137604 | + ** |
| 137605 | + ** Also, do not do this when processing one index an a multi-index |
| 137606 | + ** OR clause, since the transformation will become invalid once we |
| 137607 | + ** move forward to the next index. |
| 137608 | + ** https://sqlite.org/src/info/4e8e4857d32d401f |
| 137501 | 137609 | */ |
| 137502 | | - if( pLevel->iLeftJoin==0 ){ |
| 137610 | + if( pLevel->iLeftJoin==0 && (pWInfo->wctrlFlags & WHERE_OR_SUBCLAUSE)==0 ){ |
| 137503 | 137611 | whereIndexExprTrans(pIdx, iCur, iIdxCur, pWInfo); |
| 137504 | 137612 | } |
| 137505 | 137613 | |
| 137506 | 137614 | /* Record the instruction used to terminate the loop. */ |
| 137507 | 137615 | if( pLoop->wsFlags & WHERE_ONEROW ){ |
| | @@ -139887,10 +139995,21 @@ |
| 139887 | 139995 | k = 0; |
| 139888 | 139996 | pScan->iEquiv++; |
| 139889 | 139997 | } |
| 139890 | 139998 | return 0; |
| 139891 | 139999 | } |
| 140000 | + |
| 140001 | +/* |
| 140002 | +** This is whereScanInit() for the case of an index on an expression. |
| 140003 | +** It is factored out into a separate tail-recursion subroutine so that |
| 140004 | +** the normal whereScanInit() routine, which is a high-runner, does not |
| 140005 | +** need to push registers onto the stack as part of its prologue. |
| 140006 | +*/ |
| 140007 | +static SQLITE_NOINLINE WhereTerm *whereScanInitIndexExpr(WhereScan *pScan){ |
| 140008 | + pScan->idxaff = sqlite3ExprAffinity(pScan->pIdxExpr); |
| 140009 | + return whereScanNext(pScan); |
| 140010 | +} |
| 139892 | 140011 | |
| 139893 | 140012 | /* |
| 139894 | 140013 | ** Initialize a WHERE clause scanner object. Return a pointer to the |
| 139895 | 140014 | ** first match. Return NULL if there are no matches. |
| 139896 | 140015 | ** |
| | @@ -139920,31 +140039,33 @@ |
| 139920 | 140039 | pScan->pOrigWC = pWC; |
| 139921 | 140040 | pScan->pWC = pWC; |
| 139922 | 140041 | pScan->pIdxExpr = 0; |
| 139923 | 140042 | pScan->idxaff = 0; |
| 139924 | 140043 | pScan->zCollName = 0; |
| 140044 | + pScan->opMask = opMask; |
| 140045 | + pScan->k = 0; |
| 140046 | + pScan->aiCur[0] = iCur; |
| 140047 | + pScan->nEquiv = 1; |
| 140048 | + pScan->iEquiv = 1; |
| 139925 | 140049 | if( pIdx ){ |
| 139926 | 140050 | int j = iColumn; |
| 139927 | 140051 | iColumn = pIdx->aiColumn[j]; |
| 139928 | 140052 | if( iColumn==XN_EXPR ){ |
| 139929 | 140053 | pScan->pIdxExpr = pIdx->aColExpr->a[j].pExpr; |
| 139930 | 140054 | pScan->zCollName = pIdx->azColl[j]; |
| 140055 | + pScan->aiColumn[0] = XN_EXPR; |
| 140056 | + return whereScanInitIndexExpr(pScan); |
| 139931 | 140057 | }else if( iColumn==pIdx->pTable->iPKey ){ |
| 139932 | 140058 | iColumn = XN_ROWID; |
| 139933 | 140059 | }else if( iColumn>=0 ){ |
| 139934 | 140060 | pScan->idxaff = pIdx->pTable->aCol[iColumn].affinity; |
| 139935 | 140061 | pScan->zCollName = pIdx->azColl[j]; |
| 139936 | 140062 | } |
| 139937 | 140063 | }else if( iColumn==XN_EXPR ){ |
| 139938 | 140064 | return 0; |
| 139939 | 140065 | } |
| 139940 | | - pScan->opMask = opMask; |
| 139941 | | - pScan->k = 0; |
| 139942 | | - pScan->aiCur[0] = iCur; |
| 139943 | 140066 | pScan->aiColumn[0] = iColumn; |
| 139944 | | - pScan->nEquiv = 1; |
| 139945 | | - pScan->iEquiv = 1; |
| 139946 | 140067 | return whereScanNext(pScan); |
| 139947 | 140068 | } |
| 139948 | 140069 | |
| 139949 | 140070 | /* |
| 139950 | 140071 | ** Search for a term in the WHERE clause that is of the form "X <op> <expr>" |
| | @@ -152900,11 +153021,18 @@ |
| 152900 | 153021 | db->u1.isInterrupted = 0; |
| 152901 | 153022 | } |
| 152902 | 153023 | pParse->rc = SQLITE_OK; |
| 152903 | 153024 | pParse->zTail = zSql; |
| 152904 | 153025 | assert( pzErrMsg!=0 ); |
| 152905 | | - /* sqlite3ParserTrace(stdout, "parser: "); */ |
| 153026 | +#ifdef SQLITE_DEBUG |
| 153027 | + if( db->flags & SQLITE_ParserTrace ){ |
| 153028 | + printf("parser: [[[%s]]]\n", zSql); |
| 153029 | + sqlite3ParserTrace(stdout, "parser: "); |
| 153030 | + }else{ |
| 153031 | + sqlite3ParserTrace(0, 0); |
| 153032 | + } |
| 153033 | +#endif |
| 152906 | 153034 | #ifdef sqlite3Parser_ENGINEALWAYSONSTACK |
| 152907 | 153035 | pEngine = &sEngine; |
| 152908 | 153036 | sqlite3ParserInit(pEngine, pParse); |
| 152909 | 153037 | #else |
| 152910 | 153038 | pEngine = sqlite3ParserAlloc(sqlite3Malloc, pParse); |
| | @@ -153068,11 +153196,11 @@ |
| 153068 | 153196 | ){ |
| 153069 | 153197 | sqlite3 *db; /* The database connection */ |
| 153070 | 153198 | int i; /* Next unread byte of zSql[] */ |
| 153071 | 153199 | int n; /* length of current token */ |
| 153072 | 153200 | int tokenType; /* type of current token */ |
| 153073 | | - int prevType; /* Previous non-whitespace token */ |
| 153201 | + int prevType = 0; /* Previous non-whitespace token */ |
| 153074 | 153202 | int nParen; /* Number of nested levels of parentheses */ |
| 153075 | 153203 | int iStartIN; /* Start of RHS of IN operator in z[] */ |
| 153076 | 153204 | int nParenAtIN; /* Value of nParent at start of RHS of IN operator */ |
| 153077 | 153205 | int j; /* Bytes of normalized SQL generated so far */ |
| 153078 | 153206 | sqlite3_str *pStr; /* The normalized SQL string under construction */ |
| | @@ -154223,10 +154351,17 @@ |
| 154223 | 154351 | sqlite3GlobalConfig.szSorterRef = (u32)iVal; |
| 154224 | 154352 | break; |
| 154225 | 154353 | } |
| 154226 | 154354 | #endif /* SQLITE_ENABLE_SORTER_REFERENCES */ |
| 154227 | 154355 | |
| 154356 | +#ifdef SQLITE_ENABLE_DESERIALIZE |
| 154357 | + case SQLITE_CONFIG_MEMDB_MAXSIZE: { |
| 154358 | + sqlite3GlobalConfig.mxMemdbSize = va_arg(ap, sqlite3_int64); |
| 154359 | + break; |
| 154360 | + } |
| 154361 | +#endif /* SQLITE_ENABLE_DESERIALIZE */ |
| 154362 | + |
| 154228 | 154363 | default: { |
| 154229 | 154364 | rc = SQLITE_ERROR; |
| 154230 | 154365 | break; |
| 154231 | 154366 | } |
| 154232 | 154367 | } |
| | @@ -156505,10 +156640,44 @@ |
| 156505 | 156640 | *pFlags = flags; |
| 156506 | 156641 | *pzFile = zFile; |
| 156507 | 156642 | return rc; |
| 156508 | 156643 | } |
| 156509 | 156644 | |
| 156645 | +#if defined(SQLITE_HAS_CODEC) |
| 156646 | +/* |
| 156647 | +** Process URI filename query parameters relevant to the SQLite Encryption |
| 156648 | +** Extension. Return true if any of the relevant query parameters are |
| 156649 | +** seen and return false if not. |
| 156650 | +*/ |
| 156651 | +SQLITE_PRIVATE int sqlite3CodecQueryParameters( |
| 156652 | + sqlite3 *db, /* Database connection */ |
| 156653 | + const char *zDb, /* Which schema is being created/attached */ |
| 156654 | + const char *zUri /* URI filename */ |
| 156655 | +){ |
| 156656 | + const char *zKey; |
| 156657 | + if( (zKey = sqlite3_uri_parameter(zUri, "hexkey"))!=0 && zKey[0] ){ |
| 156658 | + u8 iByte; |
| 156659 | + int i; |
| 156660 | + char zDecoded[40]; |
| 156661 | + for(i=0, iByte=0; i<sizeof(zDecoded)*2 && sqlite3Isxdigit(zKey[i]); i++){ |
| 156662 | + iByte = (iByte<<4) + sqlite3HexToInt(zKey[i]); |
| 156663 | + if( (i&1)!=0 ) zDecoded[i/2] = iByte; |
| 156664 | + } |
| 156665 | + sqlite3_key_v2(db, zDb, zDecoded, i/2); |
| 156666 | + return 1; |
| 156667 | + }else if( (zKey = sqlite3_uri_parameter(zUri, "key"))!=0 ){ |
| 156668 | + sqlite3_key_v2(db, zDb, zKey, sqlite3Strlen30(zKey)); |
| 156669 | + return 1; |
| 156670 | + }else if( (zKey = sqlite3_uri_parameter(zUri, "textkey"))!=0 ){ |
| 156671 | + sqlite3_key_v2(db, zDb, zKey, -1); |
| 156672 | + return 1; |
| 156673 | + }else{ |
| 156674 | + return 0; |
| 156675 | + } |
| 156676 | +} |
| 156677 | +#endif |
| 156678 | + |
| 156510 | 156679 | |
| 156511 | 156680 | /* |
| 156512 | 156681 | ** This routine does the work of opening a database on behalf of |
| 156513 | 156682 | ** sqlite3_open() and sqlite3_open16(). The database filename "zFilename" |
| 156514 | 156683 | ** is UTF-8 encoded. |
| | @@ -156850,29 +157019,16 @@ |
| 156850 | 157019 | void *pArg = sqlite3GlobalConfig.pSqllogArg; |
| 156851 | 157020 | sqlite3GlobalConfig.xSqllog(pArg, db, zFilename, 0); |
| 156852 | 157021 | } |
| 156853 | 157022 | #endif |
| 156854 | 157023 | #if defined(SQLITE_HAS_CODEC) |
| 156855 | | - if( rc==SQLITE_OK ){ |
| 156856 | | - const char *zKey; |
| 156857 | | - if( (zKey = sqlite3_uri_parameter(zOpen, "hexkey"))!=0 && zKey[0] ){ |
| 156858 | | - u8 iByte; |
| 156859 | | - int i; |
| 156860 | | - char zDecoded[40]; |
| 156861 | | - for(i=0, iByte=0; i<sizeof(zDecoded)*2 && sqlite3Isxdigit(zKey[i]); i++){ |
| 156862 | | - iByte = (iByte<<4) + sqlite3HexToInt(zKey[i]); |
| 156863 | | - if( (i&1)!=0 ) zDecoded[i/2] = iByte; |
| 156864 | | - } |
| 156865 | | - sqlite3_key_v2(db, 0, zDecoded, i/2); |
| 156866 | | - }else if( (zKey = sqlite3_uri_parameter(zOpen, "key"))!=0 ){ |
| 156867 | | - sqlite3_key_v2(db, 0, zKey, sqlite3Strlen30(zKey)); |
| 156868 | | - } |
| 156869 | | - } |
| 157024 | + if( rc==SQLITE_OK ) sqlite3CodecQueryParameters(db, 0, zOpen); |
| 156870 | 157025 | #endif |
| 156871 | 157026 | sqlite3_free(zOpen); |
| 156872 | 157027 | return rc & 0xff; |
| 156873 | 157028 | } |
| 157029 | + |
| 156874 | 157030 | |
| 156875 | 157031 | /* |
| 156876 | 157032 | ** Open a new database handle. |
| 156877 | 157033 | */ |
| 156878 | 157034 | SQLITE_API int sqlite3_open( |
| | @@ -161990,10 +162146,11 @@ |
| 161990 | 162146 | */ |
| 161991 | 162147 | pTS->aaOutput[0] = sqlite3_malloc(nDoclist + FTS3_VARINT_MAX + 1); |
| 161992 | 162148 | pTS->anOutput[0] = nDoclist; |
| 161993 | 162149 | if( pTS->aaOutput[0] ){ |
| 161994 | 162150 | memcpy(pTS->aaOutput[0], aDoclist, nDoclist); |
| 162151 | + memset(&pTS->aaOutput[0][nDoclist], 0, FTS3_VARINT_MAX); |
| 161995 | 162152 | }else{ |
| 161996 | 162153 | return SQLITE_NOMEM; |
| 161997 | 162154 | } |
| 161998 | 162155 | }else{ |
| 161999 | 162156 | char *aMerge = aDoclist; |
| | @@ -165603,19 +165760,19 @@ |
| 165603 | 165760 | if( iEq>=0 || iGe>=0 ){ |
| 165604 | 165761 | const unsigned char *zStr = sqlite3_value_text(apVal[0]); |
| 165605 | 165762 | assert( (iEq==0 && iGe==-1) || (iEq==-1 && iGe==0) ); |
| 165606 | 165763 | if( zStr ){ |
| 165607 | 165764 | pCsr->filter.zTerm = sqlite3_mprintf("%s", zStr); |
| 165608 | | - pCsr->filter.nTerm = sqlite3_value_bytes(apVal[0]); |
| 165609 | 165765 | if( pCsr->filter.zTerm==0 ) return SQLITE_NOMEM; |
| 165766 | + pCsr->filter.nTerm = (int)strlen(pCsr->filter.zTerm); |
| 165610 | 165767 | } |
| 165611 | 165768 | } |
| 165612 | 165769 | |
| 165613 | 165770 | if( iLe>=0 ){ |
| 165614 | 165771 | pCsr->zStop = sqlite3_mprintf("%s", sqlite3_value_text(apVal[iLe])); |
| 165615 | | - pCsr->nStop = sqlite3_value_bytes(apVal[iLe]); |
| 165616 | 165772 | if( pCsr->zStop==0 ) return SQLITE_NOMEM; |
| 165773 | + pCsr->nStop = (int)strlen(pCsr->zStop); |
| 165617 | 165774 | } |
| 165618 | 165775 | |
| 165619 | 165776 | if( iLangid>=0 ){ |
| 165620 | 165777 | iLangVal = sqlite3_value_int(apVal[iLangid]); |
| 165621 | 165778 | |
| | @@ -175488,15 +175645,18 @@ |
| 175488 | 175645 | assert( rc==SQLITE_OK || pCsr==0 ); |
| 175489 | 175646 | if( pCsr ){ |
| 175490 | 175647 | int iFirst = 0; |
| 175491 | 175648 | pPhrase->pList = pCsr; |
| 175492 | 175649 | fts3GetDeltaPosition(&pCsr, &iFirst); |
| 175493 | | - assert( iFirst>=0 ); |
| 175494 | | - pPhrase->pHead = pCsr; |
| 175495 | | - pPhrase->pTail = pCsr; |
| 175496 | | - pPhrase->iHead = iFirst; |
| 175497 | | - pPhrase->iTail = iFirst; |
| 175650 | + if( iFirst<0 ){ |
| 175651 | + rc = FTS_CORRUPT_VTAB; |
| 175652 | + }else{ |
| 175653 | + pPhrase->pHead = pCsr; |
| 175654 | + pPhrase->pTail = pCsr; |
| 175655 | + pPhrase->iHead = iFirst; |
| 175656 | + pPhrase->iTail = iFirst; |
| 175657 | + } |
| 175498 | 175658 | }else{ |
| 175499 | 175659 | assert( rc!=SQLITE_OK || ( |
| 175500 | 175660 | pPhrase->pList==0 && pPhrase->pHead==0 && pPhrase->pTail==0 |
| 175501 | 175661 | )); |
| 175502 | 175662 | } |
| | @@ -202679,11 +202839,13 @@ |
| 202679 | 202839 | int iAdj; |
| 202680 | 202840 | int nScore; |
| 202681 | 202841 | int jj; |
| 202682 | 202842 | |
| 202683 | 202843 | rc = pApi->xInst(pFts, ii, &ip, &ic, &io); |
| 202684 | | - if( ic!=i || rc!=SQLITE_OK ) continue; |
| 202844 | + if( ic!=i ) continue; |
| 202845 | + if( io>nDocsize ) rc = FTS5_CORRUPT; |
| 202846 | + if( rc!=SQLITE_OK ) continue; |
| 202685 | 202847 | memset(aSeen, 0, nPhrase); |
| 202686 | 202848 | rc = fts5SnippetScore(pApi, pFts, nDocsize, aSeen, i, |
| 202687 | 202849 | io, nToken, &nScore, &iAdj |
| 202688 | 202850 | ); |
| 202689 | 202851 | if( rc==SQLITE_OK && nScore>nBestScore ){ |
| | @@ -209352,11 +209514,11 @@ |
| 209352 | 209514 | u8 *a = pIter->pLeaf->p; /* Buffer to read data from */ |
| 209353 | 209515 | int iOff = pIter->iLeafOffset; /* Offset to read at */ |
| 209354 | 209516 | int nNew; /* Bytes of new data */ |
| 209355 | 209517 | |
| 209356 | 209518 | iOff += fts5GetVarint32(&a[iOff], nNew); |
| 209357 | | - if( iOff+nNew>pIter->pLeaf->szLeaf || nKeep>pIter->term.n ){ |
| 209519 | + if( iOff+nNew>pIter->pLeaf->szLeaf || nKeep>pIter->term.n || nNew==0 ){ |
| 209358 | 209520 | p->rc = FTS5_CORRUPT; |
| 209359 | 209521 | return; |
| 209360 | 209522 | } |
| 209361 | 209523 | pIter->term.n = nKeep; |
| 209362 | 209524 | fts5BufferAppendBlob(&p->rc, &pIter->term, nNew, &a[iOff]); |
| | @@ -210026,11 +210188,11 @@ |
| 210026 | 210188 | }while( 1 ); |
| 210027 | 210189 | } |
| 210028 | 210190 | |
| 210029 | 210191 | search_success: |
| 210030 | 210192 | pIter->iLeafOffset = iOff + nNew; |
| 210031 | | - if( pIter->iLeafOffset>n ){ |
| 210193 | + if( pIter->iLeafOffset>n || nNew<1 ){ |
| 210032 | 210194 | p->rc = FTS5_CORRUPT; |
| 210033 | 210195 | return; |
| 210034 | 210196 | } |
| 210035 | 210197 | pIter->iTermLeafOffset = pIter->iLeafOffset; |
| 210036 | 210198 | pIter->iTermLeafPgno = pIter->iLeafPgno; |
| | @@ -210804,11 +210966,12 @@ |
| 210804 | 210966 | Fts5Index *p, |
| 210805 | 210967 | Fts5SegIter *pSeg, |
| 210806 | 210968 | Fts5Colset *pColset, |
| 210807 | 210969 | Fts5Buffer *pBuf |
| 210808 | 210970 | ){ |
| 210809 | | - if( 0==fts5BufferGrow(&p->rc, pBuf, pSeg->nPos) ){ |
| 210971 | + if( 0==fts5BufferGrow(&p->rc, pBuf, pSeg->nPos+FTS5_DATA_ZERO_PADDING) ){ |
| 210972 | + memset(&pBuf->p[pBuf->n+pSeg->nPos], 0, FTS5_DATA_ZERO_PADDING); |
| 210810 | 210973 | if( pColset==0 ){ |
| 210811 | 210974 | fts5ChunkIterate(p, pSeg, (void*)pBuf, fts5PoslistCallback); |
| 210812 | 210975 | }else{ |
| 210813 | 210976 | if( p->pConfig->eDetail==FTS5_DETAIL_FULL ){ |
| 210814 | 210977 | PoslistCallbackCtx sCtx; |
| | @@ -211845,11 +212008,11 @@ |
| 211845 | 212008 | Fts5Data *pData; |
| 211846 | 212009 | int iId = pSeg->pSeg->iSegid; |
| 211847 | 212010 | u8 aHdr[4] = {0x00, 0x00, 0x00, 0x00}; |
| 211848 | 212011 | |
| 211849 | 212012 | iLeafRowid = FTS5_SEGMENT_ROWID(iId, pSeg->iTermLeafPgno); |
| 211850 | | - pData = fts5DataRead(p, iLeafRowid); |
| 212013 | + pData = fts5LeafRead(p, iLeafRowid); |
| 211851 | 212014 | if( pData ){ |
| 211852 | 212015 | if( iOff>pData->szLeaf ){ |
| 211853 | 212016 | /* This can occur if the pages that the segments occupy overlap - if |
| 211854 | 212017 | ** a single page has been assigned to more than one segment. In |
| 211855 | 212018 | ** this case a prior iteration of this loop may have corrupted the |
| | @@ -212251,10 +212414,11 @@ |
| 212251 | 212414 | if( writer.bFirstRowidInPage ){ |
| 212252 | 212415 | fts5PutU16(&pBuf->p[0], (u16)pBuf->n); /* first rowid on page */ |
| 212253 | 212416 | pBuf->n += sqlite3Fts5PutVarint(&pBuf->p[pBuf->n], iRowid); |
| 212254 | 212417 | writer.bFirstRowidInPage = 0; |
| 212255 | 212418 | fts5WriteDlidxAppend(p, &writer, iRowid); |
| 212419 | + if( p->rc!=SQLITE_OK ) break; |
| 212256 | 212420 | }else{ |
| 212257 | 212421 | pBuf->n += sqlite3Fts5PutVarint(&pBuf->p[pBuf->n], iDelta); |
| 212258 | 212422 | } |
| 212259 | 212423 | assert( pBuf->n<=pBuf->nSpace ); |
| 212260 | 212424 | |
| | @@ -212482,15 +212646,17 @@ |
| 212482 | 212646 | i64 iDelta, |
| 212483 | 212647 | Fts5Iter *pMulti, |
| 212484 | 212648 | Fts5Buffer *pBuf |
| 212485 | 212649 | ){ |
| 212486 | 212650 | int nData = pMulti->base.nData; |
| 212651 | + int nByte = nData + 9 + 9 + FTS5_DATA_ZERO_PADDING; |
| 212487 | 212652 | assert( nData>0 ); |
| 212488 | | - if( p->rc==SQLITE_OK && 0==fts5BufferGrow(&p->rc, pBuf, nData+9+9) ){ |
| 212653 | + if( p->rc==SQLITE_OK && 0==fts5BufferGrow(&p->rc, pBuf, nByte) ){ |
| 212489 | 212654 | fts5BufferSafeAppendVarint(pBuf, iDelta); |
| 212490 | 212655 | fts5BufferSafeAppendVarint(pBuf, nData*2); |
| 212491 | 212656 | fts5BufferSafeAppendBlob(pBuf, pMulti->base.pData, nData); |
| 212657 | + memset(&pBuf->p[pBuf->n], 0, FTS5_DATA_ZERO_PADDING); |
| 212492 | 212658 | } |
| 212493 | 212659 | } |
| 212494 | 212660 | |
| 212495 | 212661 | |
| 212496 | 212662 | static void fts5DoclistIterNext(Fts5DoclistIter *pIter){ |
| | @@ -212700,11 +212866,11 @@ |
| 212700 | 212866 | sqlite3Fts5PoslistSafeAppend(&tmp, &iPrev, iPos1); |
| 212701 | 212867 | } |
| 212702 | 212868 | sqlite3Fts5PoslistNext64(a1, i1.nPoslist, &iOff1, &iPos1); |
| 212703 | 212869 | if( iPos1<0 ) break; |
| 212704 | 212870 | }else{ |
| 212705 | | - assert( iPos2!=iPrev ); |
| 212871 | + assert_nc( iPos2!=iPrev ); |
| 212706 | 212872 | sqlite3Fts5PoslistSafeAppend(&tmp, &iPrev, iPos2); |
| 212707 | 212873 | sqlite3Fts5PoslistNext64(a2, i2.nPoslist, &iOff2, &iPos2); |
| 212708 | 212874 | if( iPos2<0 ) break; |
| 212709 | 212875 | } |
| 212710 | 212876 | } |
| | @@ -214126,10 +214292,14 @@ |
| 214126 | 214292 | fts5DecodePoslist(&rc, &s, &a[4], iOff-4); |
| 214127 | 214293 | |
| 214128 | 214294 | /* Decode any more doclist data that appears on the page before the |
| 214129 | 214295 | ** first term. */ |
| 214130 | 214296 | nDoclist = (iTermOff ? iTermOff : szLeaf) - iOff; |
| 214297 | + if( nDoclist+iOff>n ){ |
| 214298 | + rc = FTS5_CORRUPT; |
| 214299 | + goto decode_out; |
| 214300 | + } |
| 214131 | 214301 | fts5DecodeDoclist(&rc, &s, &a[iOff], nDoclist); |
| 214132 | 214302 | |
| 214133 | 214303 | while( iPgidxOff<n && rc==SQLITE_OK ){ |
| 214134 | 214304 | int bFirst = (iPgidxOff==szLeaf); /* True for first term on page */ |
| 214135 | 214305 | int nByte; /* Bytes of data */ |
| | @@ -214544,11 +214714,11 @@ |
| 214544 | 214714 | p->ts.iSavepoint = iSavepoint-1; |
| 214545 | 214715 | break; |
| 214546 | 214716 | |
| 214547 | 214717 | case FTS5_ROLLBACKTO: |
| 214548 | 214718 | assert( p->ts.eState==1 ); |
| 214549 | | - assert( iSavepoint>=0 ); |
| 214719 | + assert( iSavepoint>=-1 ); |
| 214550 | 214720 | assert( iSavepoint<=p->ts.iSavepoint ); |
| 214551 | 214721 | p->ts.iSavepoint = iSavepoint; |
| 214552 | 214722 | break; |
| 214553 | 214723 | } |
| 214554 | 214724 | } |
| | @@ -215964,11 +216134,13 @@ |
| 215964 | 216134 | const char **pz, |
| 215965 | 216135 | int *pn |
| 215966 | 216136 | ){ |
| 215967 | 216137 | int rc = SQLITE_OK; |
| 215968 | 216138 | Fts5Cursor *pCsr = (Fts5Cursor*)pCtx; |
| 215969 | | - if( fts5IsContentless((Fts5FullTable*)(pCsr->base.pVtab)) ){ |
| 216139 | + if( fts5IsContentless((Fts5FullTable*)(pCsr->base.pVtab)) |
| 216140 | + || pCsr->ePlan==FTS5_PLAN_SPECIAL |
| 216141 | + ){ |
| 215970 | 216142 | *pz = 0; |
| 215971 | 216143 | *pn = 0; |
| 215972 | 216144 | }else{ |
| 215973 | 216145 | rc = fts5SeekCursor(pCsr, 0); |
| 215974 | 216146 | if( rc==SQLITE_OK ){ |
| | @@ -216902,11 +217074,11 @@ |
| 216902 | 217074 | int nArg, /* Number of args */ |
| 216903 | 217075 | sqlite3_value **apUnused /* Function arguments */ |
| 216904 | 217076 | ){ |
| 216905 | 217077 | assert( nArg==0 ); |
| 216906 | 217078 | UNUSED_PARAM2(nArg, apUnused); |
| 216907 | | - sqlite3_result_text(pCtx, "fts5: 2019-01-26 23:34:50 a3ea1a822d3a110f4f186f2fc8550f435c8c98635d058096b7be9d4df7066b8b", -1, SQLITE_TRANSIENT); |
| 217079 | + sqlite3_result_text(pCtx, "fts5: 2019-02-08 13:17:39 0eca3dd3d38b31c92b49ca2d311128b74584714d9e7de895b1a6286ef959a1dd", -1, SQLITE_TRANSIENT); |
| 216908 | 217080 | } |
| 216909 | 217081 | |
| 216910 | 217082 | /* |
| 216911 | 217083 | ** Return true if zName is the extension on one of the shadow tables used |
| 216912 | 217084 | ** by this module. |
| | @@ -221072,10 +221244,11 @@ |
| 221072 | 221244 | }else{ |
| 221073 | 221245 | const char *zTerm; |
| 221074 | 221246 | int nTerm; |
| 221075 | 221247 | |
| 221076 | 221248 | zTerm = sqlite3Fts5IterTerm(pCsr->pIter, &nTerm); |
| 221249 | + assert( nTerm>=0 ); |
| 221077 | 221250 | if( pCsr->nLeTerm>=0 ){ |
| 221078 | 221251 | int nCmp = MIN(nTerm, pCsr->nLeTerm); |
| 221079 | 221252 | int bCmp = memcmp(pCsr->zLeTerm, zTerm, nCmp); |
| 221080 | 221253 | if( bCmp<0 || (bCmp==0 && pCsr->nLeTerm<nTerm) ){ |
| 221081 | 221254 | pCsr->bEof = 1; |
| | @@ -221665,12 +221838,12 @@ |
| 221665 | 221838 | } |
| 221666 | 221839 | #endif /* SQLITE_CORE */ |
| 221667 | 221840 | #endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_STMTVTAB) */ |
| 221668 | 221841 | |
| 221669 | 221842 | /************** End of stmt.c ************************************************/ |
| 221670 | | -#if __LINE__!=221670 |
| 221843 | +#if __LINE__!=221843 |
| 221671 | 221844 | #undef SQLITE_SOURCE_ID |
| 221672 | | -#define SQLITE_SOURCE_ID "2019-01-27 02:45:32 9cf8ebd141aa2eb661d457624c76433bd9e4abfdef04aa52e28bc169172calt2" |
| 221845 | +#define SQLITE_SOURCE_ID "2019-02-08 13:17:39 0eca3dd3d38b31c92b49ca2d311128b74584714d9e7de895b1a6286ef959alt2" |
| 221673 | 221846 | #endif |
| 221674 | 221847 | /* Return the source-id for this library */ |
| 221675 | 221848 | SQLITE_API const char *sqlite3_sourceid(void){ return SQLITE_SOURCE_ID; } |
| 221676 | 221849 | /************************** End of sqlite3.c ******************************/ |
| 221677 | 221850 | |