git: 2d67765f10e7 - main - malloc: Use ckdint.h helpers instead of WOULD_OVERFLOW
- Go to: [ bottom of page ] [ top of archives ] [ this month ]
Date: Mon, 17 Aug 2026 18:42:19 UTC
The branch main has been updated by markj:
URL: https://cgit.FreeBSD.org/src/commit/?id=2d67765f10e7da43ba2d4a7fc074c15d5354684b
commit 2d67765f10e7da43ba2d4a7fc074c15d5354684b
Author: Mark Johnston <markj@FreeBSD.org>
AuthorDate: 2026-08-17 18:15:17 +0000
Commit: Mark Johnston <markj@FreeBSD.org>
CommitDate: 2026-08-17 18:41:36 +0000
malloc: Use ckdint.h helpers instead of WOULD_OVERFLOW
This serves to demonstrate some usage of the ckdint.h helpers. The new
version also generates better machine code on amd64 and arm64.
Reviewed by: kib, emaste
MFC after: 2 weeks
Sponsored by: The FreeBSD Foundation
---
sys/kern/kern_malloc.c | 12 +++++++-----
1 file changed, 7 insertions(+), 5 deletions(-)
diff --git a/sys/kern/kern_malloc.c b/sys/kern/kern_malloc.c
index b0bb4511cb16..ffc260781d60 100644
--- a/sys/kern/kern_malloc.c
+++ b/sys/kern/kern_malloc.c
@@ -43,13 +43,13 @@
* description.
*/
-#include <sys/cdefs.h>
#include "opt_ddb.h"
#include "opt_vm.h"
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/asan.h>
+#include <sys/ckdint.h>
#include <sys/kdb.h>
#include <sys/kernel.h>
#include <sys/lock.h>
@@ -823,22 +823,24 @@ malloc_domainset_aligned(size_t size, size_t align,
void *
mallocarray(size_t nmemb, size_t size, struct malloc_type *type, int flags)
{
+ size_t n;
- if (WOULD_OVERFLOW(nmemb, size))
+ if (ckd_mul(&n, nmemb, size) != 0)
panic("mallocarray: %zu * %zu overflowed", nmemb, size);
- return (malloc(size * nmemb, type, flags));
+ return (malloc(n, type, flags));
}
void *
mallocarray_domainset(size_t nmemb, size_t size, struct malloc_type *type,
struct domainset *ds, int flags)
{
+ size_t n;
- if (WOULD_OVERFLOW(nmemb, size))
+ if (ckd_mul(&n, nmemb, size) != 0)
panic("mallocarray_domainset: %zu * %zu overflowed", nmemb, size);
- return (malloc_domainset(size * nmemb, type, ds, flags));
+ return (malloc_domainset(n, type, ds, flags));
}
#if defined(INVARIANTS) && !defined(KASAN)