diff options
Diffstat (limited to 'Kernel/StdLib.h')
-rw-r--r-- | Kernel/StdLib.h | 72 |
1 files changed, 57 insertions, 15 deletions
diff --git a/Kernel/StdLib.h b/Kernel/StdLib.h index 1a9398700f..00b8912987 100644 --- a/Kernel/StdLib.h +++ b/Kernel/StdLib.h @@ -26,6 +26,7 @@ #pragma once +#include <AK/Checked.h> #include <AK/Forward.h> #include <AK/Userspace.h> @@ -34,12 +35,13 @@ struct StringArgument; } String copy_string_from_user(const char*, size_t); +String copy_string_from_user(Userspace<const char*>, size_t); extern "C" { -void copy_to_user(void*, const void*, size_t); -void copy_from_user(void*, const void*, size_t); -void memset_user(void*, int, size_t); +[[nodiscard]] bool copy_to_user(void*, const void*, size_t); +[[nodiscard]] bool copy_from_user(void*, const void*, size_t); +[[nodiscard]] bool memset_user(void*, int, size_t); void* memcpy(void*, const void*, size_t); int strncmp(const char* s1, const char* s2, size_t n); @@ -57,37 +59,77 @@ inline u16 htons(u16 w) { return (w & 0xff) << 8 | ((w >> 8) & 0xff); } } template<typename T> -inline void copy_from_user(T* dest, const T* src) +[[nodiscard]] inline bool copy_from_user(T* dest, const T* src) { - copy_from_user(dest, src, sizeof(T)); + return copy_from_user(dest, src, sizeof(T)); } template<typename T> -inline void copy_to_user(T* dest, const T* src) +[[nodiscard]] inline bool copy_to_user(T* dest, const T* src) { - copy_to_user(dest, src, sizeof(T)); + return copy_to_user(dest, src, sizeof(T)); } template<typename T> -inline void copy_from_user(T* dest, Userspace<const T*> src) +[[nodiscard]] inline bool copy_from_user(T* dest, Userspace<const T*> src) { - copy_from_user(dest, src.unsafe_userspace_ptr(), sizeof(T)); + return copy_from_user(dest, src.unsafe_userspace_ptr(), sizeof(T)); } template<typename T> -inline void copy_to_user(Userspace<T*> dest, const T* src) +[[nodiscard]] inline bool copy_to_user(Userspace<T*> dest, const T* src) { - copy_to_user(dest.unsafe_userspace_ptr(), src, sizeof(T)); + return copy_to_user(dest.unsafe_userspace_ptr(), src, sizeof(T)); } template<typename T> -inline void copy_to_user(Userspace<T*> dest, const void* src, size_t size) +[[nodiscard]] inline bool copy_to_user(Userspace<T*> dest, const void* src, size_t size) { - copy_to_user(dest.unsafe_userspace_ptr(), src, size); + return copy_to_user(dest.unsafe_userspace_ptr(), src, size); } template<typename T> -inline void copy_from_user(void* dest, Userspace<const T*> src, size_t size) +[[nodiscard]] inline bool copy_from_user(void* dest, Userspace<const T*> src, size_t size) { - copy_from_user(dest, src.unsafe_userspace_ptr(), size); + return copy_from_user(dest, src.unsafe_userspace_ptr(), size); +} + +template<typename T> +[[nodiscard]] inline bool copy_n_from_user(T* dest, const T* src, size_t count) +{ + Checked size = sizeof(T); + size *= count; + if (size.has_overflow()) + return false; + return copy_from_user(dest, src, sizeof(T) * count); +} + +template<typename T> +[[nodiscard]] inline bool copy_n_to_user(T* dest, const T* src, size_t count) +{ + Checked size = sizeof(T); + size *= count; + if (size.has_overflow()) + return false; + return copy_to_user(dest, src, sizeof(T) * count); +} + +template<typename T> +[[nodiscard]] inline bool copy_n_from_user(T* dest, Userspace<const T*> src, size_t count) +{ + Checked size = sizeof(T); + size *= count; + if (size.has_overflow()) + return false; + return copy_from_user(dest, src.unsafe_userspace_ptr(), sizeof(T) * count); +} + +template<typename T> +[[nodiscard]] inline bool copy_n_to_user(Userspace<T*> dest, const T* src, size_t count) +{ + Checked size = sizeof(T); + size *= count; + if (size.has_overflow()) + return false; + return copy_to_user(dest.unsafe_userspace_ptr(), src, sizeof(T) * count); } |