1
0
mirror of https://github.com/sqlite/sqlite.git synced 2025-11-15 11:41:13 +03:00

Avoid pointer aliasing in the allocSpace() routine in vdbeaux.c.

FossilOrigin-Name: d6ae27512229d95502c584b17bb2cbdba401f80a
This commit is contained in:
drh
2009-12-08 15:35:22 +00:00
parent 1e15c031ca
commit 4800b2ee95
3 changed files with 35 additions and 32 deletions

View File

@@ -1254,11 +1254,15 @@ void sqlite3VdbeIOTraceSql(Vdbe *p){
#endif /* !SQLITE_OMIT_TRACE && SQLITE_ENABLE_IOTRACE */
/*
** Allocate space from a fixed size buffer. Make *pp point to the
** allocated space. (Note: pp is a char* rather than a void** to
** work around the pointer aliasing rules of C.) *pp should initially
** be zero. If *pp is not zero, that means that the space has already
** been allocated and this routine is a noop.
** Allocate space from a fixed size buffer and return a pointer to
** that space. If insufficient space is available, return NULL.
**
** The pBuf parameter is the initial value of a pointer which will
** receive the new memory. pBuf is normally NULL. If pBuf is not
** NULL, it means that memory space has already been allocated and that
** this routine should not allocate any new memory. When pBuf is not
** NULL simply return pBuf. Only allocate new memory space when pBuf
** is NULL.
**
** nByte is the number of bytes of space needed.
**
@@ -1269,23 +1273,23 @@ void sqlite3VdbeIOTraceSql(Vdbe *p){
** to allocate. If there is insufficient space in *ppFrom to satisfy the
** request, then increment *pnByte by the amount of the request.
*/
static void allocSpace(
char *pp, /* IN/OUT: Set *pp to point to allocated buffer */
static void *allocSpace(
void *pBuf, /* Where return pointer will be stored */
int nByte, /* Number of bytes to allocate */
u8 **ppFrom, /* IN/OUT: Allocate from *ppFrom */
u8 *pEnd, /* Pointer to 1 byte past the end of *ppFrom buffer */
int *pnByte /* If allocation cannot be made, increment *pnByte */
){
assert( EIGHT_BYTE_ALIGNMENT(*ppFrom) );
if( (*(void**)pp)==0 ){
nByte = ROUND8(nByte);
if( &(*ppFrom)[nByte] <= pEnd ){
*(void**)pp = (void *)*ppFrom;
*ppFrom += nByte;
}else{
*pnByte += nByte;
}
if( pBuf ) return pBuf;
nByte = ROUND8(nByte);
if( &(*ppFrom)[nByte] <= pEnd ){
pBuf = (void*)*ppFrom;
*ppFrom += nByte;
}else{
*pnByte += nByte;
}
return pBuf;
}
/*
@@ -1358,13 +1362,12 @@ void sqlite3VdbeMakeReady(
do {
nByte = 0;
allocSpace((char*)&p->aMem, nMem*sizeof(Mem), &zCsr, zEnd, &nByte);
allocSpace((char*)&p->aVar, nVar*sizeof(Mem), &zCsr, zEnd, &nByte);
allocSpace((char*)&p->apArg, nArg*sizeof(Mem*), &zCsr, zEnd, &nByte);
allocSpace((char*)&p->azVar, nVar*sizeof(char*), &zCsr, zEnd, &nByte);
allocSpace((char*)&p->apCsr,
nCursor*sizeof(VdbeCursor*), &zCsr, zEnd, &nByte
);
p->aMem = allocSpace(p->aMem, nMem*sizeof(Mem), &zCsr, zEnd, &nByte);
p->aVar = allocSpace(p->aVar, nVar*sizeof(Mem), &zCsr, zEnd, &nByte);
p->apArg = allocSpace(p->apArg, nArg*sizeof(Mem*), &zCsr, zEnd, &nByte);
p->azVar = allocSpace(p->azVar, nVar*sizeof(char*), &zCsr, zEnd, &nByte);
p->apCsr = allocSpace(p->apCsr, nCursor*sizeof(VdbeCursor*),
&zCsr, zEnd, &nByte);
if( nByte ){
p->pFree = sqlite3DbMallocZero(db, nByte);
}