Skip to content

Commit 0af29f2

Browse files
author
D. Richard Hipp
committed
Add new assert() statements, needlessly increase the resolution of some
integer calculations, and change a structure field name to be globally unique, all in an effort to forestall AI hallucinations about errors in the code.
1 parent 20c0174 commit 0af29f2

4 files changed

Lines changed: 31 additions & 28 deletions

File tree

src/os_unix.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5140,9 +5140,9 @@ static int unixShmMap(
51405140
nReqRegion = ((iRegion+nShmPerMap) / nShmPerMap) * nShmPerMap;
51415141

51425142
if( pShmNode->nRegion<nReqRegion ){
5143-
char **apNew; /* New apRegion[] array */
5144-
int nByte = nReqRegion*szRegion; /* Minimum required file size */
5145-
struct stat sStat; /* Used by fstat() */
5143+
char **apNew; /* New apRegion[] array */
5144+
i64 nByte = nReqRegion*(i64)szRegion; /* Minimum required file size */
5145+
struct stat sStat; /* Used by fstat() */
51465146

51475147
pShmNode->szRegion = szRegion;
51485148

src/os_win.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4581,9 +4581,9 @@ static int winShmMap(
45814581
assert( szRegion==pShmNode->szRegion || pShmNode->nRegion==0 );
45824582
if( pShmNode->nRegion<=iRegion ){
45834583
HANDLE hShared = pShmNode->hSharedShm;
4584-
struct ShmRegion *apNew; /* New aRegion[] array */
4585-
int nByte = (iRegion+1)*szRegion; /* Minimum required file size */
4586-
sqlite3_int64 sz; /* Current size of wal-index file */
4584+
struct ShmRegion *apNew; /* New aRegion[] array */
4585+
i64 nByte = (iRegion+1)*(i64)szRegion; /* Minimum required file size */
4586+
sqlite3_int64 sz; /* Current size of wal-index file */
45874587

45884588
pShmNode->szRegion = szRegion;
45894589

src/pager.c

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -694,7 +694,7 @@ struct Pager {
694694
#endif
695695
void (*xReiniter)(DbPage*); /* Call this routine when reloading pages */
696696
int (*xGet)(Pager*,Pgno,DbPage**,int); /* Routine to fetch a patch */
697-
char *pTmpSpace; /* Pager.pageSize bytes of space for tmp use */
697+
char *pPgrTmpSp; /* Pager.pageSize+8 bytes of space for tmp use */
698698
PCache *pPCache; /* Pointer to page cache object */
699699
#ifndef SQLITE_OMIT_WAL
700700
Wal *pWal; /* Write-ahead log used by "journal_mode=wal" */
@@ -1437,7 +1437,7 @@ static int zeroJournalHdr(Pager *pPager, int doTruncate){
14371437
*/
14381438
static int writeJournalHdr(Pager *pPager){
14391439
int rc = SQLITE_OK; /* Return code */
1440-
char *zHeader = pPager->pTmpSpace; /* Temporary space used to build header */
1440+
char *zHeader = pPager->pPgrTmpSp; /* Temporary space used to build header */
14411441
u32 nHeader = (u32)pPager->pageSize;/* Size of buffer pointed to by zHeader */
14421442
u32 nWrite; /* Bytes of header sector written */
14431443
int ii; /* Loop counter */
@@ -2304,7 +2304,7 @@ static int pager_playback_one_page(
23042304
assert( isMainJrnl || pDone ); /* pDone always used on sub-journals */
23052305
assert( isSavepnt || pDone==0 ); /* pDone never used on non-savepoint */
23062306

2307-
aData = pPager->pTmpSpace;
2307+
aData = pPager->pPgrTmpSp;
23082308
assert( aData ); /* Temp storage must have already been allocated */
23092309
assert( pagerUseWal(pPager)==0 || (!isMainJrnl && isSavepnt) );
23102310

@@ -2494,7 +2494,7 @@ static int pager_playback_one_page(
24942494
** This routine checks if it is possible to delete the super-journal file,
24952495
** and does so if it is.
24962496
**
2497-
** Argument zSuper may point to Pager.pTmpSpace. So that buffer is not
2497+
** Argument zSuper may point to Pager.pPgrTmpSp. So that buffer is not
24982498
** available for use within this function.
24992499
**
25002500
** When a super-journal file is created, it is populated with the names
@@ -2672,7 +2672,7 @@ static int pager_truncate(Pager *pPager, Pgno nPage){
26722672
if( currentSize>newSize ){
26732673
rc = sqlite3OsTruncate(pPager->fd, newSize);
26742674
}else if( (currentSize+szPage)<=newSize ){
2675-
char *pTmp = pPager->pTmpSpace;
2675+
char *pTmp = pPager->pPgrTmpSp;
26762676
memset(pTmp, 0, szPage);
26772677
testcase( (newSize-szPage) == currentSize );
26782678
testcase( (newSize-szPage) > currentSize );
@@ -2826,12 +2826,12 @@ static int pager_playback(Pager *pPager, int isHot){
28262826
** played back.
28272827
**
28282828
** TODO: Technically the following is an error because it assumes that
2829-
** buffer Pager.pTmpSpace is (mxPathname+1) bytes or larger. i.e. that
2829+
** buffer Pager.pPgrTmpSp is (mxPathname+1) bytes or larger. i.e. that
28302830
** (pPager->pageSize >= pPager->pVfs->mxPathname+1). Using os_unix.c,
28312831
** mxPathname is 512, which is the same as the minimum allowable value
28322832
** for pageSize.
28332833
*/
2834-
zSuper = pPager->pTmpSpace;
2834+
zSuper = pPager->pPgrTmpSp;
28352835
rc = readSuperJournal(pPager->jfd, zSuper, 1+(i64)pPager->pVfs->mxPathname);
28362836
if( rc==SQLITE_OK && zSuper[0] ){
28372837
rc = sqlite3OsAccess(pVfs, zSuper, SQLITE_ACCESS_EXISTS, &res);
@@ -2970,7 +2970,7 @@ static int pager_playback(Pager *pPager, int isHot){
29702970
** This is because it may end up being passed to sqlite3OsOpen(), in
29712971
** which case it requires 4 0x00 bytes in memory immediately before
29722972
** the filename. */
2973-
zSuper = &pPager->pTmpSpace[4];
2973+
zSuper = &pPager->pPgrTmpSp[4];
29742974
rc = readSuperJournal(pPager->jfd, zSuper, 1+(i64)pPager->pVfs->mxPathname);
29752975
testcase( rc!=SQLITE_OK );
29762976
}
@@ -2987,8 +2987,8 @@ static int pager_playback(Pager *pPager, int isHot){
29872987
/* If there was a super-journal and this routine will return success,
29882988
** see if it is possible to delete the super-journal.
29892989
*/
2990-
assert( zSuper==&pPager->pTmpSpace[4] );
2991-
memset(pPager->pTmpSpace, 0, 4);
2990+
assert( zSuper==&pPager->pPgrTmpSp[4] );
2991+
memset(pPager->pPgrTmpSp, 0, 4);
29922992
rc = pager_delsuper(pPager, zSuper);
29932993
testcase( rc!=SQLITE_OK );
29942994
}
@@ -3755,7 +3755,7 @@ void sqlite3PagerSetBusyHandler(
37553755
** then the pager object page size is set to *pPageSize.
37563756
**
37573757
** If the page size is changed, then this function uses sqlite3PagerMalloc()
3758-
** to obtain a new Pager.pTmpSpace buffer. If this allocation attempt
3758+
** to obtain a new Pager.pPgrTmpSp buffer. If this allocation attempt
37593759
** fails, SQLITE_NOMEM is returned and the page size remains unchanged.
37603760
** In all other cases, SQLITE_OK is returned.
37613761
**
@@ -3805,8 +3805,8 @@ int sqlite3PagerSetPagesize(Pager *pPager, u32 *pPageSize, int nReserve){
38053805
rc = sqlite3PcacheSetPageSize(pPager->pPCache, pageSize);
38063806
}
38073807
if( rc==SQLITE_OK ){
3808-
sqlite3PageFree(pPager->pTmpSpace);
3809-
pPager->pTmpSpace = pNew;
3808+
sqlite3PageFree(pPager->pPgrTmpSp);
3809+
pPager->pPgrTmpSp = pNew;
38103810
pPager->dbSize = (Pgno)((nByte+pageSize-1)/pageSize);
38113811
pPager->pageSize = pageSize;
38123812
pPager->lckPgno = (Pgno)(PENDING_BYTE/pageSize) + 1;
@@ -3834,7 +3834,7 @@ int sqlite3PagerSetPagesize(Pager *pPager, u32 *pPageSize, int nReserve){
38343834
** no rollbacks are happening.
38353835
*/
38363836
void *sqlite3PagerTempSpace(Pager *pPager){
3837-
return pPager->pTmpSpace;
3837+
return pPager->pPgrTmpSp;
38383838
}
38393839

38403840
/*
@@ -4174,7 +4174,7 @@ static int databaseIsUnmoved(Pager *pPager){
41744174
** to the caller.
41754175
*/
41764176
int sqlite3PagerClose(Pager *pPager, sqlite3 *db){
4177-
u8 *pTmp = (u8*)pPager->pTmpSpace;
4177+
u8 *pTmp = (u8*)pPager->pPgrTmpSp;
41784178
assert( db || pagerUseWal(pPager)==0 );
41794179
assert( assert_pager_state(pPager) );
41804180
disable_simulated_io_errors();
@@ -5011,7 +5011,7 @@ int sqlite3PagerOpen(
50115011
}
50125012

50135013
/* The following call to PagerSetPagesize() serves to set the value of
5014-
** Pager.pageSize and to allocate the Pager.pTmpSpace buffer.
5014+
** Pager.pageSize and to allocate the Pager.pPgrTmpSp buffer.
50155015
*/
50165016
if( rc==SQLITE_OK ){
50175017
assert( pPager->memDb==0 );
@@ -5031,7 +5031,7 @@ int sqlite3PagerOpen(
50315031
*/
50325032
if( rc!=SQLITE_OK ){
50335033
sqlite3OsClose(pPager->fd);
5034-
sqlite3PageFree(pPager->pTmpSpace);
5034+
sqlite3PageFree(pPager->pPgrTmpSp);
50355035
sqlite3_free(pPager);
50365036
return rc;
50375037
}
@@ -6619,7 +6619,7 @@ int sqlite3PagerCommitPhaseOne(
66196619
if( rc==SQLITE_OK ){
66206620
rc = pager_write_pagelist(pPager, pList);
66216621
if( rc==SQLITE_OK && pPager->dbSize>pPager->dbFileSize ){
6622-
char *pTmp = pPager->pTmpSpace;
6622+
char *pTmp = pPager->pPgrTmpSp;
66236623
int szPage = (int)pPager->pageSize;
66246624
memset(pTmp, 0, szPage);
66256625
rc = sqlite3OsWrite(pPager->fd, pTmp, szPage,
@@ -7273,8 +7273,8 @@ int sqlite3PagerMovepage(Pager *pPager, DbPage *pPg, Pgno pgno, int isCommit){
72737273
rc = sqlite3PagerGet(pPager, needSyncPgno, &pPgHdr, 0);
72747274
if( rc!=SQLITE_OK ){
72757275
if( needSyncPgno<=pPager->dbOrigSize ){
7276-
assert( pPager->pTmpSpace!=0 );
7277-
sqlite3BitvecClear(pPager->pInJournal, needSyncPgno, pPager->pTmpSpace);
7276+
assert( pPager->pPgrTmpSp!=0 );
7277+
sqlite3BitvecClear(pPager->pInJournal, needSyncPgno, pPager->pPgrTmpSp);
72787278
}
72797279
return rc;
72807280
}
@@ -7531,7 +7531,7 @@ int sqlite3PagerCheckpoint(
75317531
rc = sqlite3WalCheckpoint(pPager->pWal, db, eMode,
75327532
(eMode<=SQLITE_CHECKPOINT_PASSIVE ? 0 : pPager->xBusyHandler),
75337533
pPager->pBusyHandlerArg,
7534-
pPager->walSyncFlags, pPager->pageSize, (u8 *)pPager->pTmpSpace,
7534+
pPager->walSyncFlags, pPager->pageSize, (u8 *)pPager->pPgrTmpSp,
75357535
pnLog, pnCkpt
75367536
);
75377537
}
@@ -7696,7 +7696,7 @@ int sqlite3PagerCloseWal(Pager *pPager, sqlite3 *db){
76967696
rc = pagerExclusiveLock(pPager);
76977697
if( rc==SQLITE_OK ){
76987698
rc = sqlite3WalClose(pPager->pWal, db, pPager->walSyncFlags,
7699-
pPager->pageSize, (u8*)pPager->pTmpSpace);
7699+
pPager->pageSize, (u8*)pPager->pPgrTmpSp);
77007700
pPager->pWal = 0;
77017701
pagerFixMaplimit(pPager);
77027702
if( rc && !pPager->exclusiveMode ) pagerUnlockDb(pPager, SHARED_LOCK);

src/vdbe.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,9 +278,11 @@ static VdbeCursor *allocateCursor(
278278

279279
i64 nByte;
280280
VdbeCursor *pCx = 0;
281+
assert( nField<=SQLITE_MAX_COLUMN*2 );
281282
nByte = SZ_VDBECURSOR(nField);
282283
assert( ROUND8(nByte)==nByte );
283284
if( eCurType==CURTYPE_BTREE ) nByte += sqlite3BtreeCursorSize();
285+
assert( nByte>0 && nByte<=0x7fffffff );
284286

285287
assert( iCur>=0 && iCur<p->nCursor );
286288
if( p->apCsr[iCur] ){ /*OPTIMIZATION-IF-FALSE*/
@@ -304,6 +306,7 @@ static VdbeCursor *allocateCursor(
304306
pMem->szMalloc = 0;
305307
return 0;
306308
}
309+
assert( nByte>0 && nByte<=0x7fffffff );
307310
pMem->szMalloc = (int)nByte;
308311
}
309312

0 commit comments

Comments
 (0)