summaryrefslogtreecommitdiff
path: root/Userland
diff options
context:
space:
mode:
authorBen Wiederhake <BenWiederhake.GitHub@gmx.de>2021-01-16 13:32:11 +0100
committerAndreas Kling <kling@serenityos.org>2021-01-16 22:40:53 +0100
commit72ac97ef6ab7d2cb806ee8dcee4afbe9e06832cd (patch)
tree022eb8103df7d20c538129bdd1a5927f24b316cb /Userland
parented857bc06e6db442cb3f309e3972649233441c13 (diff)
downloadserenity-72ac97ef6ab7d2cb806ee8dcee4afbe9e06832cd.zip
LibC: Fix memory leak in getcwd
Diffstat (limited to 'Userland')
-rw-r--r--Userland/Libraries/LibC/unistd.cpp5
1 files changed, 5 insertions, 0 deletions
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);
}