From 5ed48d7d43dc5df7d917f596475a1f633eeeda3a Mon Sep 17 00:00:00 2001 From: Michael Gilbert Date: Tue, 13 Sep 2022 01:46:21 +0100 Subject: [PATCH] fix incomplete memory wrap around logic (gcc 12 -Waddress) Gbp-Pq: Topic warnings Gbp-Pq: Name address.patch --- dlls/ntdll/unix/virtual.c | 11 +++++++++-- libs/wine/mmap.c | 10 ++++++++-- 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/dlls/ntdll/unix/virtual.c b/dlls/ntdll/unix/virtual.c index 1f817cd..0ed89b6 100644 --- a/dlls/ntdll/unix/virtual.c +++ b/dlls/ntdll/unix/virtual.c @@ -30,6 +30,7 @@ #include #include #include +#include #include #include #include @@ -235,10 +236,13 @@ void *anon_mmap_alloc( size_t size, int prot ) static void mmap_add_reserved_area( void *addr, SIZE_T size ) { + SIZE_T max_size = SIZE_MAX - (SIZE_T)addr; struct reserved_area *area; struct list *ptr; - if (!((char *)addr + size)) size--; /* avoid wrap-around */ + _Static_assert(sizeof(SIZE_MAX) == sizeof(SIZE_T), "SIZE_MAX and SIZE_T are not the same size"); + + if (size > max_size) size = max_size; /* avoid wrap-around */ LIST_FOR_EACH( ptr, &reserved_areas ) { @@ -284,10 +288,13 @@ static void mmap_add_reserved_area( void *addr, SIZE_T size ) static void mmap_remove_reserved_area( void *addr, SIZE_T size ) { + SIZE_T max_size = SIZE_MAX - (SIZE_T)addr; struct reserved_area *area; struct list *ptr; - if (!((char *)addr + size)) size--; /* avoid wrap-around */ + _Static_assert(sizeof(SIZE_MAX) == sizeof(SIZE_T), "SIZE_MAX and SIZE_T are not the same size"); + + if (size > max_size) size = max_size; /* avoid wrap-around */ ptr = list_head( &reserved_areas ); /* find the first area covering address */ diff --git a/libs/wine/mmap.c b/libs/wine/mmap.c index ad51153..c8b6bba 100644 --- a/libs/wine/mmap.c +++ b/libs/wine/mmap.c @@ -498,10 +498,13 @@ void mmap_init(void) */ void wine_mmap_add_reserved_area_obsolete( void *addr, size_t size ) { + size_t max_size = SIZE_MAX - (size_t)addr; struct reserved_area *area; struct list *ptr; - if (!((char *)addr + size)) size--; /* avoid wrap-around */ + _Static_assert(sizeof(SIZE_MAX) == sizeof(size_t), "SIZE_MAX and SIZE_T are not the same size"); + + if (size > max_size) size = max_size; /* avoid wrap-around */ LIST_FOR_EACH( ptr, &reserved_areas ) { @@ -557,10 +560,13 @@ void wine_mmap_add_reserved_area_obsolete( void *addr, size_t size ) */ void wine_mmap_remove_reserved_area_obsolete( void *addr, size_t size, int unmap ) { + size_t max_size = SIZE_MAX - (size_t)addr; struct reserved_area *area; struct list *ptr; - if (!((char *)addr + size)) size--; /* avoid wrap-around */ + _Static_assert(sizeof(SIZE_MAX) == sizeof(size_t), "SIZE_MAX and SIZE_T are not the same size"); + + if (size > max_size) size = max_size; /* avoid wrap-around */ ptr = list_head( &reserved_areas ); /* find the first area covering address */ -- 2.30.2