commit: aaaf1052ff7bed04feece2a1d865974eb578ef57
Author: Mike Gilbert <floppym <AT> gentoo <DOT> org>
AuthorDate: Sat Mar 8 19:30:13 2025 +0000
Commit: Mike Gilbert <floppym <AT> gentoo <DOT> org>
CommitDate: Sat Mar 8 19:36:04 2025 +0000
URL: https://gitweb.gentoo.org/proj/sandbox.git/commit/?id=aaaf1052
libsandbox: fix pagesize rounding
Signed-off-by: Mike Gilbert <floppym <AT> gentoo.org>
libsandbox/memory.c | 22 +++++++++-------------
1 file changed, 9 insertions(+), 13 deletions(-)
diff --git a/libsandbox/memory.c b/libsandbox/memory.c
index 1446116..c861fbc 100644
--- a/libsandbox/memory.c
+++ b/libsandbox/memory.c
@@ -53,15 +53,16 @@ static void *sb_mremap(void *old_address, size_t old_size,
size_t new_size, int
#define ALIGN_FACTOR 2
#define ALIGN_SIZE (ALIGN_FACTOR * sizeof(size_t))
-void *malloc(size_t size)
+static size_t round_to_page(size_t size)
{
long pagesize = sysconf(_SC_PAGESIZE);
+ size_t remainder = size % pagesize;
+ return remainder ? size + pagesize - remainder : size;
+}
- if (__builtin_add_overflow(size, ALIGN_SIZE, &size) ||
- __builtin_add_overflow(size, pagesize - size %
pagesize, &size)) {
- errno = ENOMEM;
- return NULL;
- }
+void *malloc(size_t size)
+{
+ size = round_to_page(size + ALIGN_SIZE);
size_t *sp = mmap(0, size, PROT_READ|PROT_WRITE,
MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
if (sp == MAP_FAILED)
@@ -103,18 +104,13 @@ void *realloc(void *ptr, size_t size)
{
if (ptr == NULL)
return malloc(size);
+
if (size == 0) {
free(ptr);
return NULL;
}
- long pagesize = sysconf(_SC_PAGESIZE);
-
- if (__builtin_add_overflow(size, ALIGN_SIZE, &size) ||
- __builtin_add_overflow(size, pagesize - size %
pagesize, &size)) {
- errno = ENOMEM;
- return NULL;
- }
+ size = round_to_page(size + ALIGN_SIZE);
size_t *sp = ptr;
sp -= 2;