LibC: Fix memory leak in realpath

This commit is contained in:
Ben Wiederhake 2021-01-15 22:12:56 +01:00 committed by Andreas Kling
parent 2a8baf9582
commit ed857bc06e
Notes: sideshowbarker 2024-07-18 23:10:29 +09:00

View file

@ -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, &params);
if (rc < 0) {
if (self_allocated)
free(buffer);
errno = -rc;
return nullptr;
}