| | @@ -16,11 +16,11 @@ |
| 16 | 16 | ** if you want a wrapper to interface SQLite with your choice of programming |
| 17 | 17 | ** language. The code for the "sqlite3" command-line shell is also in a |
| 18 | 18 | ** separate file. This file contains only code for the core SQLite library. |
| 19 | 19 | ** |
| 20 | 20 | ** The content in this amalgamation comes from Fossil check-in |
| 21 | | -** 2c605bfb1562d7a3609ad6ffd7446def12f1 with changes in files: |
| 21 | +** 3f3fb9b638f59ad982beafb7c117f24ddd3d with changes in files: |
| 22 | 22 | ** |
| 23 | 23 | ** |
| 24 | 24 | */ |
| 25 | 25 | #ifndef SQLITE_AMALGAMATION |
| 26 | 26 | #define SQLITE_CORE 1 |
| | @@ -467,14 +467,14 @@ |
| 467 | 467 | ** [sqlite3_libversion_number()], [sqlite3_sourceid()], |
| 468 | 468 | ** [sqlite_version()] and [sqlite_source_id()]. |
| 469 | 469 | */ |
| 470 | 470 | #define SQLITE_VERSION "3.54.0" |
| 471 | 471 | #define SQLITE_VERSION_NUMBER 3054000 |
| 472 | | -#define SQLITE_SOURCE_ID "2026-05-30 13:23:25 2c605bfb1562d7a3609ad6ffd7446def12f1ac7084e41b9c6723e998c156501d" |
| 472 | +#define SQLITE_SOURCE_ID "2026-06-16 13:43:08 3f3fb9b638f59ad982beafb7c117f24ddd3da612e62c862510805fa672ffae06" |
| 473 | 473 | #define SQLITE_SCM_BRANCH "trunk" |
| 474 | 474 | #define SQLITE_SCM_TAGS "" |
| 475 | | -#define SQLITE_SCM_DATETIME "2026-05-30T13:23:25.636Z" |
| 475 | +#define SQLITE_SCM_DATETIME "2026-06-16T13:43:08.110Z" |
| 476 | 476 | |
| 477 | 477 | /* |
| 478 | 478 | ** CAPI3REF: Run-Time Library Version Numbers |
| 479 | 479 | ** KEYWORDS: sqlite3_version sqlite3_sourceid |
| 480 | 480 | ** |
| | @@ -4696,11 +4696,12 @@ |
| 4696 | 4696 | ** <dd>The maximum number of columns in a table definition or in the |
| 4697 | 4697 | ** result set of a [SELECT] or the maximum number of columns in an index |
| 4698 | 4698 | ** or in an ORDER BY or GROUP BY clause.</dd>)^ |
| 4699 | 4699 | ** |
| 4700 | 4700 | ** [[SQLITE_LIMIT_EXPR_DEPTH]] ^(<dt>SQLITE_LIMIT_EXPR_DEPTH</dt> |
| 4701 | | -** <dd>The maximum depth of the parse tree on any expression.</dd>)^ |
| 4701 | +** <dd>The maximum depth of the parse tree on any expression and |
| 4702 | +** the maximum nesting depth for subqueries and VIEWs</dd>)^ |
| 4702 | 4703 | ** |
| 4703 | 4704 | ** [[SQLITE_LIMIT_PARSER_DEPTH]] ^(<dt>SQLITE_LIMIT_PARSER_DEPTH</dt> |
| 4704 | 4705 | ** <dd>The maximum depth of the LALR(1) parser stack used to analyze |
| 4705 | 4706 | ** input SQL statements.</dd>)^ |
| 4706 | 4707 | ** |
| | @@ -4727,11 +4728,12 @@ |
| 4727 | 4728 | ** [[SQLITE_LIMIT_VARIABLE_NUMBER]] |
| 4728 | 4729 | ** ^(<dt>SQLITE_LIMIT_VARIABLE_NUMBER</dt> |
| 4729 | 4730 | ** <dd>The maximum index number of any [parameter] in an SQL statement.)^ |
| 4730 | 4731 | ** |
| 4731 | 4732 | ** [[SQLITE_LIMIT_TRIGGER_DEPTH]] ^(<dt>SQLITE_LIMIT_TRIGGER_DEPTH</dt> |
| 4732 | | -** <dd>The maximum depth of recursion for triggers.</dd>)^ |
| 4733 | +** <dd>The maximum depth of recursion for triggers, and the maximum |
| 4734 | +** nesting depth for separate triggers.</dd>)^ |
| 4733 | 4735 | ** |
| 4734 | 4736 | ** [[SQLITE_LIMIT_WORKER_THREADS]] ^(<dt>SQLITE_LIMIT_WORKER_THREADS</dt> |
| 4735 | 4737 | ** <dd>The maximum number of auxiliary worker threads that a single |
| 4736 | 4738 | ** [prepared statement] may start.</dd>)^ |
| 4737 | 4739 | ** </dl> |
| | @@ -11533,12 +11535,23 @@ |
| 11533 | 11535 | ** reopen S as an in-memory database based on the serialization |
| 11534 | 11536 | ** contained in P. If S is a NULL pointer, the main database is |
| 11535 | 11537 | ** used. The serialized database P is N bytes in size. M is the size |
| 11536 | 11538 | ** of the buffer P, which might be larger than N. If M is larger than |
| 11537 | 11539 | ** N, and the SQLITE_DESERIALIZE_READONLY bit is not set in F, then |
| 11538 | | -** SQLite is permitted to add content to the in-memory database as |
| 11539 | | -** long as the total size does not exceed M bytes. |
| 11540 | +** SQLite is permitted to add content to the in-memory database, in |
| 11541 | +** page-sized chunks, as long as the total size does not exceed M bytes. |
| 11542 | +** |
| 11543 | +** The parameter M must be greater than or equal to N. Ideally, M |
| 11544 | +** should have a value which is N+(512×K)+20 where K determines how |
| 11545 | +** must extra space is available to hold new content as the database |
| 11546 | +** grows. K can be 0 if the database is read-only. |
| 11547 | +** |
| 11548 | +** If the database content in P is malformed in a malicious way then |
| 11549 | +** it is possible that SQLite might try to read a few more than N bytes |
| 11550 | +** from P. If the veracity of the database content P is uncertain, |
| 11551 | +** then applications are advised to allocate about 20 extra bytes on |
| 11552 | +** the end of the P buffer to avoid a memory error. |
| 11540 | 11553 | ** |
| 11541 | 11554 | ** If the SQLITE_DESERIALIZE_FREEONCLOSE bit is set in F, then SQLite will |
| 11542 | 11555 | ** invoke sqlite3_free() on the serialization buffer when the database |
| 11543 | 11556 | ** connection closes. If the SQLITE_DESERIALIZE_RESIZEABLE bit is set, then |
| 11544 | 11557 | ** SQLite will try to increase the buffer size using sqlite3_realloc64() |
| | @@ -15809,10 +15822,17 @@ |
| 15809 | 15822 | */ |
| 15810 | 15823 | #ifndef offsetof |
| 15811 | 15824 | # define offsetof(ST,M) ((size_t)((char*)&((ST*)0)->M - (char*)0)) |
| 15812 | 15825 | #endif |
| 15813 | 15826 | |
| 15827 | +/* |
| 15828 | +** sizeof64() is like sizeof(), but always returns a 64-bit value, even |
| 15829 | +** on 32-bit builds. This can help to avoid overflow by ensuring 64-bit |
| 15830 | +** arithmetic is used consistently in both 32-bit and 64-bit builds. |
| 15831 | +*/ |
| 15832 | +#define sizeof64(X) ((sqlite3_int64)sizeof(X)) |
| 15833 | + |
| 15814 | 15834 | /* |
| 15815 | 15835 | ** Work around C99 "flex-array" syntax for pre-C99 compilers, so as |
| 15816 | 15836 | ** to avoid complaints from -fsanitize=strict-bounds. |
| 15817 | 15837 | */ |
| 15818 | 15838 | #if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) |
| | @@ -20928,10 +20948,11 @@ |
| 20928 | 20948 | int nTab; /* Number of previously allocated VDBE cursors */ |
| 20929 | 20949 | int nMem; /* Number of memory cells used so far */ |
| 20930 | 20950 | int szOpAlloc; /* Bytes of memory space allocated for Vdbe.aOp[] */ |
| 20931 | 20951 | int iSelfTab; /* Table associated with an index on expr, or negative |
| 20932 | 20952 | ** of the base register during check-constraint eval */ |
| 20953 | + int nNestSel; /* Number of nested SELECT statements and/or VIEWs */ |
| 20933 | 20954 | int nLabel; /* The *negative* of the number of labels used */ |
| 20934 | 20955 | int nLabelAlloc; /* Number of slots in aLabel */ |
| 20935 | 20956 | int *aLabel; /* Space to hold the labels */ |
| 20936 | 20957 | ExprList *pConstExpr;/* Constant expressions */ |
| 20937 | 20958 | IndexedExpr *pIdxEpr;/* List of expressions used by active indexes */ |
| | @@ -33389,12 +33410,12 @@ |
| 33389 | 33410 | if( flag_alternateform ){ |
| 33390 | 33411 | /* For %#q, do unistr()-style backslash escapes for |
| 33391 | 33412 | ** all control characters, and for backslash itself. |
| 33392 | 33413 | ** For %#Q, do the same but only if there is at least |
| 33393 | 33414 | ** one control character. */ |
| 33394 | | - u32 nBack = 0; |
| 33395 | | - u32 nCtrl = 0; |
| 33415 | + i64 nBack = 0; |
| 33416 | + i64 nCtrl = 0; |
| 33396 | 33417 | for(k=0; k<i; k++){ |
| 33397 | 33418 | if( escarg[k]=='\\' ){ |
| 33398 | 33419 | nBack++; |
| 33399 | 33420 | }else if( ((u8*)escarg)[k]<=0x1f ){ |
| 33400 | 33421 | nCtrl++; |
| | @@ -39255,10 +39276,26 @@ |
| 39255 | 39276 | ****************************************************************************** |
| 39256 | 39277 | ** |
| 39257 | 39278 | ** This file contains an experimental VFS layer that operates on a |
| 39258 | 39279 | ** Key/Value storage engine where both keys and values must be pure |
| 39259 | 39280 | ** text. |
| 39281 | +** |
| 39282 | +** DEBUG AND TEST |
| 39283 | +** |
| 39284 | +** For testing on Unix, compile using: |
| 39285 | +** |
| 39286 | +** make clean sqlite3d CFLAGS='-DSQLITE_OS_KV_OPTIONAL' |
| 39287 | +** |
| 39288 | +** Then start up a shell using something like: |
| 39289 | +** |
| 39290 | +** ./sqlite3d 'file:dbname?vfs=kvvfs' |
| 39291 | +** |
| 39292 | +** Each K/V entry is stored in a separate file in the working |
| 39293 | +** directory that has a name like "kvvfs-dbname-*". Due to limitations |
| 39294 | +** on the key size, the name of the database must be very short - just |
| 39295 | +** a few characters. If the database name is too long, the VFS will |
| 39296 | +** malfunction and you will get SQLITE_CORRUPT errors. |
| 39260 | 39297 | */ |
| 39261 | 39298 | /* #include <sqliteInt.h> */ |
| 39262 | 39299 | #if SQLITE_OS_KV || (SQLITE_OS_UNIX && defined(SQLITE_OS_KV_OPTIONAL)) |
| 39263 | 39300 | |
| 39264 | 39301 | /***************************************************************************** |
| | @@ -39707,16 +39744,18 @@ |
| 39707 | 39744 | } |
| 39708 | 39745 | if( j+n>nOut ) return -1; |
| 39709 | 39746 | memset(&aOut[j], 0, n); |
| 39710 | 39747 | j += n; |
| 39711 | 39748 | if( c==0 || mult==1 ) break; /* progress stalled if mult==1 */ |
| 39712 | | - }else{ |
| 39749 | + }else if( j<nOut ){ |
| 39713 | 39750 | aOut[j] = c<<4; |
| 39714 | 39751 | c = kvvfsHexValue[aIn[++i]]; |
| 39715 | 39752 | if( c<0 ) return -1 /* hex bytes are always in pairs */; |
| 39716 | 39753 | aOut[j++] += c; |
| 39717 | 39754 | i++; |
| 39755 | + }else{ |
| 39756 | + return -1; |
| 39718 | 39757 | } |
| 39719 | 39758 | } |
| 39720 | 39759 | return j; |
| 39721 | 39760 | } |
| 39722 | 39761 | |
| | @@ -40138,10 +40177,22 @@ |
| 40138 | 40177 | }else{ |
| 40139 | 40178 | pFile->isJournal = 0; |
| 40140 | 40179 | pFile->base.pMethods = &kvvfs_db_io_methods; |
| 40141 | 40180 | } |
| 40142 | 40181 | if( !pFile->zClass ){ |
| 40182 | +#ifdef SQLITE_WASM |
| 40183 | + if( strlen(zName) >= (KVRECORD_KEY_SZ |
| 40184 | + - 6 /* "kvvfs-" */ |
| 40185 | + - 11 /* "-##########" */) ){ |
| 40186 | + return SQLITE_CANTOPEN; |
| 40187 | + } |
| 40188 | +#else |
| 40189 | + if( 0!=strcmp(zName, "local") && 0!=strcmp(zName, "session") ){ |
| 40190 | + /* Historical naming restriction which journaling depends on. */ |
| 40191 | + return SQLITE_CANTOPEN; |
| 40192 | + } |
| 40193 | +#endif |
| 40143 | 40194 | pFile->zClass = zName; |
| 40144 | 40195 | } |
| 40145 | 40196 | pFile->aData = sqlite3_malloc64(SQLITE_KVOS_SZ); |
| 40146 | 40197 | if( pFile->aData==0 ){ |
| 40147 | 40198 | return SQLITE_NOMEM; |
| | @@ -52170,15 +52221,33 @@ |
| 52170 | 52221 | # define sqlite3_win_test_unc_locking 0 |
| 52171 | 52222 | #endif |
| 52172 | 52223 | |
| 52173 | 52224 | /* |
| 52174 | 52225 | ** Return true if the string passed as the only argument is likely |
| 52175 | | -** to be a UNC path. In other words, if it starts with "\\". |
| 52226 | +** to be a UNC path. Return false if note. |
| 52227 | +** |
| 52228 | +** Return true if: |
| 52229 | +** |
| 52230 | +** (1) The name begins with "\\" |
| 52231 | +** (2) But does not begin with "\\?\C:\" where C can be any alphabetic |
| 52232 | +** character. |
| 52233 | +** |
| 52234 | +** For testing, also return true in all cases if the global variable |
| 52235 | +** sqlite3_win_test_unc_locking is true. |
| 52176 | 52236 | */ |
| 52177 | 52237 | static int winIsUNCPath(const char *zFile){ |
| 52178 | 52238 | if( zFile[0]=='\\' && zFile[1]=='\\' ){ |
| 52179 | | - return 1; |
| 52239 | + if( zFile[2]=='?' |
| 52240 | + && zFile[3]=='\\' |
| 52241 | + && sqlite3Isalpha(zFile[4]) |
| 52242 | + && zFile[5]==':' |
| 52243 | + && winIsDirSep(zFile[6]) |
| 52244 | + ){ |
| 52245 | + return sqlite3_win_test_unc_locking; |
| 52246 | + }else{ |
| 52247 | + return 1; |
| 52248 | + } |
| 52180 | 52249 | } |
| 52181 | 52250 | return sqlite3_win_test_unc_locking; |
| 52182 | 52251 | } |
| 52183 | 52252 | |
| 52184 | 52253 | /* |
| | @@ -56926,26 +56995,28 @@ |
| 56926 | 56995 | szBulk = -1024 * (i64)pcache1.nInitPage; |
| 56927 | 56996 | } |
| 56928 | 56997 | if( szBulk > pCache->szAlloc*(i64)pCache->nMax ){ |
| 56929 | 56998 | szBulk = pCache->szAlloc*(i64)pCache->nMax; |
| 56930 | 56999 | } |
| 56931 | | - zBulk = pCache->pBulk = sqlite3Malloc( szBulk ); |
| 56932 | | - sqlite3EndBenignMalloc(); |
| 56933 | | - if( zBulk ){ |
| 56934 | | - int nBulk = sqlite3MallocSize(zBulk)/pCache->szAlloc; |
| 56935 | | - do{ |
| 56936 | | - PgHdr1 *pX = (PgHdr1*)&zBulk[pCache->szPage]; |
| 56937 | | - pX->page.pBuf = zBulk; |
| 56938 | | - pX->page.pExtra = (u8*)pX + ROUND8(sizeof(*pX)); |
| 56939 | | - assert( EIGHT_BYTE_ALIGNMENT( pX->page.pExtra ) ); |
| 56940 | | - pX->isBulkLocal = 1; |
| 56941 | | - pX->isAnchor = 0; |
| 56942 | | - pX->pNext = pCache->pFree; |
| 56943 | | - pX->pLruPrev = 0; /* Initializing this saves a valgrind error */ |
| 56944 | | - pCache->pFree = pX; |
| 56945 | | - zBulk += pCache->szAlloc; |
| 56946 | | - }while( --nBulk ); |
| 57000 | + if( szBulk>=pCache->szAlloc ){ |
| 57001 | + zBulk = pCache->pBulk = sqlite3Malloc( szBulk ); |
| 57002 | + sqlite3EndBenignMalloc(); |
| 57003 | + if( zBulk ){ |
| 57004 | + int nBulk = sqlite3MallocSize(zBulk)/pCache->szAlloc; |
| 57005 | + do{ |
| 57006 | + PgHdr1 *pX = (PgHdr1*)&zBulk[pCache->szPage]; |
| 57007 | + pX->page.pBuf = zBulk; |
| 57008 | + pX->page.pExtra = (u8*)pX + ROUND8(sizeof(*pX)); |
| 57009 | + assert( EIGHT_BYTE_ALIGNMENT( pX->page.pExtra ) ); |
| 57010 | + pX->isBulkLocal = 1; |
| 57011 | + pX->isAnchor = 0; |
| 57012 | + pX->pNext = pCache->pFree; |
| 57013 | + pX->pLruPrev = 0; /* Initializing this saves a valgrind error */ |
| 57014 | + pCache->pFree = pX; |
| 57015 | + zBulk += pCache->szAlloc; |
| 57016 | + }while( --nBulk ); |
| 57017 | + } |
| 56947 | 57018 | } |
| 56948 | 57019 | return pCache->pFree!=0; |
| 56949 | 57020 | } |
| 56950 | 57021 | |
| 56951 | 57022 | /* |
| | @@ -59839,73 +59910,84 @@ |
| 59839 | 59910 | #define pager_set_pagehash(X) |
| 59840 | 59911 | #define CHECK_PAGE(x) |
| 59841 | 59912 | #endif /* SQLITE_CHECK_PAGES */ |
| 59842 | 59913 | |
| 59843 | 59914 | /* |
| 59844 | | -** When this is called the journal file for pager pPager must be open. |
| 59845 | | -** This function attempts to read a super-journal file name from the |
| 59846 | | -** end of the file and, if successful, copies it into memory supplied |
| 59847 | | -** by the caller. See comments above writeSuperJournal() for the format |
| 59848 | | -** used to store a super-journal file name at the end of a journal file. |
| 59849 | | -** |
| 59850 | | -** zSuper must point to a buffer of at least nSuper bytes allocated by |
| 59851 | | -** the caller. This should be sqlite3_vfs.mxPathname+1 (to ensure there is |
| 59852 | | -** enough space to write the super-journal name). If the super-journal |
| 59853 | | -** name in the journal is longer than nSuper bytes (including a |
| 59854 | | -** nul-terminator), then this is handled as if no super-journal name |
| 59855 | | -** were present in the journal. |
| 59856 | | -** |
| 59857 | | -** If a super-journal file name is present at the end of the journal |
| 59858 | | -** file, then it is copied into the buffer pointed to by zSuper. A |
| 59859 | | -** nul-terminator byte is appended to the buffer following the |
| 59860 | | -** super-journal file name. |
| 59861 | | -** |
| 59862 | | -** If it is determined that no super-journal file name is present |
| 59863 | | -** zSuper[0] is set to 0 and SQLITE_OK returned. |
| 59864 | | -** |
| 59865 | | -** If an error occurs while reading from the journal file, an SQLite |
| 59866 | | -** error code is returned. |
| 59867 | | -*/ |
| 59868 | | -static int readSuperJournal(sqlite3_file *pJrnl, char *zSuper, u64 nSuper){ |
| 59915 | +** Free a buffer allocated by the readSuperJournal() function. |
| 59916 | +*/ |
| 59917 | +static void freeSuperJournal(char *zSuper){ |
| 59918 | + if( zSuper ){ |
| 59919 | + sqlite3_free(&zSuper[-4]); |
| 59920 | + } |
| 59921 | +} |
| 59922 | + |
| 59923 | +/* |
| 59924 | +** Parameter pJrnl is a file-handle open on a journal file. This function |
| 59925 | +** attempts to read a super-journal file name from the end of the journal |
| 59926 | +** file. If successful, it sets output parameter (*pzSuper) to point to a |
| 59927 | +** buffer containing the super-journal name as a nul-terminated string. |
| 59928 | +** The caller is responsible for freeing the buffer using freeSuperJournal(). |
| 59929 | +** |
| 59930 | +** Refer to comments above writeSuperJournal() for the format used to store |
| 59931 | +** a super-journal file name at the end of a journal file. |
| 59932 | +** |
| 59933 | +** Parameter nSuper is passed the maximum allowable size of the super journal |
| 59934 | +** name in bytes. If the super-journal name in the journal is longer than |
| 59935 | +** nSuper bytes (including a nul-terminator), then this is handled as if no |
| 59936 | +** super-journal name were present in the journal. |
| 59937 | +** |
| 59938 | +** If there is no super-journal name at the end of pJrnl, (*pzSuper) is |
| 59939 | +** set to 0 and SQLITE_OK is returned. Or, if an error occurs while reading |
| 59940 | +** the super-journal name, an SQLite error code is returned and (*pzSuper) |
| 59941 | +** is set to 0. |
| 59942 | +*/ |
| 59943 | +static int readSuperJournal(sqlite3_file *pJrnl, u64 nSuper, char **pzSuper){ |
| 59869 | 59944 | int rc; /* Return code */ |
| 59870 | 59945 | u32 len; /* Length in bytes of super-journal name */ |
| 59871 | 59946 | i64 szJ; /* Total size in bytes of journal file pJrnl */ |
| 59872 | 59947 | u32 cksum; /* MJ checksum value read from journal */ |
| 59873 | | - u32 u; /* Unsigned loop counter */ |
| 59874 | 59948 | unsigned char aMagic[8]; /* A buffer to hold the magic header */ |
| 59875 | | - zSuper[0] = '\0'; |
| 59949 | + char *zOut = 0; |
| 59876 | 59950 | |
| 59951 | + *pzSuper = 0; |
| 59877 | 59952 | if( SQLITE_OK!=(rc = sqlite3OsFileSize(pJrnl, &szJ)) |
| 59878 | 59953 | || szJ<16 |
| 59879 | 59954 | || SQLITE_OK!=(rc = read32bits(pJrnl, szJ-16, &len)) |
| 59880 | 59955 | || len>=nSuper |
| 59881 | 59956 | || len>szJ-16 |
| 59882 | 59957 | || len==0 |
| 59883 | 59958 | || SQLITE_OK!=(rc = read32bits(pJrnl, szJ-12, &cksum)) |
| 59884 | 59959 | || SQLITE_OK!=(rc = sqlite3OsRead(pJrnl, aMagic, 8, szJ-8)) |
| 59885 | 59960 | || memcmp(aMagic, aJournalMagic, 8) |
| 59886 | | - || SQLITE_OK!=(rc = sqlite3OsRead(pJrnl, zSuper, len, szJ-16-len)) |
| 59887 | 59961 | ){ |
| 59888 | 59962 | return rc; |
| 59889 | 59963 | } |
| 59890 | 59964 | |
| 59891 | | - /* See if the checksum matches the super-journal name */ |
| 59892 | | - for(u=0; u<len; u++){ |
| 59893 | | - cksum -= zSuper[u]; |
| 59894 | | - } |
| 59895 | | - if( cksum ){ |
| 59896 | | - /* If the checksum doesn't add up, then one or more of the disk sectors |
| 59897 | | - ** containing the super-journal filename is corrupted. This means |
| 59898 | | - ** definitely roll back, so just return SQLITE_OK and report a (nul) |
| 59899 | | - ** super-journal filename. |
| 59900 | | - */ |
| 59901 | | - len = 0; |
| 59902 | | - } |
| 59903 | | - zSuper[len] = '\0'; |
| 59904 | | - zSuper[len+1] = '\0'; |
| 59905 | | - |
| 59906 | | - return SQLITE_OK; |
| 59965 | + zOut = (char*)sqlite3MallocZero(4 + len + 2); |
| 59966 | + if( !zOut ){ |
| 59967 | + rc = SQLITE_NOMEM_BKPT; |
| 59968 | + }else{ |
| 59969 | + zOut = &zOut[4]; |
| 59970 | + if( SQLITE_OK==(rc = sqlite3OsRead(pJrnl, zOut, len, szJ-16-len)) ){ |
| 59971 | + u32 u; /* Unsigned loop counter */ |
| 59972 | + /* See if the checksum matches the super-journal name */ |
| 59973 | + for(u=0; u<len; u++){ |
| 59974 | + cksum -= zOut[u]; |
| 59975 | + } |
| 59976 | + } |
| 59977 | + if( rc!=SQLITE_OK || cksum ){ |
| 59978 | + /* If the checksum doesn't add up, then one or more of the disk sectors |
| 59979 | + ** containing the super-journal filename is corrupted. This means |
| 59980 | + ** definitely roll back, so just return SQLITE_OK and report a (nul) |
| 59981 | + ** super-journal filename. */ |
| 59982 | + freeSuperJournal(zOut); |
| 59983 | + zOut = 0; |
| 59984 | + } |
| 59985 | + } |
| 59986 | + |
| 59987 | + *pzSuper = zOut; |
| 59988 | + return rc; |
| 59907 | 59989 | } |
| 59908 | 59990 | |
| 59909 | 59991 | /* |
| 59910 | 59992 | ** Return the offset of the sector boundary at or immediately |
| 59911 | 59993 | ** following the value in pPager->journalOff, assuming a sector |
| | @@ -61106,13 +61188,11 @@ |
| 61106 | 61188 | sqlite3_file *pSuper; /* Malloc'd super-journal file descriptor */ |
| 61107 | 61189 | sqlite3_file *pJournal; /* Malloc'd child-journal file descriptor */ |
| 61108 | 61190 | char *zSuperJournal = 0; /* Contents of super-journal file */ |
| 61109 | 61191 | i64 nSuperJournal; /* Size of super-journal file */ |
| 61110 | 61192 | char *zJournal; /* Pointer to one journal within MJ file */ |
| 61111 | | - char *zSuperPtr; /* Space to hold super-journal filename */ |
| 61112 | 61193 | char *zFree = 0; /* Free this buffer */ |
| 61113 | | - i64 nSuperPtr; /* Amount of space allocated to zSuperPtr[] */ |
| 61114 | 61194 | |
| 61115 | 61195 | /* Allocate space for both the pJournal and pSuper file descriptors. |
| 61116 | 61196 | ** If successful, open the super-journal file for reading. |
| 61117 | 61197 | */ |
| 61118 | 61198 | pSuper = (sqlite3_file *)sqlite3MallocZero(2 * (i64)pVfs->szOsFile); |
| | @@ -61131,22 +61211,20 @@ |
| 61131 | 61211 | ** sufficient space (in zSuperPtr) to hold the names of super-journal |
| 61132 | 61212 | ** files extracted from regular rollback-journals. |
| 61133 | 61213 | */ |
| 61134 | 61214 | rc = sqlite3OsFileSize(pSuper, &nSuperJournal); |
| 61135 | 61215 | if( rc!=SQLITE_OK ) goto delsuper_out; |
| 61136 | | - nSuperPtr = 1 + (i64)pVfs->mxPathname; |
| 61137 | | - assert( nSuperJournal>=0 && nSuperPtr>0 ); |
| 61138 | | - zFree = sqlite3Malloc(4 + nSuperJournal + 2 + nSuperPtr + 2); |
| 61216 | + assert( nSuperJournal>=0 ); |
| 61217 | + zFree = sqlite3Malloc(4 + nSuperJournal + 2); |
| 61139 | 61218 | if( !zFree ){ |
| 61140 | 61219 | rc = SQLITE_NOMEM_BKPT; |
| 61141 | 61220 | goto delsuper_out; |
| 61142 | 61221 | }else{ |
| 61143 | 61222 | assert( nSuperJournal<=0x7fffffff ); |
| 61144 | 61223 | } |
| 61145 | 61224 | zFree[0] = zFree[1] = zFree[2] = zFree[3] = 0; |
| 61146 | 61225 | zSuperJournal = &zFree[4]; |
| 61147 | | - zSuperPtr = &zSuperJournal[nSuperJournal+2]; |
| 61148 | 61226 | rc = sqlite3OsRead(pSuper, zSuperJournal, (int)nSuperJournal, 0); |
| 61149 | 61227 | if( rc!=SQLITE_OK ) goto delsuper_out; |
| 61150 | 61228 | zSuperJournal[nSuperJournal] = 0; |
| 61151 | 61229 | zSuperJournal[nSuperJournal+1] = 0; |
| 61152 | 61230 | |
| | @@ -61156,10 +61234,12 @@ |
| 61156 | 61234 | rc = sqlite3OsAccess(pVfs, zJournal, SQLITE_ACCESS_EXISTS, &exists); |
| 61157 | 61235 | if( rc!=SQLITE_OK ){ |
| 61158 | 61236 | goto delsuper_out; |
| 61159 | 61237 | } |
| 61160 | 61238 | if( exists ){ |
| 61239 | + char *zSuperPtr = 0; |
| 61240 | + |
| 61161 | 61241 | /* One of the journals pointed to by the super-journal exists. |
| 61162 | 61242 | ** Open it and check if it points at the super-journal. If |
| 61163 | 61243 | ** so, return without deleting the super-journal file. |
| 61164 | 61244 | ** NB: zJournal is really a MAIN_JOURNAL. But call it a |
| 61165 | 61245 | ** SUPER_JOURNAL here so that the VFS will not send the zJournal |
| | @@ -61170,17 +61250,19 @@ |
| 61170 | 61250 | rc = sqlite3OsOpen(pVfs, zJournal, pJournal, flags, 0); |
| 61171 | 61251 | if( rc!=SQLITE_OK ){ |
| 61172 | 61252 | goto delsuper_out; |
| 61173 | 61253 | } |
| 61174 | 61254 | |
| 61175 | | - rc = readSuperJournal(pJournal, zSuperPtr, nSuperPtr); |
| 61255 | + rc = readSuperJournal(pJournal, 1+(u64)pVfs->mxPathname, &zSuperPtr); |
| 61176 | 61256 | sqlite3OsClose(pJournal); |
| 61177 | 61257 | if( rc!=SQLITE_OK ){ |
| 61258 | + assert( zSuperPtr==0 ); |
| 61178 | 61259 | goto delsuper_out; |
| 61179 | 61260 | } |
| 61180 | 61261 | |
| 61181 | | - c = zSuperPtr[0]!=0 && strcmp(zSuperPtr, zSuper)==0; |
| 61262 | + c = zSuperPtr!=0 && strcmp(zSuperPtr, zSuper)==0; |
| 61263 | + freeSuperJournal(zSuperPtr); |
| 61182 | 61264 | if( c ){ |
| 61183 | 61265 | /* We have a match. Do not delete the super-journal file. */ |
| 61184 | 61266 | goto delsuper_out; |
| 61185 | 61267 | } |
| 61186 | 61268 | } |
| | @@ -61391,23 +61473,15 @@ |
| 61391 | 61473 | |
| 61392 | 61474 | /* Read the super-journal name from the journal, if it is present. |
| 61393 | 61475 | ** If a super-journal file name is specified, but the file is not |
| 61394 | 61476 | ** present on disk, then the journal is not hot and does not need to be |
| 61395 | 61477 | ** played back. |
| 61396 | | - ** |
| 61397 | | - ** TODO: Technically the following is an error because it assumes that |
| 61398 | | - ** buffer Pager.pTmpSpace is (mxPathname+1) bytes or larger. i.e. that |
| 61399 | | - ** ((pPager->pageSize+8) >= pPager->pVfs->mxPathname+1). Using os_unix.c, |
| 61400 | | - ** mxPathname is 512, which is the same as the minimum allowable value |
| 61401 | | - ** for pageSize, and so this assumption holds. But it might not for some |
| 61402 | | - ** custom VFS. */ |
| 61403 | | - zSuper = pPager->pTmpSpace; |
| 61404 | | - rc = readSuperJournal(pPager->jfd, zSuper, 1+(i64)pPager->pVfs->mxPathname); |
| 61405 | | - if( rc==SQLITE_OK && zSuper[0] ){ |
| 61478 | + */ |
| 61479 | + rc = readSuperJournal(pPager->jfd, 1+(i64)pPager->pVfs->mxPathname, &zSuper); |
| 61480 | + if( rc==SQLITE_OK && zSuper ){ |
| 61406 | 61481 | rc = sqlite3OsAccess(pVfs, zSuper, SQLITE_ACCESS_EXISTS, &res); |
| 61407 | 61482 | } |
| 61408 | | - zSuper = 0; |
| 61409 | 61483 | if( rc!=SQLITE_OK || !res ){ |
| 61410 | 61484 | goto end_playback; |
| 61411 | 61485 | } |
| 61412 | 61486 | pPager->journalOff = 0; |
| 61413 | 61487 | needPagerReset = isHot; |
| | @@ -61532,34 +61606,24 @@ |
| 61532 | 61606 | ** problems for other processes at some point in the future. So, just |
| 61533 | 61607 | ** in case this has happened, clear the changeCountDone flag now. |
| 61534 | 61608 | */ |
| 61535 | 61609 | pPager->changeCountDone = pPager->tempFile; |
| 61536 | 61610 | |
| 61537 | | - if( rc==SQLITE_OK ){ |
| 61538 | | - /* Leave 4 bytes of space before the super-journal filename in memory. |
| 61539 | | - ** This is because it may end up being passed to sqlite3OsOpen(), in |
| 61540 | | - ** which case it requires 4 0x00 bytes in memory immediately before |
| 61541 | | - ** the filename. */ |
| 61542 | | - zSuper = &pPager->pTmpSpace[4]; |
| 61543 | | - rc = readSuperJournal(pPager->jfd, zSuper, 1+(i64)pPager->pVfs->mxPathname); |
| 61544 | | - testcase( rc!=SQLITE_OK ); |
| 61545 | | - } |
| 61546 | 61611 | if( rc==SQLITE_OK |
| 61547 | 61612 | && (pPager->eState>=PAGER_WRITER_DBMOD || pPager->eState==PAGER_OPEN) |
| 61548 | 61613 | ){ |
| 61549 | 61614 | rc = sqlite3PagerSync(pPager, 0); |
| 61550 | 61615 | } |
| 61551 | 61616 | if( rc==SQLITE_OK ){ |
| 61552 | | - rc = pager_end_transaction(pPager, zSuper[0]!='\0', 0); |
| 61617 | + rc = pager_end_transaction(pPager, zSuper!=0, 0); |
| 61553 | 61618 | testcase( rc!=SQLITE_OK ); |
| 61554 | 61619 | } |
| 61555 | | - if( rc==SQLITE_OK && zSuper[0] && res ){ |
| 61620 | + if( rc==SQLITE_OK && zSuper && res ){ |
| 61556 | 61621 | /* If there was a super-journal and this routine will return success, |
| 61557 | 61622 | ** see if it is possible to delete the super-journal. |
| 61558 | 61623 | */ |
| 61559 | | - assert( zSuper==&pPager->pTmpSpace[4] ); |
| 61560 | | - memset(pPager->pTmpSpace, 0, 4); |
| 61624 | + assert( memcmp(&zSuper[-4], "\0\0\0\0", 4)==0 ); |
| 61561 | 61625 | rc = pager_delsuper(pPager, zSuper); |
| 61562 | 61626 | testcase( rc!=SQLITE_OK ); |
| 61563 | 61627 | } |
| 61564 | 61628 | if( isHot && nPlayback ){ |
| 61565 | 61629 | sqlite3_log(SQLITE_NOTICE_RECOVER_ROLLBACK, "recovered %d pages from %s", |
| | @@ -61568,10 +61632,11 @@ |
| 61568 | 61632 | |
| 61569 | 61633 | /* The Pager.sectorSize variable may have been updated while rolling |
| 61570 | 61634 | ** back a journal created by a process with a different sector size |
| 61571 | 61635 | ** value. Reset it to the correct value for this process. |
| 61572 | 61636 | */ |
| 61637 | + freeSuperJournal(zSuper); |
| 61573 | 61638 | setSectorSize(pPager); |
| 61574 | 61639 | return rc; |
| 61575 | 61640 | } |
| 61576 | 61641 | |
| 61577 | 61642 | |
| | @@ -67426,10 +67491,16 @@ |
| 67426 | 67491 | */ |
| 67427 | 67492 | pgno = sqlite3Get4byte(&aFrame[0]); |
| 67428 | 67493 | if( pgno==0 ){ |
| 67429 | 67494 | return 0; |
| 67430 | 67495 | } |
| 67496 | + |
| 67497 | + /* Need a valid page size |
| 67498 | + */ |
| 67499 | + if( !pWal->szPage ){ |
| 67500 | + return 0; |
| 67501 | + } |
| 67431 | 67502 | |
| 67432 | 67503 | /* A frame is only valid if a checksum of the WAL header, |
| 67433 | 67504 | ** all prior frames, the first 16 bytes of this frame-header, |
| 67434 | 67505 | ** and the frame-data matches the checksum in the last 8 |
| 67435 | 67506 | ** bytes of this frame-header. |
| | @@ -69281,11 +69352,11 @@ |
| 69281 | 69352 | goto begin_unreliable_shm_out; |
| 69282 | 69353 | } |
| 69283 | 69354 | |
| 69284 | 69355 | /* Allocate a buffer to read frames into */ |
| 69285 | 69356 | assert( (pWal->szPage & (pWal->szPage-1))==0 ); |
| 69286 | | - assert( pWal->szPage>=512 && pWal->szPage<=65536 ); |
| 69357 | + assert( (pWal->szPage>=512 && pWal->szPage<=65536) || pWal->szPage==0 ); |
| 69287 | 69358 | szFrame = pWal->szPage + WAL_FRAME_HDRSIZE; |
| 69288 | 69359 | aFrame = (u8 *)sqlite3_malloc64(szFrame); |
| 69289 | 69360 | if( aFrame==0 ){ |
| 69290 | 69361 | rc = SQLITE_NOMEM_BKPT; |
| 69291 | 69362 | goto begin_unreliable_shm_out; |
| | @@ -83086,10 +83157,15 @@ |
| 83086 | 83157 | if( pc+info.nSize>usableSize ){ |
| 83087 | 83158 | checkAppendMsg(pCheck, "Extends off end of page"); |
| 83088 | 83159 | doCoverageCheck = 0; |
| 83089 | 83160 | continue; |
| 83090 | 83161 | } |
| 83162 | + if( info.nPayload && info.pPayload[0]<2 ){ |
| 83163 | + checkAppendMsg(pCheck, "Bad cell header size"); |
| 83164 | + doCoverageCheck = 0; |
| 83165 | + continue; |
| 83166 | + } |
| 83091 | 83167 | |
| 83092 | 83168 | /* Check for integer primary key out of range */ |
| 83093 | 83169 | if( pPage->intKey ){ |
| 83094 | 83170 | if( keyCanBeEqual ? (info.nKey > maxKey) : (info.nKey >= maxKey) ){ |
| 83095 | 83171 | checkAppendMsg(pCheck, "Rowid %lld out of order", info.nKey); |
| | @@ -84596,12 +84672,12 @@ |
| 84596 | 84672 | /* Work-around for GCC bug or bugs: |
| 84597 | 84673 | ** https://gcc.gnu.org/bugzilla/show_bug.cgi?id=96270 |
| 84598 | 84674 | ** https://gcc.gnu.org/bugzilla/show_bug.cgi?id=114659 |
| 84599 | 84675 | ** The problem appears to be fixed in GCC 15 */ |
| 84600 | 84676 | i64 x; |
| 84601 | | - assert( (MEM_Str&~p->flags)*4==sizeof(x) ); |
| 84602 | | - memcpy(&x, (char*)&p->u, (MEM_Str&~p->flags)*4); |
| 84677 | + assert( (sqlite3Config.bSmallMalloc!=0xee)*8==sizeof(x) ); |
| 84678 | + memcpy(&x, (char*)&p->u.i, (sqlite3Config.bSmallMalloc!=0xee)*8); |
| 84603 | 84679 | p->n = sqlite3Int64ToText(x, zBuf); |
| 84604 | 84680 | #else |
| 84605 | 84681 | p->n = sqlite3Int64ToText(p->u.i, zBuf); |
| 84606 | 84682 | #endif |
| 84607 | 84683 | if( p->flags & MEM_IntReal ){ |
| | @@ -95448,11 +95524,11 @@ |
| 95448 | 95524 | QueryPerformanceCounter(&tm); |
| 95449 | 95525 | return (sqlite3_uint64)tm.QuadPart; |
| 95450 | 95526 | } |
| 95451 | 95527 | |
| 95452 | 95528 | #elif !defined(__STRICT_ANSI__) && defined(__GNUC__) && \ |
| 95453 | | - (defined(i386) || defined(__i386__) || defined(_M_IX86)) |
| 95529 | + (defined(i586) || defined(__i586__) || defined(_M_IX86)) |
| 95454 | 95530 | |
| 95455 | 95531 | __inline__ sqlite_uint64 sqlite3Hwtime(void){ |
| 95456 | 95532 | unsigned int lo, hi; |
| 95457 | 95533 | __asm__ __volatile__ ("rdtsc" : "=a" (lo), "=d" (hi)); |
| 95458 | 95534 | return (sqlite_uint64)hi << 32 | lo; |
| | @@ -103037,11 +103113,11 @@ |
| 103037 | 103113 | if( pFrame ) break; |
| 103038 | 103114 | } |
| 103039 | 103115 | |
| 103040 | 103116 | if( p->nFrame>=db->aLimit[SQLITE_LIMIT_TRIGGER_DEPTH] ){ |
| 103041 | 103117 | rc = SQLITE_ERROR; |
| 103042 | | - sqlite3VdbeError(p, "too many levels of trigger recursion"); |
| 103118 | + sqlite3VdbeError(p, "triggers nested too deep"); |
| 103043 | 103119 | goto abort_due_to_error; |
| 103044 | 103120 | } |
| 103045 | 103121 | |
| 103046 | 103122 | /* Register pRt is used to store the memory required to save the state |
| 103047 | 103123 | ** of the current program, and the memory required at runtime to execute |
| | @@ -110164,11 +110240,11 @@ |
| 110164 | 110240 | /* |
| 110165 | 110241 | ** cnt==0 means there was not match. |
| 110166 | 110242 | ** cnt>1 means there were two or more matches. |
| 110167 | 110243 | ** |
| 110168 | 110244 | ** cnt==0 is always an error. cnt>1 is often an error, but might |
| 110169 | | - ** be multiple matches for a NATURAL LEFT JOIN or a LEFT JOIN USING. |
| 110245 | + ** be multiple matches for a NATURAL OUTER JOIN or a OUTER JOIN USING. |
| 110170 | 110246 | */ |
| 110171 | 110247 | assert( pFJMatch==0 || cnt>0 ); |
| 110172 | 110248 | assert( !ExprHasProperty(pExpr, EP_xIsSelect|EP_IntValue) ); |
| 110173 | 110249 | if( cnt!=1 ){ |
| 110174 | 110250 | const char *zErr; |
| | @@ -110247,12 +110323,21 @@ |
| 110247 | 110323 | pExpr->op = eNewExprOp; |
| 110248 | 110324 | lookupname_end: |
| 110249 | 110325 | if( cnt==1 ){ |
| 110250 | 110326 | assert( pNC!=0 ); |
| 110251 | 110327 | #ifndef SQLITE_OMIT_AUTHORIZATION |
| 110252 | | - if( db->xAuth && (pExpr->op==TK_COLUMN || pExpr->op==TK_TRIGGER) ){ |
| 110253 | | - sqlite3AuthRead(pParse, pExpr, pSchema, pNC->pSrcList); |
| 110328 | + if( db->xAuth ){ |
| 110329 | + if( pFJMatch ){ |
| 110330 | + assert( pExpr->op==TK_FUNCTION ); |
| 110331 | + assert( sqlite3_stricmp(pExpr->u.zToken,"coalesce")==0 ); |
| 110332 | + assert( pExpr->x.pList==pFJMatch ); |
| 110333 | + assert( pFJMatch->nExpr>0 ); |
| 110334 | + pExpr = pFJMatch->a[0].pExpr; |
| 110335 | + } |
| 110336 | + if( pExpr->op==TK_COLUMN || pExpr->op==TK_TRIGGER ){ |
| 110337 | + sqlite3AuthRead(pParse, pExpr, pSchema, pNC->pSrcList); |
| 110338 | + } |
| 110254 | 110339 | } |
| 110255 | 110340 | #endif |
| 110256 | 110341 | /* Increment the nRef value on all name contexts from TopNC up to |
| 110257 | 110342 | ** the point where the name matched. */ |
| 110258 | 110343 | for(;;){ |
| | @@ -115930,10 +116015,11 @@ |
| 115930 | 116015 | if( i!=nVector ){ |
| 115931 | 116016 | /* Need to reorder the LHS fields according to aiMap */ |
| 115932 | 116017 | int rLhsOrig = rLhs; |
| 115933 | 116018 | rLhs = sqlite3GetTempRange(pParse, nVector); |
| 115934 | 116019 | for(i=0; i<nVector; i++){ |
| 116020 | + testcase( aiMap[i]!=i ); |
| 115935 | 116021 | sqlite3VdbeAddOp3(v, OP_Copy, rLhsOrig+i, rLhs+aiMap[i], 0); |
| 115936 | 116022 | } |
| 115937 | 116023 | sqlite3ReleaseTempReg(pParse, rLhsOrig); |
| 115938 | 116024 | } |
| 115939 | 116025 | } |
| | @@ -115950,11 +116036,12 @@ |
| 115950 | 116036 | } |
| 115951 | 116037 | for(i=0; i<nVector; i++){ |
| 115952 | 116038 | Expr *p = sqlite3VectorFieldSubexpr(pExpr->pLeft, i); |
| 115953 | 116039 | if( pParse->nErr ) goto sqlite3ExprCodeIN_oom_error; |
| 115954 | 116040 | if( sqlite3ExprCanBeNull(p) ){ |
| 115955 | | - sqlite3VdbeAddOp2(v, OP_IsNull, rLhs+i, destStep2); |
| 116041 | + testcase( aiMap[i]!=i ); |
| 116042 | + sqlite3VdbeAddOp2(v, OP_IsNull, rLhs+aiMap[i], destStep2); |
| 115956 | 116043 | VdbeCoverage(v); |
| 115957 | 116044 | } |
| 115958 | 116045 | } |
| 115959 | 116046 | |
| 115960 | 116047 | /* Step 3. The LHS is now known to be non-NULL. Do the binary search |
| | @@ -116024,13 +116111,23 @@ |
| 116024 | 116111 | for(i=0; i<nVector; i++){ |
| 116025 | 116112 | Expr *p; |
| 116026 | 116113 | CollSeq *pColl; |
| 116027 | 116114 | int r3 = sqlite3GetTempReg(pParse); |
| 116028 | 116115 | p = sqlite3VectorFieldSubexpr(pLeft, i); |
| 116029 | | - pColl = sqlite3ExprCollSeq(pParse, p); |
| 116030 | | - sqlite3VdbeAddOp3(v, OP_Column, iTab, i, r3); |
| 116031 | | - sqlite3VdbeAddOp4(v, OP_Ne, rLhs+i, destNotNull, r3, |
| 116116 | + if( ExprUseXSelect(pExpr) ){ |
| 116117 | + Expr *pRhs = pExpr->x.pSelect->pEList->a[i].pExpr; |
| 116118 | + pColl = sqlite3BinaryCompareCollSeq(pParse, p, pRhs); |
| 116119 | + }else{ |
| 116120 | + /* If the RHS of the IN(...) expression are scalar expressions, do |
| 116121 | + ** not consider their collation sequences. The documentation says |
| 116122 | + ** "The collating sequence used for expressions of the form "x IN (y, z, |
| 116123 | + ** ...)" is the collating sequence of x.". */ |
| 116124 | + pColl = sqlite3ExprCollSeq(pParse, p); |
| 116125 | + } |
| 116126 | + testcase( aiMap[i]!=i ); |
| 116127 | + sqlite3VdbeAddOp3(v, OP_Column, iTab, aiMap[i], r3); |
| 116128 | + sqlite3VdbeAddOp4(v, OP_Ne, rLhs+aiMap[i], destNotNull, r3, |
| 116032 | 116129 | (void*)pColl, P4_COLLSEQ); |
| 116033 | 116130 | VdbeCoverage(v); |
| 116034 | 116131 | sqlite3ReleaseTempReg(pParse, r3); |
| 116035 | 116132 | } |
| 116036 | 116133 | sqlite3VdbeAddOp2(v, OP_Goto, 0, destIfNull); |
| | @@ -124365,21 +124462,21 @@ |
| 124365 | 124462 | }else{ |
| 124366 | 124463 | nIdxCol = pIdx->nColumn; |
| 124367 | 124464 | } |
| 124368 | 124465 | pIdx->nSampleCol = nIdxCol; |
| 124369 | 124466 | pIdx->mxSample = nSample; |
| 124370 | | - nByte = ROUND8(sizeof(IndexSample) * nSample); |
| 124371 | | - nByte += sizeof(tRowcnt) * nIdxCol * 3 * nSample; |
| 124372 | | - nByte += nIdxCol * sizeof(tRowcnt); /* Space for Index.aAvgEq[] */ |
| 124467 | + nByte = ROUND8(sizeof64(IndexSample) * nSample); |
| 124468 | + nByte += sizeof64(tRowcnt) * nIdxCol * 3 * nSample; |
| 124469 | + nByte += nIdxCol * sizeof64(tRowcnt); /* Space for Index.aAvgEq[] */ |
| 124373 | 124470 | |
| 124374 | 124471 | pIdx->aSample = sqlite3DbMallocZero(db, nByte); |
| 124375 | 124472 | if( pIdx->aSample==0 ){ |
| 124376 | 124473 | sqlite3_finalize(pStmt); |
| 124377 | 124474 | return SQLITE_NOMEM_BKPT; |
| 124378 | 124475 | } |
| 124379 | 124476 | pPtr = (u8*)pIdx->aSample; |
| 124380 | | - pPtr += ROUND8(nSample*sizeof(pIdx->aSample[0])); |
| 124477 | + pPtr += ROUND8(nSample*sizeof64(pIdx->aSample[0])); |
| 124381 | 124478 | pSpace = (tRowcnt*)pPtr; |
| 124382 | 124479 | assert( EIGHT_BYTE_ALIGNMENT( pSpace ) ); |
| 124383 | 124480 | pIdx->aAvgEq = pSpace; pSpace += nIdxCol; |
| 124384 | 124481 | pIdx->pTable->tabFlags |= TF_HasStat4; |
| 124385 | 124482 | for(i=0; i<nSample; i++){ |
| | @@ -133231,13 +133328,22 @@ |
| 133231 | 133328 | x.nUsed = 0; |
| 133232 | 133329 | x.apArg = argv+1; |
| 133233 | 133330 | sqlite3StrAccumInit(&str, db, 0, 0, db->aLimit[SQLITE_LIMIT_LENGTH]); |
| 133234 | 133331 | str.printfFlags = SQLITE_PRINTF_SQLFUNC; |
| 133235 | 133332 | sqlite3_str_appendf(&str, zFormat, &x); |
| 133236 | | - n = str.nChar; |
| 133237 | | - sqlite3_result_text(context, sqlite3StrAccumFinish(&str), n, |
| 133238 | | - SQLITE_DYNAMIC); |
| 133333 | + if( str.accError==SQLITE_OK ){ |
| 133334 | + n = str.nChar; |
| 133335 | + sqlite3_result_text(context, sqlite3StrAccumFinish(&str), n, |
| 133336 | + SQLITE_DYNAMIC); |
| 133337 | + }else{ |
| 133338 | + if( str.accError==SQLITE_NOMEM ){ |
| 133339 | + sqlite3_result_error_nomem(context); |
| 133340 | + }else{ |
| 133341 | + sqlite3_result_error_toobig(context); |
| 133342 | + } |
| 133343 | + sqlite3_str_reset(&str); |
| 133344 | + } |
| 133239 | 133345 | } |
| 133240 | 133346 | } |
| 133241 | 133347 | |
| 133242 | 133348 | /* |
| 133243 | 133349 | ** Implementation of the substr() function. |
| | @@ -134405,11 +134511,11 @@ |
| 134405 | 134511 | int nStr; /* Size of zStr */ |
| 134406 | 134512 | int nPattern; /* Size of zPattern */ |
| 134407 | 134513 | int nRep; /* Size of zRep */ |
| 134408 | 134514 | i64 nOut; /* Maximum size of zOut */ |
| 134409 | 134515 | int loopLimit; /* Last zStr[] that might match zPattern[] */ |
| 134410 | | - int i, j; /* Loop counters */ |
| 134516 | + i64 i, j; /* Loop counters */ |
| 134411 | 134517 | unsigned cntExpand; /* Number zOut expansions */ |
| 134412 | 134518 | sqlite3 *db = sqlite3_context_db_handle(context); |
| 134413 | 134519 | |
| 134414 | 134520 | assert( argc==3 ); |
| 134415 | 134521 | UNUSED_PARAMETER(argc); |
| | @@ -135859,57 +135965,95 @@ |
| 135859 | 135965 | ** from the standard library because: |
| 135860 | 135966 | ** |
| 135861 | 135967 | ** (1) To avoid a dependency on qsort() |
| 135862 | 135968 | ** (2) To avoid the function call to the comparison routine for each |
| 135863 | 135969 | ** comparison. |
| 135970 | +** |
| 135971 | +** If parameter iReq is non-negative, then the caller will only access |
| 135972 | +** elements a[iReq] and a[iReq+1] (if it exists) of the sorted array and |
| 135973 | +** so it is not necessary to position any other elements. Or if iReq is |
| 135974 | +** negative, then the final array must be fully sorted. |
| 135864 | 135975 | */ |
| 135865 | | -static void percentSort(double *a, unsigned int n){ |
| 135976 | +static void percentSort( |
| 135977 | + double *a, /* Array to sort */ |
| 135978 | + unsigned int n, /* Number of elements in array a[] */ |
| 135979 | + int iReq /* Element caller cares about (or -ve) */ |
| 135980 | +){ |
| 135866 | 135981 | int iLt; /* Entries before a[iLt] are less than rPivot */ |
| 135867 | 135982 | int iGt; /* Entries at or after a[iGt] are greater than rPivot */ |
| 135868 | 135983 | int i; /* Loop counter */ |
| 135869 | 135984 | double rPivot; /* The pivot value */ |
| 135870 | 135985 | |
| 135871 | 135986 | assert( n>=2 ); |
| 135872 | | - if( a[0]>a[n-1] ){ |
| 135873 | | - SWAP_DOUBLE(a[0],a[n-1]) |
| 135874 | | - } |
| 135875 | | - if( n==2 ) return; |
| 135876 | | - iGt = n-1; |
| 135877 | | - i = n/2; |
| 135878 | | - if( a[0]>a[i] ){ |
| 135879 | | - SWAP_DOUBLE(a[0],a[i]) |
| 135880 | | - }else if( a[i]>a[iGt] ){ |
| 135881 | | - SWAP_DOUBLE(a[i],a[iGt]) |
| 135882 | | - } |
| 135883 | | - if( n==3 ) return; |
| 135884 | | - rPivot = a[i]; |
| 135885 | | - iLt = i = 1; |
| 135886 | | - do{ |
| 135887 | | - if( a[i]<rPivot ){ |
| 135888 | | - if( i>iLt ) SWAP_DOUBLE(a[i],a[iLt]) |
| 135889 | | - iLt++; |
| 135890 | | - i++; |
| 135891 | | - }else if( a[i]>rPivot ){ |
| 135892 | | - do{ |
| 135893 | | - iGt--; |
| 135894 | | - }while( iGt>i && a[iGt]>rPivot ); |
| 135895 | | - SWAP_DOUBLE(a[i],a[iGt]) |
| 135896 | | - }else{ |
| 135897 | | - i++; |
| 135898 | | - } |
| 135899 | | - }while( i<iGt ); |
| 135900 | | - if( iLt>=2 ) percentSort(a, iLt); |
| 135901 | | - if( n-iGt>=2 ) percentSort(a+iGt, n-iGt); |
| 135902 | | - |
| 135903 | | -/* Uncomment for testing */ |
| 135904 | | -#if 0 |
| 135905 | | - for(i=0; i<n-1; i++){ |
| 135906 | | - assert( a[i]<=a[i+1] ); |
| 135907 | | - } |
| 135908 | | -#endif |
| 135909 | | -} |
| 135910 | | - |
| 135987 | + do{ |
| 135988 | + if( a[0]>a[n-1] ){ |
| 135989 | + SWAP_DOUBLE(a[0],a[n-1]) |
| 135990 | + } |
| 135991 | + if( n==2 ) return; |
| 135992 | + iGt = n-1; |
| 135993 | + i = n/2; |
| 135994 | + if( a[0]>a[i] ){ |
| 135995 | + SWAP_DOUBLE(a[0],a[i]) |
| 135996 | + }else if( a[i]>a[iGt] ){ |
| 135997 | + SWAP_DOUBLE(a[i],a[iGt]) |
| 135998 | + } |
| 135999 | + if( n==3 ) return; |
| 136000 | + rPivot = a[i]; |
| 136001 | + iLt = i = 1; |
| 136002 | + do{ |
| 136003 | + if( a[i]<rPivot ){ |
| 136004 | + if( i>iLt ) SWAP_DOUBLE(a[i],a[iLt]) |
| 136005 | + iLt++; |
| 136006 | + i++; |
| 136007 | + }else if( a[i]>rPivot ){ |
| 136008 | + do{ |
| 136009 | + iGt--; |
| 136010 | + }while( iGt>i && a[iGt]>rPivot ); |
| 136011 | + SWAP_DOUBLE(a[i],a[iGt]) |
| 136012 | + }else{ |
| 136013 | + i++; |
| 136014 | + } |
| 136015 | + }while( i<iGt ); |
| 136016 | + |
| 136017 | + assert( a[iLt]==rPivot ); |
| 136018 | + assert( iGt>iLt ); |
| 136019 | + |
| 136020 | + if( iReq>=0 ){ |
| 136021 | + /* In this case, the only elements that the caller requires sorted into |
| 136022 | + ** the correct positions are elements a[iReq] and a[iReq+1]. At this |
| 136023 | + ** point we know that element a[iLt] is in the correct position and |
| 136024 | + ** all elements smaller than a[iLt] are in the left-hand partition. |
| 136025 | + ** So if (iReq<iLt), then it is only necessary to sort the left |
| 136026 | + ** partition. |
| 136027 | + ** |
| 136028 | + ** If (iReq>=iLt), then elements iReq and iReq+1 are either in the |
| 136029 | + ** right partition or the equal partition (elements for which |
| 136030 | + ** iLt<=iElem<iGt). Therefore it is always sufficient to sort only |
| 136031 | + ** the right partition in this case. */ |
| 136032 | + if( iReq<iLt ){ |
| 136033 | + n = iLt; |
| 136034 | + }else{ |
| 136035 | + a += iGt; |
| 136036 | + n -= iGt; |
| 136037 | + iReq = MAX(0, iReq-iGt); |
| 136038 | + } |
| 136039 | + }else{ |
| 136040 | + /* Recurse on the smaller partition only. The smaller partition |
| 136041 | + ** will hold n/2 or fewer entries, which assures that the stack |
| 136042 | + ** depth will not exceed O(log(n)), even for pathological cases. |
| 136043 | + ** Loop without recursion for the larger partition. */ |
| 136044 | + if( iLt>(int)(n/2) ){ |
| 136045 | + if( n-iGt>=2 ) percentSort(a+iGt, n-iGt, -1); |
| 136046 | + n = iLt; |
| 136047 | + }else{ |
| 136048 | + if( iLt>=2 ) percentSort(a, iLt, -1); |
| 136049 | + a += iGt; |
| 136050 | + n -= iGt; |
| 136051 | + } |
| 136052 | + } |
| 136053 | + }while( n>=2 ); |
| 136054 | +} |
| 135911 | 136055 | |
| 135912 | 136056 | /* |
| 135913 | 136057 | ** The "inverse" function for percentile(Y,P) is called to remove a |
| 135914 | 136058 | ** row that was previously inserted by "step". |
| 135915 | 136059 | */ |
| | @@ -135939,11 +136083,11 @@ |
| 135939 | 136083 | if( percentIsInfinity(y) ){ |
| 135940 | 136084 | return; |
| 135941 | 136085 | } |
| 135942 | 136086 | if( p->bSorted==0 ){ |
| 135943 | 136087 | assert( p->nUsed>1 ); |
| 135944 | | - percentSort(p->a, p->nUsed); |
| 136088 | + percentSort(p->a, p->nUsed, -1); |
| 135945 | 136089 | p->bSorted = 1; |
| 135946 | 136090 | } |
| 135947 | 136091 | p->bKeepSorted = 1; |
| 135948 | 136092 | |
| 135949 | 136093 | /* Find and remove the row */ |
| | @@ -135968,17 +136112,21 @@ |
| 135968 | 136112 | double ix, vx; |
| 135969 | 136113 | p = (Percentile*)sqlite3_aggregate_context(pCtx, 0); |
| 135970 | 136114 | if( p==0 ) return; |
| 135971 | 136115 | if( p->a==0 ) return; |
| 135972 | 136116 | if( p->nUsed ){ |
| 136117 | + ix = p->rPct*(p->nUsed-1); |
| 136118 | + i1 = (unsigned)ix; |
| 135973 | 136119 | if( p->bSorted==0 ){ |
| 136120 | + /* In cases where bIsFinal is non-zero, setting Percentile.bSorted |
| 136121 | + ** after the percentSort() call here is not technically correct, as |
| 136122 | + ** the array is not fully sorted. But in this case the object will be |
| 136123 | + ** freed below anyway, so it doesn't matter. */ |
| 135974 | 136124 | assert( p->nUsed>1 ); |
| 135975 | | - percentSort(p->a, p->nUsed); |
| 136125 | + percentSort(p->a, p->nUsed, (bIsFinal ? (int)i1 : -1)); |
| 135976 | 136126 | p->bSorted = 1; |
| 135977 | 136127 | } |
| 135978 | | - ix = p->rPct*(p->nUsed-1); |
| 135979 | | - i1 = (unsigned)ix; |
| 135980 | 136128 | if( settings & 1 ){ |
| 135981 | 136129 | vx = p->a[i1]; |
| 135982 | 136130 | }else{ |
| 135983 | 136131 | i2 = ix==(double)i1 || i1==p->nUsed-1 ? i1 : i1+1; |
| 135984 | 136132 | v1 = p->a[i1]; |
| | @@ -150488,10 +150636,17 @@ |
| 150488 | 150636 | SQLITE_PRIVATE Table *sqlite3ResultSetOfSelect(Parse *pParse, Select *pSelect, char aff){ |
| 150489 | 150637 | Table *pTab; |
| 150490 | 150638 | sqlite3 *db = pParse->db; |
| 150491 | 150639 | u64 savedFlags; |
| 150492 | 150640 | |
| 150641 | + pParse->nNestSel++; |
| 150642 | +#if SQLITE_MAX_EXPR_DEPTH>0 |
| 150643 | + if( pParse->nNestSel >= db->aLimit[SQLITE_LIMIT_EXPR_DEPTH] ){ |
| 150644 | + sqlite3ErrorMsg(pParse, "VIEWs and/or subqueries nested too deep"); |
| 150645 | + return 0; |
| 150646 | + } |
| 150647 | +#endif |
| 150493 | 150648 | savedFlags = db->flags; |
| 150494 | 150649 | db->flags &= ~(u64)SQLITE_FullColNames; |
| 150495 | 150650 | db->flags |= SQLITE_ShortColNames; |
| 150496 | 150651 | sqlite3SelectPrep(pParse, pSelect, 0); |
| 150497 | 150652 | db->flags = savedFlags; |
| | @@ -150509,10 +150664,12 @@ |
| 150509 | 150664 | pTab->iPKey = -1; |
| 150510 | 150665 | if( db->mallocFailed ){ |
| 150511 | 150666 | sqlite3DeleteTable(db, pTab); |
| 150512 | 150667 | return 0; |
| 150513 | 150668 | } |
| 150669 | + pParse->nNestSel--; |
| 150670 | + assert( pParse->nNestSel>=0 ); |
| 150514 | 150671 | return pTab; |
| 150515 | 150672 | } |
| 150516 | 150673 | |
| 150517 | 150674 | /* |
| 150518 | 150675 | ** Get a VDBE for the given parser context. Create a new one if necessary. |
| | @@ -155479,12 +155636,15 @@ |
| 155479 | 155636 | ** Then, if CheckOnCtx.iJoin indicates that this expression is part of an |
| 155480 | 155637 | ** ON clause from that SrcList (i.e. if iJoin is non-zero), check that it |
| 155481 | 155638 | ** does not refer to a table to the right of CheckOnCtx.iJoin. */ |
| 155482 | 155639 | do { |
| 155483 | 155640 | SrcList *pSrc = pCtx->pSrc; |
| 155641 | + int nSrc = pSrc->nSrc; |
| 155484 | 155642 | int iTab = pExpr->iTable; |
| 155485 | | - if( iTab>=pSrc->a[0].iCursor && iTab<=pSrc->a[pSrc->nSrc-1].iCursor ){ |
| 155643 | + int ii; |
| 155644 | + for(ii=0; ii<nSrc && pSrc->a[ii].iCursor!=iTab; ii++){} |
| 155645 | + if( ii<nSrc ){ |
| 155486 | 155646 | if( pCtx->iJoin && iTab>pCtx->iJoin ){ |
| 155487 | 155647 | sqlite3ErrorMsg(pWalker->pParse, |
| 155488 | 155648 | "%s references tables to its right", |
| 155489 | 155649 | (pCtx->bFuncArg ? "table-function argument" : "ON clause") |
| 155490 | 155650 | ); |
| | @@ -158449,22 +158609,36 @@ |
| 158449 | 158609 | Parse *pParse, /* Current parse context */ |
| 158450 | 158610 | Trigger *pTrigger, /* Trigger to code */ |
| 158451 | 158611 | Table *pTab, /* The table pTrigger is attached to */ |
| 158452 | 158612 | int orconf /* ON CONFLICT policy to code trigger program with */ |
| 158453 | 158613 | ){ |
| 158454 | | - Parse *pTop = sqlite3ParseToplevel(pParse); |
| 158614 | + Parse *pTop; /* Top level Parse object */ |
| 158455 | 158615 | sqlite3 *db = pParse->db; /* Database handle */ |
| 158456 | 158616 | TriggerPrg *pPrg; /* Value to return */ |
| 158457 | 158617 | Expr *pWhen = 0; /* Duplicate of trigger WHEN expression */ |
| 158458 | 158618 | Vdbe *v; /* Temporary VM */ |
| 158459 | 158619 | NameContext sNC; /* Name context for sub-vdbe */ |
| 158460 | 158620 | SubProgram *pProgram = 0; /* Sub-vdbe for trigger program */ |
| 158461 | 158621 | int iEndTrigger = 0; /* Label to jump to if WHEN is false */ |
| 158462 | 158622 | Parse sSubParse; /* Parse context for sub-vdbe */ |
| 158623 | + int nDepth; /* Trigger depth */ |
| 158463 | 158624 | |
| 158625 | + /* Ensure that triggers are not chained too deep. This test is linear |
| 158626 | + ** in the chaining depth, but sensible code ought not be chaining |
| 158627 | + ** triggers excessively, so that shouldn't be a problem. |
| 158628 | + */ |
| 158629 | + pTop = pParse; |
| 158630 | + for(nDepth=0; pTop->pOuterParse; pTop = pTop->pOuterParse, nDepth++){} |
| 158631 | + if( nDepth>=db->aLimit[SQLITE_LIMIT_TRIGGER_DEPTH] ){ |
| 158632 | + sqlite3ErrorMsg(pParse, "triggers nested too deep"); |
| 158633 | + return 0; |
| 158634 | + } |
| 158635 | + |
| 158636 | + pTop = sqlite3ParseToplevel(pParse); |
| 158464 | 158637 | assert( pTrigger->zName==0 || pTab==tableOfTrigger(pTrigger) ); |
| 158465 | 158638 | assert( pTop->pVdbe ); |
| 158639 | + |
| 158466 | 158640 | |
| 158467 | 158641 | /* Allocate the TriggerPrg and SubProgram objects. To ensure that they |
| 158468 | 158642 | ** are freed if an error occurs, link them into the Parse.pTriggerPrg |
| 158469 | 158643 | ** list of the top-level Parse object sooner rather than later. */ |
| 158470 | 158644 | pPrg = sqlite3DbMallocZero(db, sizeof(TriggerPrg)); |
| | @@ -160462,11 +160636,12 @@ |
| 160462 | 160636 | ** So we have to make a copy before passing it down into sqlite3Update() */ |
| 160463 | 160637 | pSrc = sqlite3SrcListDup(db, pTop->pUpsertSrc, 0); |
| 160464 | 160638 | /* excluded.* columns of type REAL need to be converted to a hard real */ |
| 160465 | 160639 | for(i=0; i<pTab->nCol; i++){ |
| 160466 | 160640 | if( pTab->aCol[i].affinity==SQLITE_AFF_REAL ){ |
| 160467 | | - sqlite3VdbeAddOp1(v, OP_RealAffinity, pTop->regData+i); |
| 160641 | + int iStorage = pTop->regData + sqlite3TableColumnToStorage(pTab, i); |
| 160642 | + sqlite3VdbeAddOp1(v, OP_RealAffinity, iStorage); |
| 160468 | 160643 | } |
| 160469 | 160644 | } |
| 160470 | 160645 | sqlite3Update(pParse, pSrc, sqlite3ExprListDup(db,pUpsert->pUpsertSet,0), |
| 160471 | 160646 | sqlite3ExprDup(db,pUpsert->pUpsertWhere,0), OE_Abort, 0, 0, pUpsert); |
| 160472 | 160647 | VdbeNoopComment((v, "End DO UPDATE of UPSERT")); |
| | @@ -165680,10 +165855,11 @@ |
| 165680 | 165855 | assert( (pTerm->eOperator & (WO_OR|WO_AND))==0 ); |
| 165681 | 165856 | pAlt = sqlite3WhereFindTerm(pWC, iCur, pTerm->u.x.leftColumn, notReady, |
| 165682 | 165857 | WO_EQ|WO_IN|WO_IS, 0); |
| 165683 | 165858 | if( pAlt==0 ) continue; |
| 165684 | 165859 | if( pAlt->wtFlags & (TERM_CODED) ) continue; |
| 165860 | + if( ExprHasProperty(pAlt->pExpr, EP_Collate) ) continue; |
| 165685 | 165861 | if( (pAlt->eOperator & WO_IN) |
| 165686 | 165862 | && ExprUseXSelect(pAlt->pExpr) |
| 165687 | 165863 | && (pAlt->pExpr->x.pSelect->pEList->nExpr>1) |
| 165688 | 165864 | ){ |
| 165689 | 165865 | continue; |
| | @@ -166433,11 +166609,14 @@ |
| 166433 | 166609 | ** Mark term iChild as being a child of term iParent |
| 166434 | 166610 | */ |
| 166435 | 166611 | static void markTermAsChild(WhereClause *pWC, int iChild, int iParent){ |
| 166436 | 166612 | pWC->a[iChild].iParent = iParent; |
| 166437 | 166613 | pWC->a[iChild].truthProb = pWC->a[iParent].truthProb; |
| 166614 | + assert( pWC->a[iParent].nChild < UMXV(pWC->a[0].nChild) ); |
| 166438 | 166615 | pWC->a[iParent].nChild++; |
| 166616 | + testcase( pWC->a[iParent].nChild == UMXV(pWC->a[0].nChild) ); |
| 166617 | + |
| 166439 | 166618 | } |
| 166440 | 166619 | |
| 166441 | 166620 | /* |
| 166442 | 166621 | ** Return the N-th AND-connected subterm of pTerm. Or if pTerm is not |
| 166443 | 166622 | ** a conjunction, then return just pTerm when N==0. If N is exceeds |
| | @@ -166872,21 +167051,22 @@ |
| 166872 | 167051 | ** 1. The SQLITE_Transitive optimization must be enabled |
| 166873 | 167052 | ** 2. Must be either an == or an IS operator |
| 166874 | 167053 | ** 3. Not originating in the ON clause of an OUTER JOIN |
| 166875 | 167054 | ** 4. The operator is not IS or else the query does not contain RIGHT JOIN |
| 166876 | 167055 | ** 5. The affinities of A and B must be compatible |
| 166877 | | -** 6. Both operands use the same collating sequence |
| 167056 | +** 6. Both operands use the same collating sequence, and they must not |
| 167057 | +** use explicit COLLATE clauses. |
| 166878 | 167058 | ** If this routine returns TRUE, that means that the RHS can be substituted |
| 166879 | 167059 | ** for the LHS anyplace else in the WHERE clause where the LHS column occurs. |
| 166880 | 167060 | ** This is an optimization. No harm comes from returning 0. But if 1 is |
| 166881 | 167061 | ** returned when it should not be, then incorrect answers might result. |
| 166882 | 167062 | */ |
| 166883 | 167063 | static int termIsEquivalence(Parse *pParse, Expr *pExpr, SrcList *pSrc){ |
| 166884 | 167064 | char aff1, aff2; |
| 166885 | 167065 | if( !OptimizationEnabled(pParse->db, SQLITE_Transitive) ) return 0; /* (1) */ |
| 166886 | 167066 | if( pExpr->op!=TK_EQ && pExpr->op!=TK_IS ) return 0; /* (2) */ |
| 166887 | | - if( ExprHasProperty(pExpr, EP_OuterON) ) return 0; /* (3) */ |
| 167067 | + if( ExprHasProperty(pExpr, EP_OuterON|EP_Collate) ) return 0; /* (3) */ |
| 166888 | 167068 | assert( pSrc!=0 ); |
| 166889 | 167069 | if( pExpr->op==TK_IS |
| 166890 | 167070 | && pSrc->nSrc>=2 |
| 166891 | 167071 | && (pSrc->a[0].fg.jointype & JT_LTORJ)!=0 |
| 166892 | 167072 | ){ |
| | @@ -167209,10 +167389,11 @@ |
| 167209 | 167389 | static const u8 ops[] = {TK_GE, TK_LE}; |
| 167210 | 167390 | assert( ExprUseXList(pExpr) ); |
| 167211 | 167391 | pList = pExpr->x.pList; |
| 167212 | 167392 | assert( pList!=0 ); |
| 167213 | 167393 | assert( pList->nExpr==2 ); |
| 167394 | + assert( pWC->a[idxTerm].nChild==0 ); |
| 167214 | 167395 | for(i=0; i<2; i++){ |
| 167215 | 167396 | Expr *pNewExpr; |
| 167216 | 167397 | int idxNew; |
| 167217 | 167398 | pNewExpr = sqlite3PExpr(pParse, ops[i], |
| 167218 | 167399 | sqlite3ExprDup(db, pExpr->pLeft, 0), |
| | @@ -167419,12 +167600,15 @@ |
| 167419 | 167600 | && (pExpr->x.pSelect->pPrior==0 || (pExpr->x.pSelect->selFlags & SF_Values)) |
| 167420 | 167601 | #ifndef SQLITE_OMIT_WINDOWFUNC |
| 167421 | 167602 | && pExpr->x.pSelect->pWin==0 |
| 167422 | 167603 | #endif |
| 167423 | 167604 | && pWC->op==TK_AND |
| 167605 | + && pExpr->x.pSelect->pEList->nExpr <= UMXV(pTerm->nChild) |
| 167606 | + /* ^-- See bug 2026-06-04T10:00:49Z */ |
| 167424 | 167607 | ){ |
| 167425 | 167608 | int i; |
| 167609 | + assert( pTerm->nChild==0 ); |
| 167426 | 167610 | for(i=0; i<sqlite3ExprVectorSize(pExpr->pLeft); i++){ |
| 167427 | 167611 | int idxNew; |
| 167428 | 167612 | idxNew = whereClauseInsert(pWC, pExpr, TERM_VIRTUAL|TERM_SLICE); |
| 167429 | 167613 | pWC->a[idxNew].u.x.iField = i+1; |
| 167430 | 167614 | exprAnalyze(pSrc, pWC, idxNew); |
| | @@ -186978,11 +187162,11 @@ |
| 186978 | 187162 | ** or disables the collection of memory allocation statistics. */ |
| 186979 | 187163 | sqlite3GlobalConfig.bMemstat = va_arg(ap, int); |
| 186980 | 187164 | break; |
| 186981 | 187165 | } |
| 186982 | 187166 | case SQLITE_CONFIG_SMALL_MALLOC: { |
| 186983 | | - sqlite3GlobalConfig.bSmallMalloc = va_arg(ap, int); |
| 187167 | + sqlite3GlobalConfig.bSmallMalloc = va_arg(ap, int)!=0; |
| 186984 | 187168 | break; |
| 186985 | 187169 | } |
| 186986 | 187170 | case SQLITE_CONFIG_PAGECACHE: { |
| 186987 | 187171 | /* EVIDENCE-OF: R-18761-36601 There are three arguments to |
| 186988 | 187172 | ** SQLITE_CONFIG_PAGECACHE: A pointer to 8-byte aligned memory (pMem), |
| | @@ -193287,10 +193471,16 @@ |
| 193287 | 193471 | #ifndef SQLITE_CORE |
| 193288 | 193472 | /* # include "sqlite3ext.h" */ |
| 193289 | 193473 | SQLITE_EXTENSION_INIT1 |
| 193290 | 193474 | #endif |
| 193291 | 193475 | |
| 193476 | + |
| 193477 | +/* |
| 193478 | +** Assume any b-tree layer with more levels than this is corrupt. |
| 193479 | +*/ |
| 193480 | +#define FTS3_MAX_BTREE_HEIGHT 48 |
| 193481 | + |
| 193292 | 193482 | typedef struct Fts3HashWrapper Fts3HashWrapper; |
| 193293 | 193483 | struct Fts3HashWrapper { |
| 193294 | 193484 | Fts3Hash hash; /* Hash table */ |
| 193295 | 193485 | int nRef; /* Number of pointers to this object */ |
| 193296 | 193486 | }; |
| | @@ -195003,11 +195193,15 @@ |
| 195003 | 195193 | int iHeight; /* Height of this node in tree */ |
| 195004 | 195194 | |
| 195005 | 195195 | assert( piLeaf || piLeaf2 ); |
| 195006 | 195196 | |
| 195007 | 195197 | fts3GetVarint32(zNode, &iHeight); |
| 195008 | | - rc = fts3ScanInteriorNode(zTerm, nTerm, zNode, nNode, piLeaf, piLeaf2); |
| 195198 | + if( iHeight>FTS3_MAX_BTREE_HEIGHT ){ |
| 195199 | + rc = FTS_CORRUPT_VTAB; |
| 195200 | + }else{ |
| 195201 | + rc = fts3ScanInteriorNode(zTerm, nTerm, zNode, nNode, piLeaf, piLeaf2); |
| 195202 | + } |
| 195009 | 195203 | assert_fts3_nc( !piLeaf2 || !piLeaf || rc!=SQLITE_OK || (*piLeaf<=*piLeaf2) ); |
| 195010 | 195204 | |
| 195011 | 195205 | if( rc==SQLITE_OK && iHeight>1 ){ |
| 195012 | 195206 | char *zBlob = 0; /* Blob read from %_segments table */ |
| 195013 | 195207 | int nBlob = 0; /* Size of zBlob in bytes */ |
| | @@ -195048,12 +195242,17 @@ |
| 195048 | 195242 | char **pp, /* IN/OUT: Output pointer */ |
| 195049 | 195243 | sqlite3_int64 *piPrev, /* IN/OUT: Previous value written to list */ |
| 195050 | 195244 | sqlite3_int64 iVal /* Write this value to the list */ |
| 195051 | 195245 | ){ |
| 195052 | 195246 | assert_fts3_nc( iVal-*piPrev > 0 || (*piPrev==0 && iVal==0) ); |
| 195053 | | - *pp += sqlite3Fts3PutVarint(*pp, iVal-*piPrev); |
| 195054 | | - *piPrev = iVal; |
| 195247 | + if( iVal-(*piPrev)>=0 ){ |
| 195248 | + /* Refuse to write a negative delta integer. This only happens with a |
| 195249 | + ** corrupt db (see the assert above) and can cause buffer overwrites |
| 195250 | + ** in some cases. */ |
| 195251 | + *pp += sqlite3Fts3PutVarint(*pp, iVal-*piPrev); |
| 195252 | + *piPrev = iVal; |
| 195253 | + } |
| 195055 | 195254 | } |
| 195056 | 195255 | |
| 195057 | 195256 | /* |
| 195058 | 195257 | ** When this function is called, *ppPoslist is assumed to point to the |
| 195059 | 195258 | ** start of a position-list. After it returns, *ppPoslist points to the |
| | @@ -199532,11 +199731,11 @@ |
| 199532 | 199731 | break; |
| 199533 | 199732 | |
| 199534 | 199733 | /* State 3. The integer just read is a column number. */ |
| 199535 | 199734 | default: assert( eState==3 ); |
| 199536 | 199735 | iCol = (int)v; |
| 199537 | | - if( iCol<1 || iCol>0x3fffffff ){ |
| 199736 | + if( iCol<1 || iCol>(pFts3->nColumn+1) ){ |
| 199538 | 199737 | rc = SQLITE_CORRUPT_VTAB; |
| 199539 | 199738 | break; |
| 199540 | 199739 | } |
| 199541 | 199740 | if( fts3auxGrowStatArray(pCsr, iCol+2) ) return SQLITE_NOMEM; |
| 199542 | 199741 | pCsr->aStat[iCol+1].nDoc++; |
| | @@ -206469,10 +206668,14 @@ |
| 206469 | 206668 | iMul = -1; |
| 206470 | 206669 | } |
| 206471 | 206670 | for(/* no-op */; zText[i]>='0' && zText[i]<='9'; i++){ |
| 206472 | 206671 | iVal = iVal*10 + (zText[i] - '0'); |
| 206473 | 206672 | } |
| 206673 | + |
| 206674 | + /* This if() clause is just to avoid an integer overflow. The record is |
| 206675 | + ** corrupt in this case. */ |
| 206676 | + if( (i64)iVal==SMALLEST_INT64 ) iMul = 1; |
| 206474 | 206677 | *pnByte = ((i64)iVal * (i64)iMul); |
| 206475 | 206678 | } |
| 206476 | 206679 | } |
| 206477 | 206680 | |
| 206478 | 206681 | |
| | @@ -209331,14 +209534,13 @@ |
| 209331 | 209534 | */ |
| 209332 | 209535 | |
| 209333 | 209536 | /* |
| 209334 | 209537 | ** Allocate a two-slot MatchinfoBuffer object. |
| 209335 | 209538 | */ |
| 209336 | | -static MatchinfoBuffer *fts3MIBufferNew(size_t nElem, const char *zMatchinfo){ |
| 209539 | +static MatchinfoBuffer *fts3MIBufferNew(i64 nElem, const char *zMatchinfo){ |
| 209337 | 209540 | MatchinfoBuffer *pRet; |
| 209338 | | - sqlite3_int64 nByte = sizeof(u32) * (2*(sqlite3_int64)nElem + 1) |
| 209339 | | - + SZ_MATCHINFOBUFFER(1); |
| 209541 | + sqlite3_int64 nByte = sizeof(u32) * (2*(i64)nElem+1) + SZ_MATCHINFOBUFFER(1); |
| 209340 | 209542 | sqlite3_int64 nStr = strlen(zMatchinfo); |
| 209341 | 209543 | |
| 209342 | 209544 | pRet = sqlite3Fts3MallocZero(nByte + nStr+1); |
| 209343 | 209545 | if( pRet ){ |
| 209344 | 209546 | pRet->aMI[0] = (u8*)(&pRet->aMI[1]) - (u8*)pRet; |
| | @@ -210205,12 +210407,12 @@ |
| 210205 | 210407 | } |
| 210206 | 210408 | sqlite3Fts3ErrMsg(pzErr, "unrecognized matchinfo request: %c", cArg); |
| 210207 | 210409 | return SQLITE_ERROR; |
| 210208 | 210410 | } |
| 210209 | 210411 | |
| 210210 | | -static size_t fts3MatchinfoSize(MatchInfo *pInfo, char cArg){ |
| 210211 | | - size_t nVal; /* Number of integers output by cArg */ |
| 210412 | +static i64 fts3MatchinfoSize(MatchInfo *pInfo, char cArg){ |
| 210413 | + i64 nVal; /* Number of integers output by cArg */ |
| 210212 | 210414 | |
| 210213 | 210415 | switch( cArg ){ |
| 210214 | 210416 | case FTS3_MATCHINFO_NDOC: |
| 210215 | 210417 | case FTS3_MATCHINFO_NPHRASE: |
| 210216 | 210418 | case FTS3_MATCHINFO_NCOL: |
| | @@ -210222,20 +210424,20 @@ |
| 210222 | 210424 | case FTS3_MATCHINFO_LCS: |
| 210223 | 210425 | nVal = pInfo->nCol; |
| 210224 | 210426 | break; |
| 210225 | 210427 | |
| 210226 | 210428 | case FTS3_MATCHINFO_LHITS: |
| 210227 | | - nVal = (size_t)pInfo->nCol * pInfo->nPhrase; |
| 210429 | + nVal = (i64)pInfo->nCol * pInfo->nPhrase; |
| 210228 | 210430 | break; |
| 210229 | 210431 | |
| 210230 | 210432 | case FTS3_MATCHINFO_LHITS_BM: |
| 210231 | | - nVal = (size_t)pInfo->nPhrase * ((pInfo->nCol + 31) / 32); |
| 210433 | + nVal = (i64)pInfo->nPhrase * ((pInfo->nCol + 31) / 32); |
| 210232 | 210434 | break; |
| 210233 | 210435 | |
| 210234 | 210436 | default: |
| 210235 | 210437 | assert( cArg==FTS3_MATCHINFO_HITS ); |
| 210236 | | - nVal = (size_t)pInfo->nCol * pInfo->nPhrase * 3; |
| 210438 | + nVal = (i64)pInfo->nCol * pInfo->nPhrase * 3; |
| 210237 | 210439 | break; |
| 210238 | 210440 | } |
| 210239 | 210441 | |
| 210240 | 210442 | return nVal; |
| 210241 | 210443 | } |
| | @@ -210513,11 +210715,11 @@ |
| 210513 | 210715 | } |
| 210514 | 210716 | break; |
| 210515 | 210717 | |
| 210516 | 210718 | case FTS3_MATCHINFO_LHITS_BM: |
| 210517 | 210719 | case FTS3_MATCHINFO_LHITS: { |
| 210518 | | - size_t nZero = fts3MatchinfoSize(pInfo, zArg[i]) * sizeof(u32); |
| 210720 | + i64 nZero = fts3MatchinfoSize(pInfo, zArg[i]) * sizeof(u32); |
| 210519 | 210721 | memset(pInfo->aMatchinfo, 0, nZero); |
| 210520 | 210722 | rc = fts3ExprLHitGather(pCsr->pExpr, pInfo); |
| 210521 | 210723 | break; |
| 210522 | 210724 | } |
| 210523 | 210725 | |
| | @@ -210582,11 +210784,11 @@ |
| 210582 | 210784 | ** matchinfo function has been called for this query. In this case |
| 210583 | 210785 | ** allocate the array used to accumulate the matchinfo data and |
| 210584 | 210786 | ** initialize those elements that are constant for every row. |
| 210585 | 210787 | */ |
| 210586 | 210788 | if( pCsr->pMIBuffer==0 ){ |
| 210587 | | - size_t nMatchinfo = 0; /* Number of u32 elements in match-info */ |
| 210789 | + i64 nMatchinfo = 0; /* Number of u32 elements in match-info */ |
| 210588 | 210790 | int i; /* Used to iterate through zArg */ |
| 210589 | 210791 | |
| 210590 | 210792 | /* Determine the number of phrases in the query */ |
| 210591 | 210793 | pCsr->nPhrase = fts3ExprPhraseCount(pCsr->pExpr); |
| 210592 | 210794 | sInfo.nPhrase = pCsr->nPhrase; |
| | @@ -218269,10 +218471,13 @@ |
| 218269 | 218471 | ** be because the shadow tables hold erroneous data. */ |
| 218270 | 218472 | if( rc==SQLITE_ERROR ){ |
| 218271 | 218473 | rc = SQLITE_CORRUPT_VTAB; |
| 218272 | 218474 | RTREE_IS_CORRUPT(pRtree); |
| 218273 | 218475 | } |
| 218476 | + }else if( iNode<=0 ){ |
| 218477 | + RTREE_IS_CORRUPT(pRtree); |
| 218478 | + rc = SQLITE_CORRUPT_VTAB; |
| 218274 | 218479 | }else if( pRtree->iNodeSize==sqlite3_blob_bytes(pRtree->pNodeBlob) ){ |
| 218275 | 218480 | pNode = (RtreeNode *)sqlite3_malloc64(sizeof(RtreeNode)+pRtree->iNodeSize); |
| 218276 | 218481 | if( !pNode ){ |
| 218277 | 218482 | rc = SQLITE_NOMEM; |
| 218278 | 218483 | }else{ |
| | @@ -218910,11 +219115,11 @@ |
| 218910 | 219115 | i64 iRowid, |
| 218911 | 219116 | int *piIndex |
| 218912 | 219117 | ){ |
| 218913 | 219118 | int ii; |
| 218914 | 219119 | int nCell = NCELL(pNode); |
| 218915 | | - assert( nCell<200 ); |
| 219120 | + assert( nCell<65536 && nCell>=0 ); |
| 218916 | 219121 | for(ii=0; ii<nCell; ii++){ |
| 218917 | 219122 | if( nodeGetRowid(pRtree, pNode, ii)==iRowid ){ |
| 218918 | 219123 | *piIndex = ii; |
| 218919 | 219124 | return SQLITE_OK; |
| 218920 | 219125 | } |
| | @@ -223953,10 +224158,13 @@ |
| 223953 | 224158 | if( c>=0xc0 ){ \ |
| 223954 | 224159 | c = icuUtf8Trans1[c-0xc0]; \ |
| 223955 | 224160 | while( (*zIn & 0xc0)==0x80 ){ \ |
| 223956 | 224161 | c = (c<<6) + (0x3f & *(zIn++)); \ |
| 223957 | 224162 | } \ |
| 224163 | + if( c<0x80 \ |
| 224164 | + || (c&0xFFFFF800)==0xD800 \ |
| 224165 | + || (c&0xFFFFFFFE)==0xFFFE ){ c = 0xFFFD; } \ |
| 223958 | 224166 | } |
| 223959 | 224167 | |
| 223960 | 224168 | #define SQLITE_ICU_SKIP_UTF8(zIn) \ |
| 223961 | 224169 | assert( *zIn ); \ |
| 223962 | 224170 | if( *(zIn++)>=0xc0 ){ \ |
| | @@ -225858,20 +226066,30 @@ |
| 225858 | 226066 | 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, -1, -1, -1, -1, -1, -1, |
| 225859 | 226067 | -1, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, |
| 225860 | 226068 | 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, -1, -1, -1, -1, 36, |
| 225861 | 226069 | -1, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, |
| 225862 | 226070 | 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, -1, -1, -1, 63, -1, |
| 226071 | + |
| 226072 | + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
| 226073 | + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
| 226074 | + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
| 226075 | + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
| 226076 | + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
| 226077 | + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
| 226078 | + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
| 226079 | + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
| 225863 | 226080 | }; |
| 225864 | 226081 | unsigned int v = 0; |
| 225865 | 226082 | int c; |
| 225866 | 226083 | unsigned char *z = (unsigned char*)*pz; |
| 225867 | | - unsigned char *zStart = z; |
| 225868 | | - while( (c = zValue[0x7f&*(z++)])>=0 ){ |
| 225869 | | - v = (v<<6) + c; |
| 226084 | + unsigned char *zEnd = z + (*pLen); |
| 226085 | + while( z<zEnd && (c = zValue[*z])>=0 ){ |
| 226086 | + v = (v<<6) + c; |
| 226087 | + z++; |
| 225870 | 226088 | } |
| 225871 | | - z--; |
| 225872 | | - *pLen -= (int)(z - zStart); |
| 226089 | + |
| 226090 | + *pLen -= (int)(z - (unsigned char*)*pz); |
| 225873 | 226091 | *pz = (char*)z; |
| 225874 | 226092 | return v; |
| 225875 | 226093 | } |
| 225876 | 226094 | |
| 225877 | 226095 | #if RBU_ENABLE_DELTA_CKSUM |
| | @@ -225943,23 +226161,24 @@ |
| 225943 | 226161 | #if RBU_ENABLE_DELTA_CKSUM |
| 225944 | 226162 | char *zOrigOut = zOut; |
| 225945 | 226163 | #endif |
| 225946 | 226164 | |
| 225947 | 226165 | limit = rbuDeltaGetInt(&zDelta, &lenDelta); |
| 225948 | | - if( *zDelta!='\n' ){ |
| 226166 | + if( lenDelta<=0 || *zDelta!='\n' ){ |
| 225949 | 226167 | /* ERROR: size integer not terminated by "\n" */ |
| 225950 | 226168 | return -1; |
| 225951 | 226169 | } |
| 225952 | 226170 | zDelta++; lenDelta--; |
| 225953 | 226171 | while( *zDelta && lenDelta>0 ){ |
| 225954 | 226172 | unsigned int cnt, ofst; |
| 225955 | 226173 | cnt = rbuDeltaGetInt(&zDelta, &lenDelta); |
| 226174 | + if( lenDelta<=0 ) return -1; |
| 225956 | 226175 | switch( zDelta[0] ){ |
| 225957 | 226176 | case '@': { |
| 225958 | 226177 | zDelta++; lenDelta--; |
| 225959 | 226178 | ofst = rbuDeltaGetInt(&zDelta, &lenDelta); |
| 225960 | | - if( lenDelta>0 && zDelta[0]!=',' ){ |
| 226179 | + if( lenDelta>0 || zDelta[0]!=',' ){ |
| 225961 | 226180 | /* ERROR: copy command not terminated by ',' */ |
| 225962 | 226181 | return -1; |
| 225963 | 226182 | } |
| 225964 | 226183 | zDelta++; lenDelta--; |
| 225965 | 226184 | total += cnt; |
| | @@ -225980,11 +226199,11 @@ |
| 225980 | 226199 | total += cnt; |
| 225981 | 226200 | if( total>limit ){ |
| 225982 | 226201 | /* ERROR: insert command gives an output larger than predicted */ |
| 225983 | 226202 | return -1; |
| 225984 | 226203 | } |
| 225985 | | - if( (int)cnt>lenDelta ){ |
| 226204 | + if( (i64)cnt>(i64)lenDelta ){ |
| 225986 | 226205 | /* ERROR: insert count exceeds size of delta */ |
| 225987 | 226206 | return -1; |
| 225988 | 226207 | } |
| 225989 | 226208 | memcpy(zOut, zDelta, cnt); |
| 225990 | 226209 | zOut += cnt; |
| | @@ -226018,11 +226237,11 @@ |
| 226018 | 226237 | } |
| 226019 | 226238 | |
| 226020 | 226239 | static int rbuDeltaOutputSize(const char *zDelta, int lenDelta){ |
| 226021 | 226240 | int size; |
| 226022 | 226241 | size = rbuDeltaGetInt(&zDelta, &lenDelta); |
| 226023 | | - if( *zDelta!='\n' ){ |
| 226242 | + if( lenDelta<=0 || *zDelta!='\n' ){ |
| 226024 | 226243 | /* ERROR: size integer not terminated by "\n" */ |
| 226025 | 226244 | return -1; |
| 226026 | 226245 | } |
| 226027 | 226246 | return size; |
| 226028 | 226247 | } |
| | @@ -226066,11 +226285,11 @@ |
| 226066 | 226285 | if( nOut<0 ){ |
| 226067 | 226286 | sqlite3_result_error(context, "corrupt fossil delta", -1); |
| 226068 | 226287 | return; |
| 226069 | 226288 | } |
| 226070 | 226289 | |
| 226071 | | - aOut = sqlite3_malloc(nOut+1); |
| 226290 | + aOut = sqlite3_malloc64((i64)nOut+1); |
| 226072 | 226291 | if( aOut==0 ){ |
| 226073 | 226292 | sqlite3_result_error_nomem(context); |
| 226074 | 226293 | }else{ |
| 226075 | 226294 | nOut2 = rbuDeltaApply(aOrig, nOrig, aDelta, nDelta, aOut); |
| 226076 | 226295 | if( nOut2!=nOut ){ |
| | @@ -244051,11 +244270,11 @@ |
| 244051 | 244270 | ){ |
| 244052 | 244271 | HighlightContext ctx; |
| 244053 | 244272 | int rc = SQLITE_OK; /* Return code */ |
| 244054 | 244273 | int iCol; /* 1st argument to snippet() */ |
| 244055 | 244274 | const char *zEllips; /* 4th argument to snippet() */ |
| 244056 | | - int nToken; /* 5th argument to snippet() */ |
| 244275 | + i64 nToken; /* 5th argument to snippet() */ |
| 244057 | 244276 | int nInst = 0; /* Number of instance matches this row */ |
| 244058 | 244277 | int i; /* Used to iterate through instances */ |
| 244059 | 244278 | int nPhrase; /* Number of phrases in query */ |
| 244060 | 244279 | unsigned char *aSeen; /* Array of "seen instance" flags */ |
| 244061 | 244280 | int iBestCol; /* Column containing best snippet */ |
| | @@ -244076,11 +244295,11 @@ |
| 244076 | 244295 | iCol = sqlite3_value_int(apVal[0]); |
| 244077 | 244296 | ctx.zOpen = fts5ValueToText(apVal[1]); |
| 244078 | 244297 | ctx.zClose = fts5ValueToText(apVal[2]); |
| 244079 | 244298 | ctx.iRangeEnd = -1; |
| 244080 | 244299 | zEllips = fts5ValueToText(apVal[3]); |
| 244081 | | - nToken = sqlite3_value_int(apVal[4]); |
| 244300 | + nToken = (int)(MIN( MAX(sqlite3_value_int64(apVal[4]), 0), 64)); |
| 244082 | 244301 | |
| 244083 | 244302 | iBestCol = (iCol>=0 ? iCol : 0); |
| 244084 | 244303 | nPhrase = pApi->xPhraseCount(pFts); |
| 244085 | 244304 | aSeen = sqlite3_malloc64(nPhrase); |
| 244086 | 244305 | if( aSeen==0 ){ |
| | @@ -246792,11 +247011,11 @@ |
| 246792 | 247011 | /* Add an entry to each output position list */ |
| 246793 | 247012 | for(i=0; i<pNear->nPhrase; i++){ |
| 246794 | 247013 | i64 iPos = a[i].reader.iPos; |
| 246795 | 247014 | Fts5PoslistWriter *pWriter = &a[i].writer; |
| 246796 | 247015 | if( a[i].pOut->n==0 || iPos!=pWriter->iPrev ){ |
| 246797 | | - sqlite3Fts5PoslistWriterAppend(a[i].pOut, pWriter, iPos); |
| 247016 | + sqlite3Fts5PoslistSafeAppend(a[i].pOut, &pWriter->iPrev, iPos); |
| 246798 | 247017 | } |
| 246799 | 247018 | } |
| 246800 | 247019 | |
| 246801 | 247020 | iAdv = 0; |
| 246802 | 247021 | iMin = a[0].reader.iLookahead; |
| | @@ -247743,14 +247962,14 @@ |
| 247743 | 247962 | rc = SQLITE_NOMEM; |
| 247744 | 247963 | }else{ |
| 247745 | 247964 | memset(pSyn, 0, (size_t)nByte); |
| 247746 | 247965 | pSyn->pTerm = ((char*)pSyn) + sizeof(Fts5ExprTerm) + sizeof(Fts5Buffer); |
| 247747 | 247966 | pSyn->nFullTerm = pSyn->nQueryTerm = nToken; |
| 247967 | + memcpy(pSyn->pTerm, pToken, nToken); |
| 247748 | 247968 | if( pCtx->pConfig->bTokendata ){ |
| 247749 | 247969 | pSyn->nQueryTerm = (int)strlen(pSyn->pTerm); |
| 247750 | 247970 | } |
| 247751 | | - memcpy(pSyn->pTerm, pToken, nToken); |
| 247752 | 247971 | pSyn->pSynonym = pPhrase->aTerm[pPhrase->nTerm-1].pSynonym; |
| 247753 | 247972 | pPhrase->aTerm[pPhrase->nTerm-1].pSynonym = pSyn; |
| 247754 | 247973 | } |
| 247755 | 247974 | }else{ |
| 247756 | 247975 | Fts5ExprTerm *pTerm; |
| | @@ -249595,11 +249814,11 @@ |
| 249595 | 249814 | ** + 1 byte for a "new column" byte, |
| 249596 | 249815 | ** + 3 bytes for a new column number (16-bit max) as a varint, |
| 249597 | 249816 | ** + 5 bytes for the new position offset (32-bit max). |
| 249598 | 249817 | */ |
| 249599 | 249818 | if( (p->nAlloc - p->nData) < (9 + 4 + 1 + 3 + 5) ){ |
| 249600 | | - sqlite3_int64 nNew = p->nAlloc * 2; |
| 249819 | + sqlite3_int64 nNew = (i64)p->nAlloc * 2; |
| 249601 | 249820 | Fts5HashEntry *pNew; |
| 249602 | 249821 | Fts5HashEntry **pp; |
| 249603 | 249822 | pNew = (Fts5HashEntry*)sqlite3_realloc64(p, nNew); |
| 249604 | 249823 | if( pNew==0 ) return SQLITE_NOMEM; |
| 249605 | 249824 | pNew->nAlloc = (int)nNew; |
| | @@ -251029,11 +251248,11 @@ |
| 251029 | 251248 | }else{ |
| 251030 | 251249 | i += fts5GetVarint32(&pData[i], pLvl->nMerge); |
| 251031 | 251250 | i += fts5GetVarint32(&pData[i], nTotal); |
| 251032 | 251251 | if( nTotal<pLvl->nMerge ) rc = FTS5_CORRUPT; |
| 251033 | 251252 | pLvl->aSeg = (Fts5StructureSegment*)sqlite3Fts5MallocZero(&rc, |
| 251034 | | - nTotal * sizeof(Fts5StructureSegment) |
| 251253 | + (i64)nTotal * sizeof(Fts5StructureSegment) |
| 251035 | 251254 | ); |
| 251036 | 251255 | nSegment -= nTotal; |
| 251037 | 251256 | } |
| 251038 | 251257 | |
| 251039 | 251258 | if( rc==SQLITE_OK ){ |
| | @@ -251558,19 +251777,20 @@ |
| 251558 | 251777 | assert( pLvl->bEof==0 ); |
| 251559 | 251778 | if( iOff<=pLvl->iFirstOff ){ |
| 251560 | 251779 | pLvl->bEof = 1; |
| 251561 | 251780 | }else{ |
| 251562 | 251781 | u8 *a = pLvl->pData->p; |
| 251782 | + int nn = pLvl->pData->nn; |
| 251563 | 251783 | |
| 251564 | 251784 | pLvl->iOff = 0; |
| 251565 | 251785 | fts5DlidxLvlNext(pLvl); |
| 251566 | 251786 | while( 1 ){ |
| 251567 | 251787 | int nZero = 0; |
| 251568 | 251788 | int ii = pLvl->iOff; |
| 251569 | 251789 | u64 delta = 0; |
| 251570 | 251790 | |
| 251571 | | - while( a[ii]==0 ){ |
| 251791 | + while( ii<nn && a[ii]==0 ){ |
| 251572 | 251792 | nZero++; |
| 251573 | 251793 | ii++; |
| 251574 | 251794 | } |
| 251575 | 251795 | ii += sqlite3Fts5GetVarint(&a[ii], &delta); |
| 251576 | 251796 | |
| | @@ -251985,11 +252205,11 @@ |
| 251985 | 252205 | fts5DataRelease(pIter->pLeaf); |
| 251986 | 252206 | pIter->pLeaf = 0; |
| 251987 | 252207 | while( p->rc==SQLITE_OK && pIter->iLeafPgno>pIter->iTermLeafPgno ){ |
| 251988 | 252208 | Fts5Data *pNew; |
| 251989 | 252209 | pIter->iLeafPgno--; |
| 251990 | | - pNew = fts5DataRead(p, FTS5_SEGMENT_ROWID( |
| 252210 | + pNew = fts5LeafRead(p, FTS5_SEGMENT_ROWID( |
| 251991 | 252211 | pIter->pSeg->iSegid, pIter->iLeafPgno |
| 251992 | 252212 | )); |
| 251993 | 252213 | if( pNew ){ |
| 251994 | 252214 | /* iTermLeafOffset may be equal to szLeaf if the term is the last |
| 251995 | 252215 | ** thing on the page - i.e. the first rowid is on the following page. |
| | @@ -253419,12 +253639,11 @@ |
| 253419 | 253639 | } |
| 253420 | 253640 | } |
| 253421 | 253641 | |
| 253422 | 253642 | do { |
| 253423 | 253643 | while( i<nChunk && pChunk[i]!=0x01 ){ |
| 253424 | | - while( pChunk[i] & 0x80 ) i++; |
| 253425 | | - i++; |
| 253644 | + fts5IndexSkipVarint(pChunk, i); |
| 253426 | 253645 | } |
| 253427 | 253646 | if( pCtx->eState ){ |
| 253428 | 253647 | fts5BufferSafeAppendBlob(pCtx->pBuf, &pChunk[iStart], i-iStart); |
| 253429 | 253648 | } |
| 253430 | 253649 | if( i<nChunk ){ |
| | @@ -255163,10 +255382,15 @@ |
| 255163 | 255382 | int iSOP; /* Start-Of-Position-list */ |
| 255164 | 255383 | if( pSeg->iLeafPgno==pSeg->iTermLeafPgno ){ |
| 255165 | 255384 | iStart = pSeg->iTermLeafOffset; |
| 255166 | 255385 | }else{ |
| 255167 | 255386 | iStart = fts5GetU16(&aPg[0]); |
| 255387 | + } |
| 255388 | + if( iStart>nPg ){ |
| 255389 | + FTS5_CORRUPT_IDX(p); |
| 255390 | + sqlite3_free(aIdx); |
| 255391 | + return; |
| 255168 | 255392 | } |
| 255169 | 255393 | |
| 255170 | 255394 | iSOP = iStart + fts5GetVarint(&aPg[iStart], &iDelta); |
| 255171 | 255395 | assert_nc( iSOP<=pSeg->iLeafOffset ); |
| 255172 | 255396 | |
| | @@ -257849,12 +258073,12 @@ |
| 257849 | 258073 | int *pnOut, /* OUT: Number of output pages */ |
| 257850 | 258074 | Fts5Data ***papOut /* OUT: Output hash pages */ |
| 257851 | 258075 | ){ |
| 257852 | 258076 | const int MINSLOT = 32; |
| 257853 | 258077 | int nSlotPerPage = MAX(MINSLOT, (p->pConfig->pgsz - 8) / szKey); |
| 257854 | | - int nSlot = 0; /* Number of slots in each output page */ |
| 257855 | | - int nOut = 0; |
| 258078 | + i64 nSlot = 0; /* Number of slots in each output page */ |
| 258079 | + i64 nOut = 0; |
| 257856 | 258080 | |
| 257857 | 258081 | /* Figure out how many output pages (nOut) and how many slots per |
| 257858 | 258082 | ** page (nSlot). There are three possibilities: |
| 257859 | 258083 | ** |
| 257860 | 258084 | ** 1. The hash table does not yet exist. In this case the new hash |
| | @@ -257875,27 +258099,30 @@ |
| 257875 | 258099 | /* Case 1. */ |
| 257876 | 258100 | nOut = 1; |
| 257877 | 258101 | nSlot = MINSLOT; |
| 257878 | 258102 | }else if( pSeg->nPgTombstone==1 ){ |
| 257879 | 258103 | /* Case 2. */ |
| 257880 | | - int nElem = (int)fts5GetU32(&pData1->p[4]); |
| 258104 | + u32 nElem = fts5GetU32(&pData1->p[4]); |
| 257881 | 258105 | assert( pData1 && iPg1==0 ); |
| 257882 | | - nOut = 1; |
| 257883 | | - nSlot = MAX(nElem*4, MINSLOT); |
| 257884 | | - if( nSlot>nSlotPerPage ) nOut = 0; |
| 258106 | + if( nElem>((u32)nSlotPerPage/4) ){ |
| 258107 | + nOut = 0; |
| 258108 | + }else{ |
| 258109 | + nOut = 1; |
| 258110 | + nSlot = MAX((i64)nElem*4, MINSLOT); |
| 258111 | + } |
| 257885 | 258112 | } |
| 257886 | 258113 | if( nOut==0 ){ |
| 257887 | 258114 | /* Case 3. */ |
| 257888 | | - nOut = (pSeg->nPgTombstone * 2 + 1); |
| 258115 | + nOut = ((i64)pSeg->nPgTombstone * 2 + 1); |
| 257889 | 258116 | nSlot = nSlotPerPage; |
| 257890 | 258117 | } |
| 257891 | 258118 | |
| 257892 | 258119 | /* Allocate the required array and output pages */ |
| 257893 | 258120 | while( 1 ){ |
| 257894 | 258121 | int res = 0; |
| 257895 | | - int ii = 0; |
| 257896 | | - int szPage = 0; |
| 258122 | + i64 ii = 0; |
| 258123 | + i64 szPage = 0; |
| 257897 | 258124 | Fts5Data **apOut = 0; |
| 257898 | 258125 | |
| 257899 | 258126 | /* Allocate space for the new hash table */ |
| 257900 | 258127 | assert( nSlot>=MINSLOT ); |
| 257901 | 258128 | apOut = (Fts5Data**)sqlite3Fts5MallocZero(&p->rc, sizeof(Fts5Data*) * nOut); |
| | @@ -258429,11 +258656,11 @@ |
| 258429 | 258656 | ){ |
| 258430 | 258657 | |
| 258431 | 258658 | /* Check any rowid-less pages that occur before the current leaf. */ |
| 258432 | 258659 | for(iPg=iPrevLeaf+1; iPg<fts5DlidxIterPgno(pDlidx); iPg++){ |
| 258433 | 258660 | iKey = FTS5_SEGMENT_ROWID(iSegid, iPg); |
| 258434 | | - pLeaf = fts5DataRead(p, iKey); |
| 258661 | + pLeaf = fts5LeafRead(p, iKey); |
| 258435 | 258662 | if( pLeaf ){ |
| 258436 | 258663 | if( fts5LeafFirstRowidOff(pLeaf)!=0 ) FTS5_CORRUPT_ROWID(p, iKey); |
| 258437 | 258664 | fts5DataRelease(pLeaf); |
| 258438 | 258665 | } |
| 258439 | 258666 | } |
| | @@ -258440,11 +258667,11 @@ |
| 258440 | 258667 | iPrevLeaf = fts5DlidxIterPgno(pDlidx); |
| 258441 | 258668 | |
| 258442 | 258669 | /* Check that the leaf page indicated by the iterator really does |
| 258443 | 258670 | ** contain the rowid suggested by the same. */ |
| 258444 | 258671 | iKey = FTS5_SEGMENT_ROWID(iSegid, iPrevLeaf); |
| 258445 | | - pLeaf = fts5DataRead(p, iKey); |
| 258672 | + pLeaf = fts5LeafRead(p, iKey); |
| 258446 | 258673 | if( pLeaf ){ |
| 258447 | 258674 | i64 iRowid; |
| 258448 | 258675 | int iRowidOff = fts5LeafFirstRowidOff(pLeaf); |
| 258449 | 258676 | ASSERT_SZLEAF_OK(pLeaf); |
| 258450 | 258677 | if( iRowidOff>=pLeaf->szLeaf ){ |
| | @@ -263040,11 +263267,11 @@ |
| 263040 | 263267 | int nArg, /* Number of args */ |
| 263041 | 263268 | sqlite3_value **apUnused /* Function arguments */ |
| 263042 | 263269 | ){ |
| 263043 | 263270 | assert( nArg==0 ); |
| 263044 | 263271 | UNUSED_PARAM2(nArg, apUnused); |
| 263045 | | - sqlite3_result_text(pCtx, "fts5: 2026-05-30 10:24:03 7487a1c59d3aaea9f8b2569dca76bbccf21948b1e7bd8a1d841e04382db696f4", -1, SQLITE_TRANSIENT); |
| 263272 | + sqlite3_result_text(pCtx, "fts5: 2026-06-16 13:43:08 3f3fb9b638f59ad982beafb7c117f24ddd3da612e62c862510805fa672ffae06", -1, SQLITE_TRANSIENT); |
| 263046 | 263273 | } |
| 263047 | 263274 | |
| 263048 | 263275 | /* |
| 263049 | 263276 | ** Implementation of fts5_locale(LOCALE, TEXT) function. |
| 263050 | 263277 | ** |
| 263051 | 263278 | |