From ed857bc06e6db442cb3f309e3972649233441c13 Mon Sep 17 00:00:00 2001 From: Ben Wiederhake Date: Fri, 15 Jan 2021 22:12:56 +0100 Subject: [PATCH] LibC: Fix memory leak in realpath --- Userland/Libraries/LibC/stdlib.cpp | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/Userland/Libraries/LibC/stdlib.cpp b/Userland/Libraries/LibC/stdlib.cpp index 07f7199bcf..095ce612de 100644 --- a/Userland/Libraries/LibC/stdlib.cpp +++ b/Userland/Libraries/LibC/stdlib.cpp @@ -1049,11 +1049,16 @@ char* realpath(const char* pathname, char* buffer) return nullptr; } size_t size = PATH_MAX; - if (buffer == nullptr) + bool self_allocated = false; + if (buffer == nullptr) { buffer = (char*)malloc(size); + self_allocated = true; + } Syscall::SC_realpath_params params { { pathname, strlen(pathname) }, { buffer, size } }; int rc = syscall(SC_realpath, ¶ms); if (rc < 0) { + if (self_allocated) + free(buffer); errno = -rc; return nullptr; }