git: 37bd69d43c70 - main - sys: Add sys/ckdint.h
Mark Johnston <[email protected]>
| Newsgroups | gmane.os.freebsd.devel.cvs.src,gmane.os.freebsd.current.scm |
|---|---|
| Message-ID | <[email protected]> |
The branch main has been updated by markj: URL: https://cgit.FreeBSD.org/src/commit/?id=37bd69d43c70346b9191f7ce07ee9ed783ce528f commit 37bd69d43c70346b9191f7ce07ee9ed783ce528f Author: Mark Johnston <[email protected]> AuthorDate: 2026-08-17 17:06:04 +0000 Commit: Mark Johnston <[email protected]> CommitDate: 2026-08-17 18:41:36 +0000 sys: Add sys/ckdint.h We have a C23 stdckdint.h header for userspace, which provides checked addition, subtraction and multiplication. We lack similar helpers in the kernel, where they are regularly needed. Let's just adopt the C23 macros. For bonus points, I added a wrapper to ensure that ignored an return value is raised as an error by the compiler. Reviewed by: kib, emaste MFC after: 2 weeks Sponsored by: The FreeBSD Foundation Differential Revision: https://reviews.freebsd.org/D58773 --- include/stdckdint.h | 26 ++------------------------ sys/sys/ckdint.h | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+), 24 deletions(-) diff --git a/include/stdckdint.h b/include/stdckdint.h index 9cb877fe8198..fe1e100abe32 100644 --- 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/sys/ckdint.h b/sys/sys/ckdint.h new file mode 100644 index 000000000000..273d38fd713b --- /dev/null +++ b/sys/sys/ckdint.h @@ -0,0 +1,47 @@ +/*- + * Copyright (c) 2023 Dag-Erling Smørgrav + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#ifndef _SYS_CKDINT_H_ +#define _SYS_CKDINT_H_ + +#include <sys/cdefs.h> + +#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) \ + __ckd_result(__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) \ + __ckd_result(__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) \ + __ckd_result(__builtin_mul_overflow((a), (b), (result))) +#else +#define ckd_mul(result, a, b) \ + _Static_assert(0, "checked multiplication not supported") +#endif + +#endif /* _SYS_CKDINT_H_ */