From 72ac97ef6ab7d2cb806ee8dcee4afbe9e06832cd Mon Sep 17 00:00:00 2001 From: Ben Wiederhake Date: Sat, 16 Jan 2021 13:32:11 +0100 Subject: [PATCH] LibC: Fix memory leak in getcwd --- Userland/Libraries/LibC/unistd.cpp | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/Userland/Libraries/LibC/unistd.cpp b/Userland/Libraries/LibC/unistd.cpp index b35f011289..718ff6961c 100644 --- a/Userland/Libraries/LibC/unistd.cpp +++ b/Userland/Libraries/LibC/unistd.cpp @@ -322,11 +322,16 @@ int fchdir(int fd) char* getcwd(char* buffer, size_t size) { + bool self_allocated = false; if (!buffer) { size = size ? size : PATH_MAX; buffer = (char*)malloc(size); + self_allocated = true; } int rc = syscall(SC_getcwd, buffer, size); + if (rc < 0 && self_allocated) { + free(buffer); + } __RETURN_WITH_ERRNO(rc, buffer, nullptr); }