diff options
author | drh <> | 2025-06-02 09:49:07 +0000 |
---|---|---|
committer | drh <> | 2025-06-02 09:49:07 +0000 |
commit | 7590bfd7fc01718a6666e8f927aab839ce136cc2 (patch) | |
tree | 9d029d168f976cee98f26f5597a4dcb9092dbffe /src | |
parent | d4c224b833b7c4d58f1e5748f5a1de2af013fc3d (diff) | |
download | sqlite-7590bfd7fc01718a6666e8f927aab839ce136cc2.tar.gz sqlite-7590bfd7fc01718a6666e8f927aab839ce136cc2.zip |
Fix stale comments related to KeyInfo. Add new assert()s associated with
memory management of KeyInfo.
FossilOrigin-Name: abd805bc76f14ede7359b029908179b7ca57e929c5918acae1403ef73ae0bd47
Diffstat (limited to 'src')
-rw-r--r-- | src/sqliteInt.h | 7 | ||||
-rw-r--r-- | src/vdbeaux.c | 20 | ||||
-rw-r--r-- | src/vdbesort.c | 8 |
3 files changed, 19 insertions, 16 deletions
diff --git a/src/sqliteInt.h b/src/sqliteInt.h index 994a3864c..230646200 100644 --- a/src/sqliteInt.h +++ b/src/sqliteInt.h @@ -2644,9 +2644,10 @@ struct FKey { ** argument to sqlite3VdbeKeyCompare and is used to control the ** comparison of the two index keys. ** -** Note that aSortOrder[] and aColl[] have nField+1 slots. There -** are nField slots for the columns of an index then one extra slot -** for the rowid at the end. +** The aSortOrder[] and aColl[] arrays have nAllField slots each. There +** are nKeyField slots for the columns of an index then extra slots +** for the rowid or key at the end. The aSortOrder array is located after +** the aColl[] array. */ struct KeyInfo { u32 nRef; /* Number of references to this KeyInfo object */ diff --git a/src/vdbeaux.c b/src/vdbeaux.c index a6798e62d..2d63654d0 100644 --- a/src/vdbeaux.c +++ b/src/vdbeaux.c @@ -4202,24 +4202,17 @@ void sqlite3VdbeSerialGet( return; } /* -** This routine is used to allocate sufficient space for an UnpackedRecord -** structure large enough to be used with sqlite3VdbeRecordUnpack() if -** the first argument is a pointer to KeyInfo structure pKeyInfo. +** Allocate sufficient space for an UnpackedRecord structure large enough +** to hold a decoded index record for pKeyInfo. ** -** The space is either allocated using sqlite3DbMallocRaw() or from within -** the unaligned buffer passed via the second and third arguments (presumably -** stack space). If the former, then *ppFree is set to a pointer that should -** be eventually freed by the caller using sqlite3DbFree(). Or, if the -** allocation comes from the pSpace/szSpace buffer, *ppFree is set to NULL -** before returning. -** -** If an OOM error occurs, NULL is returned. +** The space is allocated using sqlite3DbMallocRaw(). If an OOM error +** occurs, NULL is returned. */ UnpackedRecord *sqlite3VdbeAllocUnpackedRecord( KeyInfo *pKeyInfo /* Description of the record */ ){ UnpackedRecord *p; /* Unpacked record to return */ - int nByte; /* Number of bytes required for *p */ + u64 nByte; /* Number of bytes required for *p */ assert( sizeof(UnpackedRecord) + sizeof(Mem)*65536 < 0x7fffffff ); nByte = ROUND8P(sizeof(UnpackedRecord)) + sizeof(Mem)*(pKeyInfo->nKeyField+1); p = (UnpackedRecord *)sqlite3DbMallocRaw(pKeyInfo->db, nByte); @@ -4250,6 +4243,7 @@ void sqlite3VdbeRecordUnpack( Mem *pMem = p->aMem; p->default_rc = 0; + assert( p->pKeyInfo==pKeyInfo ); assert( EIGHT_BYTE_ALIGNMENT(pMem) ); idx = getVarint32(aKey, szHdr); d = szHdr; @@ -4275,6 +4269,8 @@ void sqlite3VdbeRecordUnpack( ** warnings from MSAN. */ sqlite3VdbeMemSetNull(pMem-1); } + testcase( u == pKeyInfo->nKeyField + 1 ); + testcase( u < pKeyInfo->nKeyField + 1 ); assert( u<=pKeyInfo->nKeyField + 1 ); p->nField = u; } diff --git a/src/vdbesort.c b/src/vdbesort.c index 6b1b4cff5..90ab28cd5 100644 --- a/src/vdbesort.c +++ b/src/vdbesort.c @@ -969,7 +969,8 @@ int sqlite3VdbeSorterInit( assert( pCsr->eCurType==CURTYPE_SORTER ); assert( sizeof(KeyInfo) + UMXV(pCsr->pKeyInfo->nKeyField)*sizeof(CollSeq*) < 0x7fffffff ); - szKeyInfo = SZ_KEYINFO(pCsr->pKeyInfo->nKeyField); + assert( pCsr->pKeyInfo->nKeyField<=pCsr->pKeyInfo->nAllField ); + szKeyInfo = SZ_KEYINFO(pCsr->pKeyInfo->nAllField); sz = SZ_VDBESORTER(nWorker+1); pSorter = (VdbeSorter*)sqlite3DbMallocZero(db, sz + szKeyInfo); @@ -983,7 +984,12 @@ int sqlite3VdbeSorterInit( pKeyInfo->db = 0; if( nField && nWorker==0 ){ pKeyInfo->nKeyField = nField; + assert( nField<=pCsr->pKeyInfo->nAllField ); } + /* It is OK that pKeyInfo reuses the aSortFlags field from pCsr->pKeyInfo, + ** since the pCsr->pKeyInfo->aSortFlags[] array is invariant and lives + ** longer that pSorter. */ + assert( pKeyInfo->aSortFlags==pCsr->pKeyInfo->aSortFlags ); sqlite3BtreeEnter(pBt); pSorter->pgsz = pgsz = sqlite3BtreeGetPageSize(pBt); sqlite3BtreeLeave(pBt); |