fix realloc zeroing

This commit is contained in:
Simon Lindholm 2022-06-29 15:18:29 +02:00
parent d565bd4231
commit e0017ac105
1 changed files with 6 additions and 4 deletions

View File

@ -3,6 +3,7 @@
#include <ctype.h> #include <ctype.h>
#include <filesystem> #include <filesystem>
#include <string> #include <string>
#include <malloc.h>
namespace kernel32 { namespace kernel32 {
uint32_t WIN_FUNC GetLastError() { uint32_t WIN_FUNC GetLastError() {
@ -42,7 +43,7 @@ namespace kernel32 {
lpProcessInformation lpProcessInformation
); );
printf("Cannot handle process creation, aborting\n"); printf("Cannot handle process creation, aborting\n");
abort(); exit(1);
return 0; return 0;
} }
@ -121,7 +122,7 @@ namespace kernel32 {
void *buffer = malloc(dwBytes); void *buffer = malloc(dwBytes);
if (buffer && (uFlags & 0x40)) { if (buffer && (uFlags & 0x40)) {
// GMEM_ZEROINT // GMEM_ZEROINT
memset(buffer, 0, dwBytes); memset(buffer, 0, malloc_usable_size(buffer));
} }
return buffer; return buffer;
} }
@ -137,10 +138,11 @@ namespace kernel32 {
} else { } else {
if (dwBytes == 0) if (dwBytes == 0)
dwBytes = 1; dwBytes = 1;
size_t oldSize = malloc_usable_size(hMem);
void *buffer = realloc(hMem, dwBytes); void *buffer = realloc(hMem, dwBytes);
if (buffer && (uFlags & 0x40)) { if (buffer && (uFlags & 0x40) && dwBytes > oldSize) {
// GMEM_ZEROINT // GMEM_ZEROINT
memset(buffer, 0, dwBytes); memset((char*)buffer + oldSize, 0, malloc_usable_size(buffer) - oldSize);
} }
return buffer; return buffer;
} }