Page Menu
Home
FreeBSD
Search
Configure Global Search
Log In
Files
F167301523
D58773.id183839.diff
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Flag For Later
Award Token
Size
4 KB
Referenced Files
None
Subscribers
None
D58773.id183839.diff
View Options
diff --git a/include/stdckdint.h b/include/stdckdint.h
--- a/include/stdckdint.h
+++ b/include/stdckdint.h
@@ -7,33 +7,11 @@
#ifndef __STDC_VERSION_STDCKDINT_H__
#define __STDC_VERSION_STDCKDINT_H__ 202311L
-#include <sys/cdefs.h>
+#include <sys/_visible.h>
#if __BSD_VISIBLE || __ISO_C_VISIBLE >= 2023
-#if __GNUC_PREREQ__(5, 1) || __has_builtin(__builtin_add_overflow)
-#define ckd_add(result, a, b) \
- __builtin_add_overflow((a), (b), (result))
-#else
-#define ckd_add(result, a, b) \
- _Static_assert(0, "checked addition not supported")
-#endif
-
-#if __GNUC_PREREQ__(5, 1) || __has_builtin(__builtin_sub_overflow)
-#define ckd_sub(result, a, b) \
- __builtin_sub_overflow((a), (b), (result))
-#else
-#define ckd_sub(result, a, b) \
- _Static_assert(0, "checked subtraction not supported")
-#endif
-
-#if __GNUC_PREREQ__(5, 1) || __has_builtin(__builtin_mul_overflow)
-#define ckd_mul(result, a, b) \
- __builtin_mul_overflow((a), (b), (result))
-#else
-#define ckd_mul(result, a, b) \
- _Static_assert(0, "checked multiplication not supported")
-#endif
+#include <sys/ckdint.h>
#endif
diff --git a/sys/dev/netmap/netmap_mem2.c b/sys/dev/netmap/netmap_mem2.c
--- a/sys/dev/netmap/netmap_mem2.c
+++ b/sys/dev/netmap/netmap_mem2.c
@@ -38,6 +38,7 @@
#ifdef __FreeBSD__
#include <sys/types.h>
+#include <sys/ckdint.h>
#include <sys/domainset.h>
#include <sys/limits.h>
#include <sys/malloc.h>
@@ -2013,16 +2014,14 @@
if (netmap_debug & NM_DEBUG_MEM)
nm_prinf("creating %s", kring->name);
ndesc = kring->nkr_num_slots;
- if (ndesc >= UINT_MAX / sizeof(struct netmap_slot)) {
+ if (ckd_mul(&len, ndesc, sizeof(struct netmap_slot))) {
error = EINVAL;
goto cleanup;
}
- len = ndesc * sizeof(struct netmap_slot);
- if (len + sizeof(struct netmap_ring) < len) {
+ if (ckd_add(&len, len, sizeof(struct netmap_ring))) {
error = EINVAL;
goto cleanup;
}
- len += sizeof(struct netmap_ring);
ring = netmap_ring_malloc(nmd, len);
if (ring == NULL) {
nm_prerr("Cannot allocate %s_ring", nm_txrx2str(t));
diff --git a/sys/kern/kern_malloc.c b/sys/kern/kern_malloc.c
--- 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 @@
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)
diff --git a/include/stdckdint.h b/sys/sys/ckdint.h
copy from include/stdckdint.h
copy to sys/sys/ckdint.h
--- a/include/stdckdint.h
+++ b/sys/sys/ckdint.h
@@ -4,16 +4,25 @@
* SPDX-License-Identifier: BSD-2-Clause
*/
-#ifndef __STDC_VERSION_STDCKDINT_H__
-#define __STDC_VERSION_STDCKDINT_H__ 202311L
+#ifndef _SYS_CKDINT_H_
+#define _SYS_CKDINT_H_
#include <sys/cdefs.h>
-#if __BSD_VISIBLE || __ISO_C_VISIBLE >= 2023
+#ifdef _KERNEL
+/* If you're not checking the return value, what's the point? */
+__nodiscard static inline bool
+__ckd_result(bool result)
+{
+ return (result);
+}
+#else
+#define __ckd_result(result) (result)
+#endif
#if __GNUC_PREREQ__(5, 1) || __has_builtin(__builtin_add_overflow)
#define ckd_add(result, a, b) \
- __builtin_add_overflow((a), (b), (result))
+ __ckd_result(__builtin_add_overflow((a), (b), (result)))
#else
#define ckd_add(result, a, b) \
_Static_assert(0, "checked addition not supported")
@@ -21,7 +30,7 @@
#if __GNUC_PREREQ__(5, 1) || __has_builtin(__builtin_sub_overflow)
#define ckd_sub(result, a, b) \
- __builtin_sub_overflow((a), (b), (result))
+ __ckd_result(__builtin_sub_overflow((a), (b), (result)))
#else
#define ckd_sub(result, a, b) \
_Static_assert(0, "checked subtraction not supported")
@@ -29,12 +38,10 @@
#if __GNUC_PREREQ__(5, 1) || __has_builtin(__builtin_mul_overflow)
#define ckd_mul(result, a, b) \
- __builtin_mul_overflow((a), (b), (result))
+ __ckd_result(__builtin_mul_overflow((a), (b), (result)))
#else
#define ckd_mul(result, a, b) \
_Static_assert(0, "checked multiplication not supported")
#endif
-#endif
-
-#endif
+#endif /* _SYS_CKDINT_H_ */
File Metadata
Details
Attached
Mime Type
text/plain
Expires
Fri, Aug 21, 7:08 PM (20 h, 20 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
37047368
Default Alt Text
D58773.id183839.diff (4 KB)
Attached To
Mode
D58773: sys: Add sys/ckdint.h
Attached
Detach File
Event Timeline
Log In to Comment