mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-12-04 05:20:30 +00:00
LibC: Fix negation overflow UB in sys/mman.cpp
When the system calls return `NumericLimits<ptrdiff_t>::min()`, negating the return code would produce `NumericLimits<ptrdiff_t>::max() + 1` since we are on a two's complement architecture. Because this value cannot be stored, signed overflow occurs which is UB. This can be fixed by applying the negation to `EMAXERRNO` since that's known to contain a relatively small value. Found when running tests with Clang.
This commit is contained in:
parent
146dcf4856
commit
78e7ff008b
Notes:
sideshowbarker
2024-07-18 07:15:12 +09:00
Author: https://github.com/BertalanD Commit: https://github.com/SerenityOS/serenity/commit/78e7ff008b5 Pull-request: https://github.com/SerenityOS/serenity/pull/8718 Issue: https://github.com/SerenityOS/serenity/issues/363 Reviewed-by: https://github.com/gunnarbeutner ✅ Reviewed-by: https://github.com/nico
1 changed files with 3 additions and 3 deletions
|
@ -17,7 +17,7 @@ void* serenity_mmap(void* addr, size_t size, int prot, int flags, int fd, off_t
|
|||
{
|
||||
Syscall::SC_mmap_params params { (uintptr_t)addr, size, alignment, prot, flags, fd, offset, { name, name ? strlen(name) : 0 } };
|
||||
ptrdiff_t rc = syscall(SC_mmap, ¶ms);
|
||||
if (rc < 0 && -rc < EMAXERRNO) {
|
||||
if (rc < 0 && rc > -EMAXERRNO) {
|
||||
errno = -rc;
|
||||
return MAP_FAILED;
|
||||
}
|
||||
|
@ -38,7 +38,7 @@ void* mremap(void* old_address, size_t old_size, size_t new_size, int flags)
|
|||
{
|
||||
Syscall::SC_mremap_params params { (uintptr_t)old_address, old_size, new_size, flags };
|
||||
ptrdiff_t rc = syscall(SC_mremap, ¶ms);
|
||||
if (rc < 0 && -rc < EMAXERRNO) {
|
||||
if (rc < 0 && rc > -EMAXERRNO) {
|
||||
errno = -rc;
|
||||
return MAP_FAILED;
|
||||
}
|
||||
|
@ -77,7 +77,7 @@ int madvise(void* address, size_t size, int advice)
|
|||
void* allocate_tls(const char* initial_data, size_t size)
|
||||
{
|
||||
ptrdiff_t rc = syscall(SC_allocate_tls, initial_data, size);
|
||||
if (rc < 0 && -rc < EMAXERRNO) {
|
||||
if (rc < 0 && rc > -EMAXERRNO) {
|
||||
errno = -rc;
|
||||
return MAP_FAILED;
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue