1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 16:17:45 +00:00

Kernel+LibC: Make sys$getcwd truncate the result silently

This gives us the superpower of knowing the ideal buffer length if it fails.
See also https://github.com/SerenityOS/serenity/discussions/4357
This commit is contained in:
Ben Wiederhake 2021-01-16 15:48:56 +01:00 committed by Andreas Kling
parent 7ed002d1ca
commit ea5825f2c9
3 changed files with 70 additions and 12 deletions

View file

@ -61,17 +61,18 @@ int Process::sys$fchdir(int fd)
return 0;
}
int Process::sys$getcwd(Userspace<char*> buffer, ssize_t size)
int Process::sys$getcwd(Userspace<char*> buffer, size_t size)
{
REQUIRE_PROMISE(rpath);
if (size < 0)
return -EINVAL;
auto path = current_directory().absolute_path();
if ((size_t)size < path.length() + 1)
return -ERANGE;
if (!copy_to_user(buffer, path.characters(), path.length() + 1))
size_t ideal_size = path.length() + 1;
auto size_to_copy = min(ideal_size, size);
if (!copy_to_user(buffer, path.characters(), size_to_copy))
return -EFAULT;
return 0;
// Note: we return the whole size here, not the copied size.
return ideal_size;
}
}