From add6726c90e112260c6b02fc8fcc8f3187780be5 Mon Sep 17 00:00:00 2001 From: "Dimitrie O. Paun" Date: Thu, 4 Dec 2003 01:42:34 +0000 Subject: [PATCH] Fix NetApiBufferReallocate and add a few tests for the border cases (thanks to Juan Lang for clarifications). --- dlls/netapi32/apibuf.c | 16 +++++++++++----- dlls/netapi32/tests/apibuf.c | 9 ++++++++- 2 files changed, 19 insertions(+), 6 deletions(-) diff --git a/dlls/netapi32/apibuf.c b/dlls/netapi32/apibuf.c index 86c4315f3a1..010409503eb 100644 --- a/dlls/netapi32/apibuf.c +++ b/dlls/netapi32/apibuf.c @@ -60,11 +60,17 @@ NET_API_STATUS WINAPI NetApiBufferReallocate(LPVOID OldBuffer, DWORD NewByteCoun LPVOID* NewBuffer) { TRACE("(%p, %ld, %p)\n", OldBuffer, NewByteCount, NewBuffer); - *NewBuffer = HeapReAlloc(GetProcessHeap(), 0, OldBuffer, NewByteCount); - if (*NewBuffer) - return NERR_Success; - else - return GetLastError(); + if (NewByteCount) + { + *NewBuffer = HeapReAlloc(GetProcessHeap(), 0, OldBuffer, NewByteCount); + return *NewBuffer ? NERR_Success : GetLastError(); + } + else + { + if (!HeapFree(GetProcessHeap(), 0, OldBuffer)) return GetLastError(); + *NewBuffer = 0; + return NERR_Success; + } } /************************************************************ diff --git a/dlls/netapi32/tests/apibuf.c b/dlls/netapi32/tests/apibuf.c index 6768a2b4fd3..088dc3a9df8 100644 --- a/dlls/netapi32/tests/apibuf.c +++ b/dlls/netapi32/tests/apibuf.c @@ -63,7 +63,14 @@ void run_apibuf_tests(void) ok(dwSize >= 0, "The size"); ok(pNetApiBufferSize(NULL, &dwSize) == ERROR_INVALID_PARAMETER, "Error for NULL pointer"); - /* 0-length buffer */ + /* border reallocate cases */ + ok(pNetApiBufferReallocate(0, 1500, (LPVOID *) &p) != NERR_Success, "(Re)allocated"); + ok(p == NULL, "Some memory got allocated"); + ok(pNetApiBufferAllocate(1024, (LPVOID *)&p) == NERR_Success, "Memory not reserved"); + ok(pNetApiBufferReallocate(p, 0, (LPVOID *) &p) == NERR_Success, "Not freed"); + ok(p == NULL, "Pointer not cleared"); + + /* 0-length buffer */ ok(pNetApiBufferAllocate(0, (LPVOID *)&p) == NERR_Success, "Reserved memory"); ok(pNetApiBufferSize(p, &dwSize) == NERR_Success, "Got size");