diff options
author | Ben Wiederhake <BenWiederhake.GitHub@gmx.de> | 2021-01-16 15:48:56 +0100 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2021-01-16 22:40:53 +0100 |
commit | ea5825f2c9e2cc641daa5584b6129c1f7ef3fa9e (patch) | |
tree | 5771045035aa4ef5885c3081eaab845656ee76ba /Kernel | |
parent | 7ed002d1ca52fed6e31ad50f5dff471dcb62be5f (diff) | |
download | serenity-ea5825f2c9e2cc641daa5584b6129c1f7ef3fa9e.zip |
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
Diffstat (limited to 'Kernel')
-rw-r--r-- | Kernel/Process.h | 2 | ||||
-rw-r--r-- | Kernel/Syscalls/chdir.cpp | 15 |
2 files changed, 9 insertions, 8 deletions
diff --git a/Kernel/Process.h b/Kernel/Process.h index 946aee0594..3d8bfba55b 100644 --- a/Kernel/Process.h +++ b/Kernel/Process.h @@ -263,7 +263,7 @@ public: int sys$select(const Syscall::SC_select_params*); int sys$poll(Userspace<const Syscall::SC_poll_params*>); ssize_t sys$get_dir_entries(int fd, void*, ssize_t); - int sys$getcwd(Userspace<char*>, ssize_t); + int sys$getcwd(Userspace<char*>, size_t); int sys$chdir(Userspace<const char*>, size_t); int sys$fchdir(int fd); int sys$sleep(unsigned seconds); diff --git a/Kernel/Syscalls/chdir.cpp b/Kernel/Syscalls/chdir.cpp index 46cf3a2459..23555e2cff 100644 --- a/Kernel/Syscalls/chdir.cpp +++ b/Kernel/Syscalls/chdir.cpp @@ -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; } } |