summaryrefslogtreecommitdiff
path: root/Kernel/Syscalls/chdir.cpp
diff options
context:
space:
mode:
authorBen Wiederhake <BenWiederhake.GitHub@gmx.de>2021-01-16 15:48:56 +0100
committerAndreas Kling <kling@serenityos.org>2021-01-16 22:40:53 +0100
commitea5825f2c9e2cc641daa5584b6129c1f7ef3fa9e (patch)
tree5771045035aa4ef5885c3081eaab845656ee76ba /Kernel/Syscalls/chdir.cpp
parent7ed002d1ca52fed6e31ad50f5dff471dcb62be5f (diff)
downloadserenity-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/Syscalls/chdir.cpp')
-rw-r--r--Kernel/Syscalls/chdir.cpp15
1 files changed, 8 insertions, 7 deletions
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;
}
}