| | @@ -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 | | -** bcac937526d9a6ef914a74b4d6757fa91cd7. |
| 21 | +** 97709ce2a1f5ae05495e412ca27108048e5b. |
| 22 | 22 | */ |
| 23 | 23 | #define SQLITE_CORE 1 |
| 24 | 24 | #define SQLITE_AMALGAMATION 1 |
| 25 | 25 | #ifndef SQLITE_PRIVATE |
| 26 | 26 | # define SQLITE_PRIVATE static |
| | @@ -459,11 +459,11 @@ |
| 459 | 459 | ** [sqlite3_libversion_number()], [sqlite3_sourceid()], |
| 460 | 460 | ** [sqlite_version()] and [sqlite_source_id()]. |
| 461 | 461 | */ |
| 462 | 462 | #define SQLITE_VERSION "3.45.0" |
| 463 | 463 | #define SQLITE_VERSION_NUMBER 3045000 |
| 464 | | -#define SQLITE_SOURCE_ID "2024-01-01 19:20:00 bcac937526d9a6ef914a74b4d6757fa91cd74edab871bcd934fde4a2f9b6debd" |
| 464 | +#define SQLITE_SOURCE_ID "2024-01-09 12:28:51 97709ce2a1f5ae05495e412ca27108048e5b8a63a1e3bca4be13933f7527da7b" |
| 465 | 465 | |
| 466 | 466 | /* |
| 467 | 467 | ** CAPI3REF: Run-Time Library Version Numbers |
| 468 | 468 | ** KEYWORDS: sqlite3_version sqlite3_sourceid |
| 469 | 469 | ** |
| | @@ -13873,11 +13873,11 @@ |
| 13873 | 13873 | ** This is really just the default value for the max_page_count pragma. |
| 13874 | 13874 | ** This value can be lowered (or raised) at run-time using that the |
| 13875 | 13875 | ** max_page_count macro. |
| 13876 | 13876 | */ |
| 13877 | 13877 | #ifndef SQLITE_MAX_PAGE_COUNT |
| 13878 | | -# define SQLITE_MAX_PAGE_COUNT 1073741823 |
| 13878 | +# define SQLITE_MAX_PAGE_COUNT 0xfffffffe /* 4294967294 */ |
| 13879 | 13879 | #endif |
| 13880 | 13880 | |
| 13881 | 13881 | /* |
| 13882 | 13882 | ** Maximum length (in bytes) of the pattern in a LIKE or GLOB |
| 13883 | 13883 | ** operator. |
| | @@ -43060,20 +43060,20 @@ |
| 43060 | 43060 | #ifdef SQLITE_ENABLE_SETLK_TIMEOUT |
| 43061 | 43061 | int iMutex; |
| 43062 | 43062 | for(iMutex=ofst; iMutex<ofst+n; iMutex++){ |
| 43063 | 43063 | if( flags==(SQLITE_SHM_LOCK|SQLITE_SHM_EXCLUSIVE) ){ |
| 43064 | 43064 | rc = sqlite3_mutex_try(pShmNode->aMutex[iMutex]); |
| 43065 | | - if( rc!=SQLITE_OK ) break; |
| 43065 | + if( rc!=SQLITE_OK ) goto leave_shmnode_mutexes; |
| 43066 | 43066 | }else{ |
| 43067 | 43067 | sqlite3_mutex_enter(pShmNode->aMutex[iMutex]); |
| 43068 | 43068 | } |
| 43069 | 43069 | } |
| 43070 | 43070 | #else |
| 43071 | 43071 | sqlite3_mutex_enter(pShmNode->pShmMutex); |
| 43072 | 43072 | #endif |
| 43073 | 43073 | |
| 43074 | | - if( rc==SQLITE_OK ){ |
| 43074 | + if( ALWAYS(rc==SQLITE_OK) ){ |
| 43075 | 43075 | if( flags & SQLITE_SHM_UNLOCK ){ |
| 43076 | 43076 | /* Case (a) - unlock. */ |
| 43077 | 43077 | int bUnlock = 1; |
| 43078 | 43078 | assert( (p->exclMask & p->sharedMask)==0 ); |
| 43079 | 43079 | assert( !(flags & SQLITE_SHM_EXCLUSIVE) || (p->exclMask & mask)==mask ); |
| | @@ -43148,10 +43148,11 @@ |
| 43148 | 43148 | assert( assertLockingArrayOk(pShmNode) ); |
| 43149 | 43149 | } |
| 43150 | 43150 | |
| 43151 | 43151 | /* Drop the mutexes acquired above. */ |
| 43152 | 43152 | #ifdef SQLITE_ENABLE_SETLK_TIMEOUT |
| 43153 | + leave_shmnode_mutexes: |
| 43153 | 43154 | for(iMutex--; iMutex>=ofst; iMutex--){ |
| 43154 | 43155 | sqlite3_mutex_leave(pShmNode->aMutex[iMutex]); |
| 43155 | 43156 | } |
| 43156 | 43157 | #else |
| 43157 | 43158 | sqlite3_mutex_leave(pShmNode->pShmMutex); |
| | @@ -57503,13 +57504,12 @@ |
| 57503 | 57504 | if( pPager->fd->pMethods==0 ) return 0; |
| 57504 | 57505 | if( sqlite3PCacheIsDirty(pPager->pPCache) ) return 0; |
| 57505 | 57506 | #ifndef SQLITE_OMIT_WAL |
| 57506 | 57507 | if( pPager->pWal ){ |
| 57507 | 57508 | u32 iRead = 0; |
| 57508 | | - int rc; |
| 57509 | | - rc = sqlite3WalFindFrame(pPager->pWal, pgno, &iRead); |
| 57510 | | - return (rc==SQLITE_OK && iRead==0); |
| 57509 | + (void)sqlite3WalFindFrame(pPager->pWal, pgno, &iRead); |
| 57510 | + return iRead==0; |
| 57511 | 57511 | } |
| 57512 | 57512 | #endif |
| 57513 | 57513 | return 1; |
| 57514 | 57514 | } |
| 57515 | 57515 | #endif |
| | @@ -67391,10 +67391,41 @@ |
| 67391 | 67391 | *pChanged = 1; |
| 67392 | 67392 | } |
| 67393 | 67393 | return rc; |
| 67394 | 67394 | } |
| 67395 | 67395 | |
| 67396 | +/* |
| 67397 | +** The final argument passed to walTryBeginRead() is of type (int*). The |
| 67398 | +** caller should invoke walTryBeginRead as follows: |
| 67399 | +** |
| 67400 | +** int cnt = 0; |
| 67401 | +** do { |
| 67402 | +** rc = walTryBeginRead(..., &cnt); |
| 67403 | +** }while( rc==WAL_RETRY ); |
| 67404 | +** |
| 67405 | +** The final value of "cnt" is of no use to the caller. It is used by |
| 67406 | +** the implementation of walTryBeginRead() as follows: |
| 67407 | +** |
| 67408 | +** + Each time walTryBeginRead() is called, it is incremented. Once |
| 67409 | +** it reaches WAL_RETRY_PROTOCOL_LIMIT - indicating that walTryBeginRead() |
| 67410 | +** has many times been invoked and failed with WAL_RETRY - walTryBeginRead() |
| 67411 | +** returns SQLITE_PROTOCOL. |
| 67412 | +** |
| 67413 | +** + If SQLITE_ENABLE_SETLK_TIMEOUT is defined and walTryBeginRead() failed |
| 67414 | +** because a blocking lock timed out (SQLITE_BUSY_TIMEOUT from the OS |
| 67415 | +** layer), the WAL_RETRY_BLOCKED_MASK bit is set in "cnt". In this case |
| 67416 | +** the next invocation of walTryBeginRead() may omit an expected call to |
| 67417 | +** sqlite3OsSleep(). There has already been a delay when the previous call |
| 67418 | +** waited on a lock. |
| 67419 | +*/ |
| 67420 | +#define WAL_RETRY_PROTOCOL_LIMIT 100 |
| 67421 | +#ifdef SQLITE_ENABLE_SETLK_TIMEOUT |
| 67422 | +# define WAL_RETRY_BLOCKED_MASK 0x10000000 |
| 67423 | +#else |
| 67424 | +# define WAL_RETRY_BLOCKED_MASK 0 |
| 67425 | +#endif |
| 67426 | + |
| 67396 | 67427 | /* |
| 67397 | 67428 | ** Attempt to start a read transaction. This might fail due to a race or |
| 67398 | 67429 | ** other transient condition. When that happens, it returns WAL_RETRY to |
| 67399 | 67430 | ** indicate to the caller that it is safe to retry immediately. |
| 67400 | 67431 | ** |
| | @@ -67441,11 +67472,11 @@ |
| 67441 | 67472 | ** checkpoint process do as much work as possible. This routine might |
| 67442 | 67473 | ** update values of the aReadMark[] array in the header, but if it does |
| 67443 | 67474 | ** so it takes care to hold an exclusive lock on the corresponding |
| 67444 | 67475 | ** WAL_READ_LOCK() while changing values. |
| 67445 | 67476 | */ |
| 67446 | | -static int walTryBeginRead(Wal *pWal, int *pChanged, int useWal, int cnt){ |
| 67477 | +static int walTryBeginRead(Wal *pWal, int *pChanged, int useWal, int *pCnt){ |
| 67447 | 67478 | volatile WalCkptInfo *pInfo; /* Checkpoint information in wal-index */ |
| 67448 | 67479 | u32 mxReadMark; /* Largest aReadMark[] value */ |
| 67449 | 67480 | int mxI; /* Index of largest aReadMark[] value */ |
| 67450 | 67481 | int i; /* Loop counter */ |
| 67451 | 67482 | int rc = SQLITE_OK; /* Return code */ |
| | @@ -67474,41 +67505,51 @@ |
| 67474 | 67505 | ** is more of a scheduler yield than an actual delay. But on the 10th |
| 67475 | 67506 | ** an subsequent retries, the delays start becoming longer and longer, |
| 67476 | 67507 | ** so that on the 100th (and last) RETRY we delay for 323 milliseconds. |
| 67477 | 67508 | ** The total delay time before giving up is less than 10 seconds. |
| 67478 | 67509 | */ |
| 67479 | | - if( cnt>5 ){ |
| 67510 | + (*pCnt)++; |
| 67511 | + if( *pCnt>5 ){ |
| 67480 | 67512 | int nDelay = 1; /* Pause time in microseconds */ |
| 67481 | | - if( cnt>100 ){ |
| 67513 | + int cnt = (*pCnt & ~WAL_RETRY_BLOCKED_MASK); |
| 67514 | + if( cnt>WAL_RETRY_PROTOCOL_LIMIT ){ |
| 67482 | 67515 | VVA_ONLY( pWal->lockError = 1; ) |
| 67483 | 67516 | return SQLITE_PROTOCOL; |
| 67484 | 67517 | } |
| 67485 | | - if( cnt>=10 ) nDelay = (cnt-9)*(cnt-9)*39; |
| 67518 | + if( *pCnt>=10 ) nDelay = (cnt-9)*(cnt-9)*39; |
| 67486 | 67519 | #ifdef SQLITE_ENABLE_SETLK_TIMEOUT |
| 67487 | 67520 | /* In SQLITE_ENABLE_SETLK_TIMEOUT builds, configure the file-descriptor |
| 67488 | 67521 | ** to block for locks for approximately nDelay us. This affects three |
| 67489 | 67522 | ** locks: (a) the shared lock taken on the DMS slot in os_unix.c (if |
| 67490 | 67523 | ** using os_unix.c), (b) the WRITER lock taken in walIndexReadHdr() if the |
| 67491 | | - ** first attempted read fails, and (c) the shared lock taken on the DMS |
| 67492 | | - ** slot in os_unix.c. All three of these locks are attempted from within |
| 67493 | | - ** the call to walIndexReadHdr() below. */ |
| 67524 | + ** first attempted read fails, and (c) the shared lock taken on the |
| 67525 | + ** read-mark. |
| 67526 | + ** |
| 67527 | + ** If the previous call failed due to an SQLITE_BUSY_TIMEOUT error, |
| 67528 | + ** then sleep for the minimum of 1us. The previous call already provided |
| 67529 | + ** an extra delay while it was blocking on the lock. |
| 67530 | + */ |
| 67494 | 67531 | nBlockTmout = (nDelay+998) / 1000; |
| 67495 | 67532 | if( !useWal && walEnableBlockingMs(pWal, nBlockTmout) ){ |
| 67496 | | - nDelay = 1; |
| 67533 | + if( *pCnt & WAL_RETRY_BLOCKED_MASK ) nDelay = 1; |
| 67497 | 67534 | } |
| 67498 | 67535 | #endif |
| 67499 | 67536 | sqlite3OsSleep(pWal->pVfs, nDelay); |
| 67537 | + *pCnt &= ~WAL_RETRY_BLOCKED_MASK; |
| 67500 | 67538 | } |
| 67501 | 67539 | |
| 67502 | 67540 | if( !useWal ){ |
| 67503 | 67541 | assert( rc==SQLITE_OK ); |
| 67504 | 67542 | if( pWal->bShmUnreliable==0 ){ |
| 67505 | 67543 | rc = walIndexReadHdr(pWal, pChanged); |
| 67506 | 67544 | } |
| 67507 | 67545 | #ifdef SQLITE_ENABLE_SETLK_TIMEOUT |
| 67508 | 67546 | walDisableBlocking(pWal); |
| 67509 | | - if( rc==SQLITE_BUSY_TIMEOUT ) rc = SQLITE_BUSY; |
| 67547 | + if( rc==SQLITE_BUSY_TIMEOUT ){ |
| 67548 | + rc = SQLITE_BUSY; |
| 67549 | + *pCnt |= WAL_RETRY_BLOCKED_MASK; |
| 67550 | + } |
| 67510 | 67551 | #endif |
| 67511 | 67552 | if( rc==SQLITE_BUSY ){ |
| 67512 | 67553 | /* If there is not a recovery running in another thread or process |
| 67513 | 67554 | ** then convert BUSY errors to WAL_RETRY. If recovery is known to |
| 67514 | 67555 | ** be running, convert BUSY to BUSY_RECOVERY. There is a race here |
| | @@ -67624,10 +67665,13 @@ |
| 67624 | 67665 | |
| 67625 | 67666 | (void)walEnableBlockingMs(pWal, nBlockTmout); |
| 67626 | 67667 | rc = walLockShared(pWal, WAL_READ_LOCK(mxI)); |
| 67627 | 67668 | walDisableBlocking(pWal); |
| 67628 | 67669 | if( rc ){ |
| 67670 | + if( rc==SQLITE_BUSY_TIMEOUT ){ |
| 67671 | + *pCnt |= WAL_RETRY_BLOCKED_MASK; |
| 67672 | + } |
| 67629 | 67673 | assert( (rc&0xFF)!=SQLITE_BUSY||rc==SQLITE_BUSY||rc==SQLITE_BUSY_TIMEOUT ); |
| 67630 | 67674 | return (rc&0xFF)==SQLITE_BUSY ? WAL_RETRY : rc; |
| 67631 | 67675 | } |
| 67632 | 67676 | /* Now that the read-lock has been obtained, check that neither the |
| 67633 | 67677 | ** value in the aReadMark[] array or the contents of the wal-index |
| | @@ -67813,11 +67857,11 @@ |
| 67813 | 67857 | ckptLock = 1; |
| 67814 | 67858 | } |
| 67815 | 67859 | #endif |
| 67816 | 67860 | |
| 67817 | 67861 | do{ |
| 67818 | | - rc = walTryBeginRead(pWal, pChanged, 0, ++cnt); |
| 67862 | + rc = walTryBeginRead(pWal, pChanged, 0, &cnt); |
| 67819 | 67863 | }while( rc==WAL_RETRY ); |
| 67820 | 67864 | testcase( (rc&0xff)==SQLITE_BUSY ); |
| 67821 | 67865 | testcase( (rc&0xff)==SQLITE_IOERR ); |
| 67822 | 67866 | testcase( rc==SQLITE_PROTOCOL ); |
| 67823 | 67867 | testcase( rc==SQLITE_OK ); |
| | @@ -67994,10 +68038,11 @@ |
| 67994 | 68038 | if( iFrame<=iLast && iFrame>=pWal->minFrame && sLoc.aPgno[iH-1]==pgno ){ |
| 67995 | 68039 | assert( iFrame>iRead || CORRUPT_DB ); |
| 67996 | 68040 | iRead = iFrame; |
| 67997 | 68041 | } |
| 67998 | 68042 | if( (nCollide--)==0 ){ |
| 68043 | + *piRead = 0; |
| 67999 | 68044 | return SQLITE_CORRUPT_BKPT; |
| 68000 | 68045 | } |
| 68001 | 68046 | iKey = walNextHash(iKey); |
| 68002 | 68047 | } |
| 68003 | 68048 | if( iRead ) break; |
| | @@ -68297,11 +68342,11 @@ |
| 68297 | 68342 | walUnlockShared(pWal, WAL_READ_LOCK(0)); |
| 68298 | 68343 | pWal->readLock = -1; |
| 68299 | 68344 | cnt = 0; |
| 68300 | 68345 | do{ |
| 68301 | 68346 | int notUsed; |
| 68302 | | - rc = walTryBeginRead(pWal, ¬Used, 1, ++cnt); |
| 68347 | + rc = walTryBeginRead(pWal, ¬Used, 1, &cnt); |
| 68303 | 68348 | }while( rc==WAL_RETRY ); |
| 68304 | 68349 | assert( (rc&0xff)!=SQLITE_BUSY ); /* BUSY not possible when useWal==1 */ |
| 68305 | 68350 | testcase( (rc&0xff)==SQLITE_IOERR ); |
| 68306 | 68351 | testcase( rc==SQLITE_PROTOCOL ); |
| 68307 | 68352 | testcase( rc==SQLITE_OK ); |
| | @@ -75232,11 +75277,10 @@ |
| 75232 | 75277 | u8 aSave[4]; |
| 75233 | 75278 | u8 *aWrite = &pBuf[-4]; |
| 75234 | 75279 | assert( aWrite>=pBufStart ); /* due to (6) */ |
| 75235 | 75280 | memcpy(aSave, aWrite, 4); |
| 75236 | 75281 | rc = sqlite3OsRead(fd, aWrite, a+4, (i64)pBt->pageSize*(nextPage-1)); |
| 75237 | | - if( rc && nextPage>pBt->nPage ) rc = SQLITE_CORRUPT_BKPT; |
| 75238 | 75282 | nextPage = get4byte(aWrite); |
| 75239 | 75283 | memcpy(aWrite, aSave, 4); |
| 75240 | 75284 | }else |
| 75241 | 75285 | #endif |
| 75242 | 75286 | |
| | @@ -122158,11 +122202,12 @@ |
| 122158 | 122202 | char aff = SQLITE_AFF_NUMERIC; |
| 122159 | 122203 | const char *zChar = 0; |
| 122160 | 122204 | |
| 122161 | 122205 | assert( zIn!=0 ); |
| 122162 | 122206 | while( zIn[0] ){ |
| 122163 | | - h = (h<<8) + sqlite3UpperToLower[(*zIn)&0xff]; |
| 122207 | + u8 x = *(u8*)zIn; |
| 122208 | + h = (h<<8) + sqlite3UpperToLower[x]; |
| 122164 | 122209 | zIn++; |
| 122165 | 122210 | if( h==(('c'<<24)+('h'<<16)+('a'<<8)+'r') ){ /* CHAR */ |
| 122166 | 122211 | aff = SQLITE_AFF_TEXT; |
| 122167 | 122212 | zChar = zIn; |
| 122168 | 122213 | }else if( h==(('c'<<24)+('l'<<16)+('o'<<8)+'b') ){ /* CLOB */ |
| | @@ -126023,20 +126068,21 @@ |
| 126023 | 126068 | } |
| 126024 | 126069 | iDb = sqlite3TwoPartName(pParse, pName1, pName2, &pObjName); |
| 126025 | 126070 | if( iDb<0 ) return; |
| 126026 | 126071 | z = sqlite3NameFromToken(db, pObjName); |
| 126027 | 126072 | if( z==0 ) return; |
| 126028 | | - zDb = db->aDb[iDb].zDbSName; |
| 126073 | + zDb = pName2->n ? db->aDb[iDb].zDbSName : 0; |
| 126029 | 126074 | pTab = sqlite3FindTable(db, z, zDb); |
| 126030 | 126075 | if( pTab ){ |
| 126031 | 126076 | reindexTable(pParse, pTab, 0); |
| 126032 | 126077 | sqlite3DbFree(db, z); |
| 126033 | 126078 | return; |
| 126034 | 126079 | } |
| 126035 | 126080 | pIndex = sqlite3FindIndex(db, z, zDb); |
| 126036 | 126081 | sqlite3DbFree(db, z); |
| 126037 | 126082 | if( pIndex ){ |
| 126083 | + iDb = sqlite3SchemaToIndex(db, pIndex->pTable->pSchema); |
| 126038 | 126084 | sqlite3BeginWriteOperation(pParse, 0, iDb); |
| 126039 | 126085 | sqlite3RefillIndex(pParse, pIndex, -1); |
| 126040 | 126086 | return; |
| 126041 | 126087 | } |
| 126042 | 126088 | sqlite3ErrorMsg(pParse, "unable to identify the object to be reindexed"); |
| | @@ -182703,11 +182749,11 @@ |
| 182703 | 182749 | ** If onOff is initially 1, then turn it on. If onOff is initially |
| 182704 | 182750 | ** off, turn it off. If onOff is initially -1, then change onOff |
| 182705 | 182751 | ** to be the current setting. |
| 182706 | 182752 | */ |
| 182707 | 182753 | case SQLITE_TESTCTRL_JSON_SELFCHECK: { |
| 182708 | | -#if defined(SQLITE_DEBUG) |
| 182754 | +#if defined(SQLITE_DEBUG) && !defined(SQLITE_OMIT_WSD) |
| 182709 | 182755 | int *pOnOff = va_arg(ap, int*); |
| 182710 | 182756 | if( *pOnOff<0 ){ |
| 182711 | 182757 | *pOnOff = sqlite3Config.bJsonSelfcheck; |
| 182712 | 182758 | }else{ |
| 182713 | 182759 | sqlite3Config.bJsonSelfcheck = (u8)((*pOnOff)&0xff); |
| | @@ -203415,10 +203461,11 @@ |
| 203415 | 203461 | struct JsonParse { |
| 203416 | 203462 | u8 *aBlob; /* JSONB representation of JSON value */ |
| 203417 | 203463 | u32 nBlob; /* Bytes of aBlob[] actually used */ |
| 203418 | 203464 | u32 nBlobAlloc; /* Bytes allocated to aBlob[]. 0 if aBlob is external */ |
| 203419 | 203465 | char *zJson; /* Json text used for parsing */ |
| 203466 | + sqlite3 *db; /* The database connection to which this object belongs */ |
| 203420 | 203467 | int nJson; /* Length of the zJson string in bytes */ |
| 203421 | 203468 | u32 nJPRef; /* Number of references to this object */ |
| 203422 | 203469 | u32 iErr; /* Error location in zJson[] */ |
| 203423 | 203470 | u16 iDepth; /* Nesting depth */ |
| 203424 | 203471 | u8 nErr; /* Number of errors seen */ |
| | @@ -203940,11 +203987,11 @@ |
| 203940 | 203987 | pParse->zJson = 0; |
| 203941 | 203988 | pParse->nJson = 0; |
| 203942 | 203989 | pParse->bJsonIsRCStr = 0; |
| 203943 | 203990 | } |
| 203944 | 203991 | if( pParse->nBlobAlloc ){ |
| 203945 | | - sqlite3_free(pParse->aBlob); |
| 203992 | + sqlite3DbFree(pParse->db, pParse->aBlob); |
| 203946 | 203993 | pParse->aBlob = 0; |
| 203947 | 203994 | pParse->nBlob = 0; |
| 203948 | 203995 | pParse->nBlobAlloc = 0; |
| 203949 | 203996 | } |
| 203950 | 203997 | } |
| | @@ -203957,11 +204004,11 @@ |
| 203957 | 204004 | if( pParse ){ |
| 203958 | 204005 | if( pParse->nJPRef>1 ){ |
| 203959 | 204006 | pParse->nJPRef--; |
| 203960 | 204007 | }else{ |
| 203961 | 204008 | jsonParseReset(pParse); |
| 203962 | | - sqlite3_free(pParse); |
| 204009 | + sqlite3DbFree(pParse->db, pParse); |
| 203963 | 204010 | } |
| 203964 | 204011 | } |
| 203965 | 204012 | } |
| 203966 | 204013 | |
| 203967 | 204014 | /************************************************************************** |
| | @@ -204188,11 +204235,11 @@ |
| 204188 | 204235 | t = 100; |
| 204189 | 204236 | }else{ |
| 204190 | 204237 | t = pParse->nBlobAlloc*2; |
| 204191 | 204238 | } |
| 204192 | 204239 | if( t<N ) t = N+100; |
| 204193 | | - aNew = sqlite3_realloc64( pParse->aBlob, t ); |
| 204240 | + aNew = sqlite3DbRealloc(pParse->db, pParse->aBlob, t); |
| 204194 | 204241 | if( aNew==0 ){ pParse->oom = 1; return 1; } |
| 204195 | 204242 | pParse->aBlob = aNew; |
| 204196 | 204243 | pParse->nBlobAlloc = t; |
| 204197 | 204244 | return 0; |
| 204198 | 204245 | } |
| | @@ -205113,18 +205160,19 @@ |
| 205113 | 205160 | JsonParse px; |
| 205114 | 205161 | memset(&px, 0, sizeof(px)); |
| 205115 | 205162 | jsonStringTerminate(pStr); |
| 205116 | 205163 | px.zJson = pStr->zBuf; |
| 205117 | 205164 | px.nJson = pStr->nUsed; |
| 205165 | + px.db = sqlite3_context_db_handle(pStr->pCtx); |
| 205118 | 205166 | (void)jsonTranslateTextToBlob(&px, 0); |
| 205119 | 205167 | if( px.oom ){ |
| 205120 | | - sqlite3_free(px.aBlob); |
| 205168 | + sqlite3DbFree(px.db, px.aBlob); |
| 205121 | 205169 | sqlite3_result_error_nomem(pStr->pCtx); |
| 205122 | 205170 | }else{ |
| 205123 | 205171 | assert( px.nBlobAlloc>0 ); |
| 205124 | 205172 | assert( !px.bReadOnly ); |
| 205125 | | - sqlite3_result_blob(pStr->pCtx, px.aBlob, px.nBlob, sqlite3_free); |
| 205173 | + sqlite3_result_blob(pStr->pCtx, px.aBlob, px.nBlob, SQLITE_DYNAMIC); |
| 205126 | 205174 | } |
| 205127 | 205175 | } |
| 205128 | 205176 | |
| 205129 | 205177 | /* The byte at index i is a node type-code. This routine |
| 205130 | 205178 | ** determines the payload size for that node and writes that |
| | @@ -205736,10 +205784,11 @@ |
| 205736 | 205784 | const char *zTail /* Tail of the path that determins substructure */ |
| 205737 | 205785 | ){ |
| 205738 | 205786 | static const u8 emptyObject[] = { JSONB_ARRAY, JSONB_OBJECT }; |
| 205739 | 205787 | int rc; |
| 205740 | 205788 | memset(pIns, 0, sizeof(*pIns)); |
| 205789 | + pIns->db = pParse->db; |
| 205741 | 205790 | if( zTail[0]==0 ){ |
| 205742 | 205791 | /* No substructure. Just insert what is given in pParse. */ |
| 205743 | 205792 | pIns->aBlob = pParse->aIns; |
| 205744 | 205793 | pIns->nBlob = pParse->nIns; |
| 205745 | 205794 | rc = 0; |
| | @@ -205863,10 +205912,11 @@ |
| 205863 | 205912 | JsonParse v; /* BLOB encoding of the value to be inserted */ |
| 205864 | 205913 | JsonParse ix; /* Header of the label to be inserted */ |
| 205865 | 205914 | testcase( pParse->eEdit==JEDIT_INS ); |
| 205866 | 205915 | testcase( pParse->eEdit==JEDIT_SET ); |
| 205867 | 205916 | memset(&ix, 0, sizeof(ix)); |
| 205917 | + ix.db = pParse->db; |
| 205868 | 205918 | jsonBlobAppendNode(&ix, rawKey?JSONB_TEXTRAW:JSONB_TEXT5, nKey, 0); |
| 205869 | 205919 | pParse->oom |= ix.oom; |
| 205870 | 205920 | rc = jsonCreateEditSubstructure(pParse, &v, &zPath[i]); |
| 205871 | 205921 | if( !JSON_LOOKUP_ISERROR(rc) |
| 205872 | 205922 | && jsonBlobMakeEditable(pParse, ix.nBlob+nKey+v.nBlob) |
| | @@ -206073,11 +206123,11 @@ |
| 206073 | 206123 | u32 iIn, iOut; |
| 206074 | 206124 | const char *z; |
| 206075 | 206125 | char *zOut; |
| 206076 | 206126 | u32 nOut = sz; |
| 206077 | 206127 | z = (const char*)&pParse->aBlob[i+n]; |
| 206078 | | - zOut = sqlite3_malloc( nOut+1 ); |
| 206128 | + zOut = sqlite3DbMallocRaw(db, nOut+1); |
| 206079 | 206129 | if( zOut==0 ) goto returnfromblob_oom; |
| 206080 | 206130 | for(iIn=iOut=0; iIn<sz; iIn++){ |
| 206081 | 206131 | char c = z[iIn]; |
| 206082 | 206132 | if( c=='\\' ){ |
| 206083 | 206133 | u32 v; |
| | @@ -206107,11 +206157,11 @@ |
| 206107 | 206157 | zOut[iOut++] = c; |
| 206108 | 206158 | } |
| 206109 | 206159 | } /* end for() */ |
| 206110 | 206160 | assert( iOut<=nOut ); |
| 206111 | 206161 | zOut[iOut] = 0; |
| 206112 | | - sqlite3_result_text(pCtx, zOut, iOut, sqlite3_free); |
| 206162 | + sqlite3_result_text(pCtx, zOut, iOut, SQLITE_DYNAMIC); |
| 206113 | 206163 | break; |
| 206114 | 206164 | } |
| 206115 | 206165 | case JSONB_ARRAY: |
| 206116 | 206166 | case JSONB_OBJECT: { |
| 206117 | 206167 | int flags = textOnly ? 0 : SQLITE_PTR_TO_INT(sqlite3_user_data(pCtx)); |
| | @@ -206160,10 +206210,11 @@ |
| 206160 | 206210 | JsonParse *pParse |
| 206161 | 206211 | ){ |
| 206162 | 206212 | int eType = sqlite3_value_type(pArg); |
| 206163 | 206213 | static u8 aNull[] = { 0x00 }; |
| 206164 | 206214 | memset(pParse, 0, sizeof(pParse[0])); |
| 206215 | + pParse->db = sqlite3_context_db_handle(ctx); |
| 206165 | 206216 | switch( eType ){ |
| 206166 | 206217 | default: { |
| 206167 | 206218 | pParse->aBlob = aNull; |
| 206168 | 206219 | pParse->nBlob = 1; |
| 206169 | 206220 | return 0; |
| | @@ -206185,11 +206236,11 @@ |
| 206185 | 206236 | if( sqlite3_value_subtype(pArg)==JSON_SUBTYPE ){ |
| 206186 | 206237 | pParse->zJson = (char*)zJson; |
| 206187 | 206238 | pParse->nJson = nJson; |
| 206188 | 206239 | if( jsonConvertTextToBlob(pParse, ctx) ){ |
| 206189 | 206240 | sqlite3_result_error(ctx, "malformed JSON", -1); |
| 206190 | | - sqlite3_free(pParse->aBlob); |
| 206241 | + sqlite3DbFree(pParse->db, pParse->aBlob); |
| 206191 | 206242 | memset(pParse, 0, sizeof(pParse[0])); |
| 206192 | 206243 | return 1; |
| 206193 | 206244 | } |
| 206194 | 206245 | }else{ |
| 206195 | 206246 | jsonBlobAppendNode(pParse, JSONB_TEXTRAW, nJson, zJson); |
| | @@ -206342,10 +206393,11 @@ |
| 206342 | 206393 | u32 flgs |
| 206343 | 206394 | ){ |
| 206344 | 206395 | int eType; /* Datatype of pArg */ |
| 206345 | 206396 | JsonParse *p = 0; /* Value to be returned */ |
| 206346 | 206397 | JsonParse *pFromCache = 0; /* Value taken from cache */ |
| 206398 | + sqlite3 *db; /* The database connection */ |
| 206347 | 206399 | |
| 206348 | 206400 | assert( ctx!=0 ); |
| 206349 | 206401 | eType = sqlite3_value_type(pArg); |
| 206350 | 206402 | if( eType==SQLITE_NULL ){ |
| 206351 | 206403 | return 0; |
| | @@ -206355,18 +206407,20 @@ |
| 206355 | 206407 | pFromCache->nJPRef++; |
| 206356 | 206408 | if( (flgs & JSON_EDITABLE)==0 ){ |
| 206357 | 206409 | return pFromCache; |
| 206358 | 206410 | } |
| 206359 | 206411 | } |
| 206412 | + db = sqlite3_context_db_handle(ctx); |
| 206360 | 206413 | rebuild_from_cache: |
| 206361 | | - p = sqlite3_malloc64( sizeof(*p) ); |
| 206414 | + p = sqlite3DbMallocZero(db, sizeof(*p)); |
| 206362 | 206415 | if( p==0 ) goto json_pfa_oom; |
| 206363 | 206416 | memset(p, 0, sizeof(*p)); |
| 206417 | + p->db = db; |
| 206364 | 206418 | p->nJPRef = 1; |
| 206365 | 206419 | if( pFromCache!=0 ){ |
| 206366 | 206420 | u32 nBlob = pFromCache->nBlob; |
| 206367 | | - p->aBlob = sqlite3_malloc64( nBlob ); |
| 206421 | + p->aBlob = sqlite3DbMallocRaw(db, nBlob); |
| 206368 | 206422 | if( p->aBlob==0 ) goto json_pfa_oom; |
| 206369 | 206423 | memcpy(p->aBlob, pFromCache->aBlob, nBlob); |
| 206370 | 206424 | p->nBlobAlloc = p->nBlob = nBlob; |
| 206371 | 206425 | p->hasNonstd = pFromCache->hasNonstd; |
| 206372 | 206426 | jsonParseFree(pFromCache); |
| | @@ -206618,11 +206672,11 @@ |
| 206618 | 206672 | sqlite3StrAccumInit(&out, 0, 0, 0, 1000000); |
| 206619 | 206673 | p = jsonParseFuncArg(ctx, argv[0], 0); |
| 206620 | 206674 | if( p==0 ) return; |
| 206621 | 206675 | if( argc==1 ){ |
| 206622 | 206676 | jsonDebugPrintBlob(p, 0, p->nBlob, 0, &out); |
| 206623 | | - sqlite3_result_text64(ctx, out.zText, out.nChar, sqlite3_free, SQLITE_UTF8); |
| 206677 | + sqlite3_result_text64(ctx, out.zText, out.nChar, SQLITE_DYNAMIC, SQLITE_UTF8); |
| 206624 | 206678 | }else{ |
| 206625 | 206679 | jsonShowParse(p); |
| 206626 | 206680 | } |
| 206627 | 206681 | jsonParseFree(p); |
| 206628 | 206682 | } |
| | @@ -207437,10 +207491,11 @@ |
| 207437 | 207491 | JsonParse s; |
| 207438 | 207492 | |
| 207439 | 207493 | assert( argc==1 ); |
| 207440 | 207494 | UNUSED_PARAMETER(argc); |
| 207441 | 207495 | memset(&s, 0, sizeof(s)); |
| 207496 | + s.db = sqlite3_context_db_handle(ctx); |
| 207442 | 207497 | if( jsonFuncArgMightBeBinary(argv[0]) ){ |
| 207443 | 207498 | s.aBlob = (u8*)sqlite3_value_blob(argv[0]); |
| 207444 | 207499 | s.nBlob = sqlite3_value_bytes(argv[0]); |
| 207445 | 207500 | iErrPos = (i64)jsonbValidityCheck(&s, 0, s.nBlob, 1); |
| 207446 | 207501 | }else{ |
| | @@ -207726,34 +207781,34 @@ |
| 207726 | 207781 | UNUSED_PARAMETER(pAux); |
| 207727 | 207782 | rc = sqlite3_declare_vtab(db, |
| 207728 | 207783 | "CREATE TABLE x(key,value,type,atom,id,parent,fullkey,path," |
| 207729 | 207784 | "json HIDDEN,root HIDDEN)"); |
| 207730 | 207785 | if( rc==SQLITE_OK ){ |
| 207731 | | - pNew = (JsonEachConnection*)(*ppVtab = sqlite3_malloc( sizeof(*pNew) )); |
| 207786 | + pNew = (JsonEachConnection*)sqlite3DbMallocZero(db, sizeof(*pNew)); |
| 207787 | + *ppVtab = (sqlite3_vtab*)pNew; |
| 207732 | 207788 | if( pNew==0 ) return SQLITE_NOMEM; |
| 207733 | | - memset(pNew, 0, sizeof(*pNew)); |
| 207734 | 207789 | sqlite3_vtab_config(db, SQLITE_VTAB_INNOCUOUS); |
| 207735 | 207790 | pNew->db = db; |
| 207736 | 207791 | } |
| 207737 | 207792 | return rc; |
| 207738 | 207793 | } |
| 207739 | 207794 | |
| 207740 | 207795 | /* destructor for json_each virtual table */ |
| 207741 | 207796 | static int jsonEachDisconnect(sqlite3_vtab *pVtab){ |
| 207742 | | - sqlite3_free(pVtab); |
| 207797 | + JsonEachConnection *p = (JsonEachConnection*)pVtab; |
| 207798 | + sqlite3DbFree(p->db, pVtab); |
| 207743 | 207799 | return SQLITE_OK; |
| 207744 | 207800 | } |
| 207745 | 207801 | |
| 207746 | 207802 | /* constructor for a JsonEachCursor object for json_each(). */ |
| 207747 | 207803 | static int jsonEachOpenEach(sqlite3_vtab *p, sqlite3_vtab_cursor **ppCursor){ |
| 207748 | 207804 | JsonEachConnection *pVtab = (JsonEachConnection*)p; |
| 207749 | 207805 | JsonEachCursor *pCur; |
| 207750 | 207806 | |
| 207751 | 207807 | UNUSED_PARAMETER(p); |
| 207752 | | - pCur = sqlite3_malloc( sizeof(*pCur) ); |
| 207808 | + pCur = sqlite3DbMallocZero(pVtab->db, sizeof(*pCur)); |
| 207753 | 207809 | if( pCur==0 ) return SQLITE_NOMEM; |
| 207754 | | - memset(pCur, 0, sizeof(*pCur)); |
| 207755 | 207810 | pCur->db = pVtab->db; |
| 207756 | 207811 | jsonStringZero(&pCur->path); |
| 207757 | 207812 | *ppCursor = &pCur->base; |
| 207758 | 207813 | return SQLITE_OK; |
| 207759 | 207814 | } |
| | @@ -207786,11 +207841,11 @@ |
| 207786 | 207841 | /* Destructor for a jsonEachCursor object */ |
| 207787 | 207842 | static int jsonEachClose(sqlite3_vtab_cursor *cur){ |
| 207788 | 207843 | JsonEachCursor *p = (JsonEachCursor*)cur; |
| 207789 | 207844 | jsonEachCursorReset(p); |
| 207790 | 207845 | |
| 207791 | | - sqlite3_free(cur); |
| 207846 | + sqlite3DbFree(p->db, cur); |
| 207792 | 207847 | return SQLITE_OK; |
| 207793 | 207848 | } |
| 207794 | 207849 | |
| 207795 | 207850 | /* Return TRUE if the jsonEachCursor object has been advanced off the end |
| 207796 | 207851 | ** of the JSON object */ |
| | @@ -208120,10 +208175,11 @@ |
| 208120 | 208175 | UNUSED_PARAMETER(argc); |
| 208121 | 208176 | jsonEachCursorReset(p); |
| 208122 | 208177 | if( idxNum==0 ) return SQLITE_OK; |
| 208123 | 208178 | memset(&p->sParse, 0, sizeof(p->sParse)); |
| 208124 | 208179 | p->sParse.nJPRef = 1; |
| 208180 | + p->sParse.db = p->db; |
| 208125 | 208181 | if( sqlite3_value_type(argv[0])==SQLITE_BLOB ){ |
| 208126 | 208182 | if( jsonFuncArgMightBeBinary(argv[0]) ){ |
| 208127 | 208183 | p->sParse.nBlob = sqlite3_value_bytes(argv[0]); |
| 208128 | 208184 | p->sParse.aBlob = (u8*)sqlite3_value_blob(argv[0]); |
| 208129 | 208185 | }else{ |
| | @@ -209071,11 +209127,11 @@ |
| 209071 | 209127 | |
| 209072 | 209128 | /* Check if the requested node is already in the hash table. If so, |
| 209073 | 209129 | ** increase its reference count and return it. |
| 209074 | 209130 | */ |
| 209075 | 209131 | if( (pNode = nodeHashLookup(pRtree, iNode))!=0 ){ |
| 209076 | | - if( pParent && pParent!=pNode->pParent ){ |
| 209132 | + if( pParent && ALWAYS(pParent!=pNode->pParent) ){ |
| 209077 | 209133 | RTREE_IS_CORRUPT(pRtree); |
| 209078 | 209134 | return SQLITE_CORRUPT_VTAB; |
| 209079 | 209135 | } |
| 209080 | 209136 | pNode->nRef++; |
| 209081 | 209137 | *ppNode = pNode; |
| | @@ -211806,11 +211862,11 @@ |
| 211806 | 211862 | }else{ |
| 211807 | 211863 | rc = SQLITE_NOMEM; |
| 211808 | 211864 | } |
| 211809 | 211865 | sqlite3_free(zSql); |
| 211810 | 211866 | } |
| 211811 | | - if( pRtree->nAux ){ |
| 211867 | + if( pRtree->nAux && rc!=SQLITE_NOMEM ){ |
| 211812 | 211868 | pRtree->zReadAuxSql = sqlite3_mprintf( |
| 211813 | 211869 | "SELECT * FROM \"%w\".\"%w_rowid\" WHERE rowid=?1", |
| 211814 | 211870 | zDb, zPrefix); |
| 211815 | 211871 | if( pRtree->zReadAuxSql==0 ){ |
| 211816 | 211872 | rc = SQLITE_NOMEM; |
| | @@ -212495,19 +212551,17 @@ |
| 212495 | 212551 | check.db = db; |
| 212496 | 212552 | check.zDb = zDb; |
| 212497 | 212553 | check.zTab = zTab; |
| 212498 | 212554 | |
| 212499 | 212555 | /* Find the number of auxiliary columns */ |
| 212500 | | - if( check.rc==SQLITE_OK ){ |
| 212501 | | - pStmt = rtreeCheckPrepare(&check, "SELECT * FROM %Q.'%q_rowid'", zDb, zTab); |
| 212502 | | - if( pStmt ){ |
| 212503 | | - nAux = sqlite3_column_count(pStmt) - 2; |
| 212504 | | - sqlite3_finalize(pStmt); |
| 212505 | | - }else |
| 212506 | | - if( check.rc!=SQLITE_NOMEM ){ |
| 212507 | | - check.rc = SQLITE_OK; |
| 212508 | | - } |
| 212556 | + pStmt = rtreeCheckPrepare(&check, "SELECT * FROM %Q.'%q_rowid'", zDb, zTab); |
| 212557 | + if( pStmt ){ |
| 212558 | + nAux = sqlite3_column_count(pStmt) - 2; |
| 212559 | + sqlite3_finalize(pStmt); |
| 212560 | + }else |
| 212561 | + if( check.rc!=SQLITE_NOMEM ){ |
| 212562 | + check.rc = SQLITE_OK; |
| 212509 | 212563 | } |
| 212510 | 212564 | |
| 212511 | 212565 | /* Find number of dimensions in the rtree table. */ |
| 212512 | 212566 | pStmt = rtreeCheckPrepare(&check, "SELECT * FROM %Q.%Q", zDb, zTab); |
| 212513 | 212567 | if( pStmt ){ |
| | @@ -212558,10 +212612,11 @@ |
| 212558 | 212612 | UNUSED_PARAMETER(isQuick); |
| 212559 | 212613 | rc = rtreeCheckTable(pRtree->db, pRtree->zDb, pRtree->zName, pzErr); |
| 212560 | 212614 | if( rc==SQLITE_OK && *pzErr ){ |
| 212561 | 212615 | *pzErr = sqlite3_mprintf("In RTree %s.%s:\n%z", |
| 212562 | 212616 | pRtree->zDb, pRtree->zName, *pzErr); |
| 212617 | + if( (*pzErr)==0 ) rc = SQLITE_NOMEM; |
| 212563 | 212618 | } |
| 212564 | 212619 | return rc; |
| 212565 | 212620 | } |
| 212566 | 212621 | |
| 212567 | 212622 | /* |
| | @@ -250229,12 +250284,12 @@ |
| 250229 | 250284 | static int fts5RollbackToMethod(sqlite3_vtab *pVtab, int iSavepoint){ |
| 250230 | 250285 | Fts5FullTable *pTab = (Fts5FullTable*)pVtab; |
| 250231 | 250286 | int rc = SQLITE_OK; |
| 250232 | 250287 | fts5CheckTransactionState(pTab, FTS5_ROLLBACKTO, iSavepoint); |
| 250233 | 250288 | fts5TripCursors(pTab); |
| 250234 | | - pTab->p.pConfig->pgsz = 0; |
| 250235 | 250289 | if( (iSavepoint+1)<=pTab->iSavepoint ){ |
| 250290 | + pTab->p.pConfig->pgsz = 0; |
| 250236 | 250291 | rc = sqlite3Fts5StorageRollback(pTab->pStorage); |
| 250237 | 250292 | } |
| 250238 | 250293 | return rc; |
| 250239 | 250294 | } |
| 250240 | 250295 | |
| | @@ -250435,11 +250490,11 @@ |
| 250435 | 250490 | int nArg, /* Number of args */ |
| 250436 | 250491 | sqlite3_value **apUnused /* Function arguments */ |
| 250437 | 250492 | ){ |
| 250438 | 250493 | assert( nArg==0 ); |
| 250439 | 250494 | UNUSED_PARAM2(nArg, apUnused); |
| 250440 | | - sqlite3_result_text(pCtx, "fts5: 2023-12-29 19:03:01 4b70b94616ef37bac969051eee3ea6913a28f30520cdd4fc3a19e848f2cf12b7", -1, SQLITE_TRANSIENT); |
| 250495 | + sqlite3_result_text(pCtx, "fts5: 2024-01-08 19:55:40 cd016f26bb61549a304f2148035e050f76a8f4a35cdb7131bba2f5fc5d09f49e", -1, SQLITE_TRANSIENT); |
| 250441 | 250496 | } |
| 250442 | 250497 | |
| 250443 | 250498 | /* |
| 250444 | 250499 | ** Return true if zName is the extension on one of the shadow tables used |
| 250445 | 250500 | ** by this module. |
| 250446 | 250501 | |