https://git.reactos.org/?p=reactos.git;a=commitdiff;h=e7440eff8fb2092bea08d…
commit e7440eff8fb2092bea08def0db7f6d196167e5a4
Author:     Serge Gautherie <32623169+SergeGautherie(a)users.noreply.github.com>
AuthorDate: Sat Jun 6 17:58:34 2020 +0200
Commit:     GitHub <noreply(a)github.com>
CommitDate: Sat Jun 6 17:58:34 2020 +0200
    [APPHELP_APITEST] Properly handle a couple 'malloc()/free()' (#2898)
    Detected by Cppcheck: memleak, redundantAssignment.
    Addendum to 78280ad2 (r71226).
---
 modules/rostests/apitests/apphelp/data.c     | 12 ++++++++++--
 modules/rostests/apitests/apphelp/layerapi.c | 16 +++++++++++++---
 2 files changed, 23 insertions(+), 5 deletions(-)
diff --git a/modules/rostests/apitests/apphelp/data.c
b/modules/rostests/apitests/apphelp/data.c
index 95dd9216194..58278271848 100644
--- a/modules/rostests/apitests/apphelp/data.c
+++ b/modules/rostests/apitests/apphelp/data.c
@@ -469,15 +469,23 @@ void test_create_exe_imp(const WCHAR* name, int skip_rsrc_exports)
     HANDLE file;
     char *buf, *cur;
     DWORD size = 0x800;
+
     buf = malloc(size);
+    winetest_ok(buf != NULL, "malloc failed\n");
+    if (buf == NULL)
+    {
+        return;
+    }
     file = CreateFileW(name, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS,
FILE_ATTRIBUTE_NORMAL, NULL);
     winetest_ok(file != INVALID_HANDLE_VALUE, "can't create file\n");
-    if(file == INVALID_HANDLE_VALUE)
+    if (file == INVALID_HANDLE_VALUE)
+    {
+        free(buf);
         return;
+    }
     memset(buf, 0, size);
-    cur = buf;
     cur = memcpy(buf, &dos_header, sizeof(dos_header));
     cur += dos_header.e_lfanew;
diff --git a/modules/rostests/apitests/apphelp/layerapi.c
b/modules/rostests/apitests/apphelp/layerapi.c
index 69306f220a9..72ccf9555c6 100644
--- a/modules/rostests/apitests/apphelp/layerapi.c
+++ b/modules/rostests/apitests/apphelp/layerapi.c
@@ -569,16 +569,26 @@ static BOOL create_file(LPCSTR dir, LPCSTR name, int filler, DWORD
size)
 {
     char target[MAX_PATH], *tmp;
     HANDLE file;
-    PathCombineA(target, dir, name);
     tmp = malloc(size);
-    memset(tmp, filler, size);
+    if (tmp == NULL)
+    {
+        SetLastError(ERROR_OUTOFMEMORY);
+        return FALSE;
+    }
+
+    PathCombineA(target, dir, name);
     file = CreateFileA(target, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS,
FILE_ATTRIBUTE_NORMAL, NULL);
-    if(file == INVALID_HANDLE_VALUE)
+    if (file == INVALID_HANDLE_VALUE)
+    {
+        free(tmp);
         return FALSE;
+    }
+    memset(tmp, filler, size);
     WriteFile(file, tmp, size, &size, NULL);
+
     CloseHandle(file);
     free(tmp);
     return TRUE;