From de12a970416aafa1fe4a2966f74b4958e6dbab4c Mon Sep 17 00:00:00 2001 From: Oleg Prokhorov Date: Tue, 14 Oct 2003 05:24:20 +0000 Subject: [PATCH] Another portion of HeapReAlloc fixes. --- controls/listbox.c | 22 +++++++++++++++++----- dlls/dsound/buffer.c | 16 +++++++++++++--- dlls/dsound/capture.c | 29 ++++++++++++++++++++++------- dlls/dsound/dsound_main.c | 7 ++++++- dlls/dsound/primary.c | 7 ++++++- dlls/kernel/editline.c | 10 ++++++++-- dlls/kernel/global16.c | 12 +++++++++--- dlls/kernel/resource16.c | 13 +++++++++---- dlls/kernel/snoop16.c | 7 ++++++- dlls/ole32/oleproxy.c | 7 ++++++- dlls/ole32/rpc.c | 7 ++++++- dlls/rpcrt4/rpc_server.c | 6 +++++- dlls/setupapi/dirid.c | 10 +++++++++- dlls/setupapi/setupx_main.c | 5 ++++- dlls/user/message.c | 8 +++++++- 15 files changed, 133 insertions(+), 33 deletions(-) diff --git a/controls/listbox.c b/controls/listbox.c index b6ddb38a08f..bd14a895e9a 100644 --- a/controls/listbox.c +++ b/controls/listbox.c @@ -714,10 +714,17 @@ static LRESULT LISTBOX_InitStorage( HWND hwnd, LB_DESCR *descr, INT nb_items ) nb_items += LB_ARRAY_GRANULARITY - 1; nb_items -= (nb_items % LB_ARRAY_GRANULARITY); - if (descr->items) + if (descr->items) { nb_items += HeapSize( GetProcessHeap(), 0, descr->items ) / sizeof(*item); - if (!(item = HeapReAlloc( GetProcessHeap(), 0, descr->items, - nb_items * sizeof(LB_ITEMDATA) ))) + item = HeapReAlloc( GetProcessHeap(), 0, descr->items, + nb_items * sizeof(LB_ITEMDATA)); + } + else { + item = HeapAlloc( GetProcessHeap(), 0, + nb_items * sizeof(LB_ITEMDATA)); + } + + if (!item) { SEND_NOTIFICATION( hwnd, descr, LBN_ERRSPACE ); return LB_ERRSPACE; @@ -1477,8 +1484,13 @@ static LRESULT LISTBOX_InsertItem( HWND hwnd, LB_DESCR *descr, INT index, { /* We need to grow the array */ max_items += LB_ARRAY_GRANULARITY; - if (!(item = HeapReAlloc( GetProcessHeap(), 0, descr->items, - max_items * sizeof(LB_ITEMDATA) ))) + if (descr->items) + item = HeapReAlloc( GetProcessHeap(), 0, descr->items, + max_items * sizeof(LB_ITEMDATA) ); + else + item = HeapAlloc( GetProcessHeap(), 0, + max_items * sizeof(LB_ITEMDATA) ); + if (!item) { SEND_NOTIFICATION( hwnd, descr, LBN_ERRSPACE ); return LB_ERRSPACE; diff --git a/dlls/dsound/buffer.c b/dlls/dsound/buffer.c index f248228ffd4..b68054b5076 100644 --- a/dlls/dsound/buffer.c +++ b/dlls/dsound/buffer.c @@ -121,8 +121,13 @@ static HRESULT WINAPI IDirectSoundNotifyImpl_SetNotificationPositions( } else { /* Make an internal copy of the caller-supplied array. * Replace the existing copy if one is already present. */ - This->dsb->notifies = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, - This->dsb->notifies, howmuch * sizeof(DSBPOSITIONNOTIFY)); + if (This->dsb->notifies) + This->dsb->notifies = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, + This->dsb->notifies, howmuch * sizeof(DSBPOSITIONNOTIFY)); + else + This->dsb->notifies = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, + howmuch * sizeof(DSBPOSITIONNOTIFY)); + if (This->dsb->notifies == NULL) { WARN("out of memory\n"); return DSERR_OUTOFMEMORY; @@ -1163,7 +1168,12 @@ HRESULT WINAPI IDirectSoundBufferImpl_Create( /* register buffer */ RtlAcquireResourceExclusive(&(ds->lock), TRUE); if (!(dsbd->dwFlags & DSBCAPS_PRIMARYBUFFER)) { - IDirectSoundBufferImpl **newbuffers = (IDirectSoundBufferImpl**)HeapReAlloc(GetProcessHeap(),0,ds->buffers,sizeof(IDirectSoundBufferImpl*)*(ds->nrofbuffers+1)); + IDirectSoundBufferImpl **newbuffers; + if (ds->buffers) + newbuffers = (IDirectSoundBufferImpl**)HeapReAlloc(GetProcessHeap(),0,ds->buffers,sizeof(IDirectSoundBufferImpl*)*(ds->nrofbuffers+1)); + else + newbuffers = (IDirectSoundBufferImpl**)HeapAlloc(GetProcessHeap(),0,sizeof(IDirectSoundBufferImpl*)*(ds->nrofbuffers+1)); + if (newbuffers) { ds->buffers = newbuffers; ds->buffers[ds->nrofbuffers] = dsb; diff --git a/dlls/dsound/capture.c b/dlls/dsound/capture.c index 3948882a912..a7a46dab6e2 100644 --- a/dlls/dsound/capture.c +++ b/dlls/dsound/capture.c @@ -757,8 +757,10 @@ DSOUND_CreateDirectSoundCaptureBuffer( buflen = lpcDSCBufferDesc->dwBufferBytes; TRACE("desired buflen=%ld, old buffer=%p\n", buflen, ipDSC->buffer); - newbuf = (LPBYTE)HeapReAlloc(GetProcessHeap(),0,ipDSC->buffer,buflen); - + if (ipDSC->buffer) + newbuf = (LPBYTE)HeapReAlloc(GetProcessHeap(),0,ipDSC->buffer,buflen); + else + newbuf = (LPBYTE)HeapAlloc(GetProcessHeap(),0,buflen); if (newbuf == NULL) { WARN("failed to allocate capture buffer\n"); err = DSERR_OUTOFMEMORY; @@ -850,8 +852,13 @@ static HRESULT WINAPI IDirectSoundCaptureNotifyImpl_SetNotificationPositions( } else { /* Make an internal copy of the caller-supplied array. * Replace the existing copy if one is already present. */ - This->dscb->notifies = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, - This->dscb->notifies, howmuch * sizeof(DSBPOSITIONNOTIFY)); + if (This->dscb->notifies) + This->dscb->notifies = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, + This->dscb->notifies, howmuch * sizeof(DSBPOSITIONNOTIFY)); + else + This->dscb->notifies = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, + howmuch * sizeof(DSBPOSITIONNOTIFY)); + if (This->dscb->notifies == NULL) { WARN("out of memory\n"); return DSERR_OUTOFMEMORY; @@ -1336,8 +1343,12 @@ IDirectSoundCaptureBufferImpl_Start( TRACE("nrofnotifies=%d\n", This->nrofnotifies); /* prepare headers */ - ipDSC->pwave = HeapReAlloc(GetProcessHeap(),0,ipDSC->pwave, - ipDSC->nrofpwaves*sizeof(WAVEHDR)); + if (ipDSC->pwave) + ipDSC->pwave = HeapReAlloc(GetProcessHeap(),0,ipDSC->pwave, + ipDSC->nrofpwaves*sizeof(WAVEHDR)); + else + ipDSC->pwave = HeapAlloc(GetProcessHeap(),0, + ipDSC->nrofpwaves*sizeof(WAVEHDR)); for (c = 0; c < ipDSC->nrofpwaves; c++) { if (c == 0) { @@ -1379,7 +1390,11 @@ IDirectSoundCaptureBufferImpl_Start( TRACE("no notifiers specified\n"); /* no notifiers specified so just create a single default header */ ipDSC->nrofpwaves = 1; - ipDSC->pwave = HeapReAlloc(GetProcessHeap(),0,ipDSC->pwave,sizeof(WAVEHDR)); + if (ipDSC->pwave) + ipDSC->pwave = HeapReAlloc(GetProcessHeap(),0,ipDSC->pwave,sizeof(WAVEHDR)); + else + ipDSC->pwave = HeapAlloc(GetProcessHeap(),0,sizeof(WAVEHDR)); + ipDSC->pwave[0].lpData = ipDSC->buffer; ipDSC->pwave[0].dwBufferLength = ipDSC->buflen; ipDSC->pwave[0].dwUser = (DWORD)ipDSC; diff --git a/dlls/dsound/dsound_main.c b/dlls/dsound/dsound_main.c index 1470a735636..67caa89be89 100644 --- a/dlls/dsound/dsound_main.c +++ b/dlls/dsound/dsound_main.c @@ -623,7 +623,12 @@ static HRESULT WINAPI IDirectSoundImpl_DuplicateSoundBuffer( /* register buffer */ RtlAcquireResourceExclusive(&(This->lock), TRUE); { - IDirectSoundBufferImpl **newbuffers = (IDirectSoundBufferImpl**)HeapReAlloc(GetProcessHeap(),0,This->buffers,sizeof(IDirectSoundBufferImpl**)*(This->nrofbuffers+1)); + IDirectSoundBufferImpl **newbuffers; + if (This->buffers) + newbuffers = (IDirectSoundBufferImpl**)HeapReAlloc(GetProcessHeap(),0,This->buffers,sizeof(IDirectSoundBufferImpl**)*(This->nrofbuffers+1)); + else + newbuffers = (IDirectSoundBufferImpl**)HeapAlloc(GetProcessHeap(),0,sizeof(IDirectSoundBufferImpl**)*(This->nrofbuffers+1)); + if (newbuffers) { This->buffers = newbuffers; This->buffers[This->nrofbuffers] = dsb; diff --git a/dlls/dsound/primary.c b/dlls/dsound/primary.c index 7589f8c06ee..f6a3368fac6 100644 --- a/dlls/dsound/primary.c +++ b/dlls/dsound/primary.c @@ -90,7 +90,12 @@ static HRESULT DSOUND_PrimaryOpen(IDirectSoundImpl *This) buflen = ((This->wfx.nAvgBytesPerSec / 100) & ~3) * DS_HEL_FRAGS; TRACE("desired buflen=%ld, old buffer=%p\n", buflen, This->buffer); /* reallocate emulated primary buffer */ - newbuf = (LPBYTE)HeapReAlloc(GetProcessHeap(),0,This->buffer,buflen); + + if (This->buffer) + newbuf = (LPBYTE)HeapReAlloc(GetProcessHeap(),0,This->buffer,buflen); + else + newbuf = (LPBYTE)HeapAlloc(GetProcessHeap(),0,buflen); + if (newbuf == NULL) { ERR("failed to allocate primary buffer\n"); merr = DSERR_OUTOFMEMORY; diff --git a/dlls/kernel/editline.c b/dlls/kernel/editline.c index 0dada92e06e..88ee70bf1d6 100644 --- a/dlls/kernel/editline.c +++ b/dlls/kernel/editline.c @@ -150,7 +150,12 @@ static BOOL WCEL_Grow(WCEL_Context* ctx, size_t len) /* round up size to 32 byte-WCHAR boundary */ newsize = (ctx->len + len + 1 + 31) & ~31; - newline = HeapReAlloc(GetProcessHeap(), 0, ctx->line, sizeof(WCHAR) * newsize); + + if (ctx->line) + newline = HeapReAlloc(GetProcessHeap(), 0, ctx->line, sizeof(WCHAR) * newsize); + else + newline = HeapAlloc(GetProcessHeap(), 0, sizeof(WCHAR) * newsize); + if (!newline) return FALSE; ctx->line = newline; ctx->alloc = newsize; @@ -237,7 +242,8 @@ static void WCEL_SaveYank(WCEL_Context* ctx, int beg, int end) if (len <= 0) return; WCEL_FreeYank(ctx); - ctx->yanked = HeapReAlloc(GetProcessHeap(), 0, ctx->yanked, (len + 1) * sizeof(WCHAR)); + /* After WCEL_FreeYank ctx->yanked is empty */ + ctx->yanked = HeapAlloc(GetProcessHeap(), 0, (len + 1) * sizeof(WCHAR)); if (!ctx->yanked) return; memcpy(ctx->yanked, &ctx->line[beg], len * sizeof(WCHAR)); ctx->yanked[len] = 0; diff --git a/dlls/kernel/global16.c b/dlls/kernel/global16.c index a21c7c2f061..76641e5ff7f 100644 --- a/dlls/kernel/global16.c +++ b/dlls/kernel/global16.c @@ -352,10 +352,16 @@ HGLOBAL16 WINAPI GlobalReAlloc16( * given out by GetVDMPointer32W16), * only try to realloc in place */ - newptr = HeapReAlloc( GetProcessHeap(), - (pArena->pageLockCount > 0) ? - HEAP_REALLOC_IN_PLACE_ONLY : 0, + + if (ptr) + newptr = HeapReAlloc( GetProcessHeap(), + (pArena->pageLockCount > 0) ? HEAP_REALLOC_IN_PLACE_ONLY : 0, ptr, size ); + else + newptr = HeapAlloc( GetProcessHeap(), + (pArena->pageLockCount > 0) ? HEAP_REALLOC_IN_PLACE_ONLY : 0, + size ); + } if (!newptr) diff --git a/dlls/kernel/resource16.c b/dlls/kernel/resource16.c index a00a89aa87b..85455881ef9 100644 --- a/dlls/kernel/resource16.c +++ b/dlls/kernel/resource16.c @@ -95,10 +95,15 @@ static HRSRC16 MapHRsrc32To16( NE_MODULE *pModule, HRSRC hRsrc32, WORD type ) /* If no space left, grow table */ if ( map->nUsed == map->nAlloc ) { - if ( !(newElem = (HRSRC_ELEM *)HeapReAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, - map->elem, - (map->nAlloc + HRSRC_MAP_BLOCKSIZE) - * sizeof(HRSRC_ELEM) ) )) + + if (map->elem) + newElem = (HRSRC_ELEM *)HeapReAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, + map->elem, (map->nAlloc + HRSRC_MAP_BLOCKSIZE) * sizeof(HRSRC_ELEM) ); + else + newElem = (HRSRC_ELEM *)HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, + (map->nAlloc + HRSRC_MAP_BLOCKSIZE) * sizeof(HRSRC_ELEM) ); + + if ( !newElem ) { ERR("Cannot grow HRSRC map\n" ); return 0; diff --git a/dlls/kernel/snoop16.c b/dlls/kernel/snoop16.c index ab276405483..a15570363af 100644 --- a/dlls/kernel/snoop16.c +++ b/dlls/kernel/snoop16.c @@ -145,7 +145,12 @@ SNOOP16_RegisterDLL(NE_MODULE *pModule,LPCSTR name) { } dll = &((*dll)->next); } - *dll = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, *dll, sizeof(SNOOP16_DLL)+strlen(name)); + + if (*dll) + *dll = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, *dll, sizeof(SNOOP16_DLL)+strlen(name)); + else + *dll = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(SNOOP16_DLL)+strlen(name)); + (*dll)->next = NULL; (*dll)->hmod = pModule->self; if ((s=strrchr(name,'\\'))) diff --git a/dlls/ole32/oleproxy.c b/dlls/ole32/oleproxy.c index bcce77cc709..0739592d9a5 100644 --- a/dlls/ole32/oleproxy.c +++ b/dlls/ole32/oleproxy.c @@ -185,7 +185,12 @@ CFStub_Invoke( } msg->cbBuffer = ststg.cbSize.s.LowPart; - msg->Buffer = HeapReAlloc(GetProcessHeap(),0,msg->Buffer,ststg.cbSize.s.LowPart); + + if (msg->Buffer) + msg->Buffer = HeapReAlloc(GetProcessHeap(),0,msg->Buffer,ststg.cbSize.s.LowPart); + else + msg->Buffer = HeapAlloc(GetProcessHeap(),0,ststg.cbSize.s.LowPart); + seekto.s.LowPart = 0;seekto.s.HighPart = 0; hres = IStream_Seek(pStm,seekto,SEEK_SET,&newpos); if (hres) { diff --git a/dlls/ole32/rpc.c b/dlls/ole32/rpc.c index fed58aabcf9..43e823a412e 100644 --- a/dlls/ole32/rpc.c +++ b/dlls/ole32/rpc.c @@ -606,7 +606,12 @@ _read_one(wine_pipe *xpipe) { continue; if (xreq->reqh.reqid == resph.reqid) { memcpy(&(xreq->resph),&resph,sizeof(resph)); - xreq->Buffer = HeapReAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,xreq->Buffer,xreq->resph.cbBuffer); + + if (xreq->Buffer) + xreq->Buffer = HeapReAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,xreq->Buffer,xreq->resph.cbBuffer); + else + xreq->Buffer = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,xreq->resph.cbBuffer); + hres = _xread(xhPipe,xreq->Buffer,xreq->resph.cbBuffer); if (hres) goto end; xreq->state = REQSTATE_RESP_GOT; diff --git a/dlls/rpcrt4/rpc_server.c b/dlls/rpcrt4/rpc_server.c index e9ed5931815..e9fe90225bc 100644 --- a/dlls/rpcrt4/rpc_server.c +++ b/dlls/rpcrt4/rpc_server.c @@ -390,7 +390,11 @@ static DWORD CALLBACK RPCRT4_server_thread(LPVOID the_arg) cps = cps->Next; } /* make array of connections */ - objs = HeapReAlloc(GetProcessHeap(), 0, objs, count*sizeof(HANDLE)); + if (objs) + objs = HeapReAlloc(GetProcessHeap(), 0, objs, count*sizeof(HANDLE)); + else + objs = HeapAlloc(GetProcessHeap(), 0, count*sizeof(HANDLE)); + objs[0] = m_event; count = 1; cps = protseqs; diff --git a/dlls/setupapi/dirid.c b/dlls/setupapi/dirid.c index 7728f898b07..63f2e86377b 100644 --- a/dlls/setupapi/dirid.c +++ b/dlls/setupapi/dirid.c @@ -176,8 +176,16 @@ static BOOL store_user_dirid( HINF hinf, int id, WCHAR *str ) if (nb_user_dirids >= alloc_user_dirids) { int new_size = max( 32, alloc_user_dirids * 2 ); - struct user_dirid *new = HeapReAlloc( GetProcessHeap(), 0, user_dirids, + + struct user_dirid *new; + + if (user_dirids) + new = HeapReAlloc( GetProcessHeap(), 0, user_dirids, new_size * sizeof(*new) ); + else + new = HeapAlloc( GetProcessHeap(), 0, + new_size * sizeof(*new) ); + if (!new) return FALSE; user_dirids = new; alloc_user_dirids = new_size; diff --git a/dlls/setupapi/setupx_main.c b/dlls/setupapi/setupx_main.c index fdbf6669afa..4f5b9525c3b 100644 --- a/dlls/setupapi/setupx_main.c +++ b/dlls/setupapi/setupx_main.c @@ -131,7 +131,10 @@ static LPSTR *SETUPX_GetSubStrings(LPSTR start, char delimiter) /* alloc entry for new substring in steps of 32 units and copy over */ if (count % 32 == 0) { /* 1 for count field + current count + 32 */ - res = HeapReAlloc(GetProcessHeap(), 0, res, (1+count+32)*sizeof(LPSTR)); + if (res) + res = HeapReAlloc(GetProcessHeap(), 0, res, (1+count+32)*sizeof(LPSTR)); + else + res = HeapAlloc(GetProcessHeap(), 0, (1+count+32)*sizeof(LPSTR)); } *(res+1+count) = HeapAlloc(GetProcessHeap(), 0, len+1); strncpy(*(res+1+count), p, len); diff --git a/dlls/user/message.c b/dlls/user/message.c index 01abe4ebcf5..31ee8b9efc2 100644 --- a/dlls/user/message.c +++ b/dlls/user/message.c @@ -1160,8 +1160,14 @@ static BOOL dde_add_pair(HGLOBAL chm, HGLOBAL shm) /* now remember the pair of hMem on both sides */ if (dde_num_used == dde_num_alloc) { - struct DDE_pair* tmp = HeapReAlloc( GetProcessHeap(), 0, dde_pairs, + struct DDE_pair* tmp; + if (dde_pairs) + tmp = HeapReAlloc( GetProcessHeap(), 0, dde_pairs, (dde_num_alloc + GROWBY) * sizeof(struct DDE_pair)); + else + tmp = HeapAlloc( GetProcessHeap(), 0, + (dde_num_alloc + GROWBY) * sizeof(struct DDE_pair)); + if (!tmp) { LeaveCriticalSection(&dde_crst);