LibC: Fix memory leak in getcwd

This commit is contained in:
Ben Wiederhake 2021-01-16 13:32:11 +01:00 committed by Andreas Kling
parent ed857bc06e
commit 72ac97ef6a
Notes: sideshowbarker 2024-07-18 23:10:25 +09:00

View file

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