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; }