LibC: Fix memory leak in realpath
This commit is contained in:
parent
2a8baf9582
commit
ed857bc06e
Notes:
sideshowbarker
2024-07-18 23:10:29 +09:00
Author: https://github.com/BenWiederhake Commit: https://github.com/SerenityOS/serenity/commit/ed857bc06e6 Pull-request: https://github.com/SerenityOS/serenity/pull/4965 Reviewed-by: https://github.com/bugaevc Reviewed-by: https://github.com/emanuele6
1 changed files with 6 additions and 1 deletions
|
@ -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;
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue