From nobody Wed Sep 02 22:40:45 2026 X-Original-To: dev-commits-src-all@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 4hZyPB2vBDz6qpL4 for ; Wed, 02 Sep 2026 22:40:46 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "YR2" (not verified)) by mx1.freebsd.org (Postfix) with ESMTPS id 4hZyP96CsFz41gK for ; Wed, 02 Sep 2026 22:40:45 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1788388845; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=xbmVfsige7Z4PA69KG/Aep6FpgD6C3gG8ijMeHo6QJE=; b=n3OD24jRjAtreFOOWEuAWgDaEqq6ldoe60o/QIhdK2FVOhXAxlikolDb7GZ1QrrKnRN5MC 5D5oebNYxs5NFXVNoFSXPdn9QT5MFjyVCLB9GdSiagHMJFsTqfIVCHG3CkNvfz5UwIVhZ0 ChRg22v5+347JxWV9qWT7lRJqYxetjNAAZjf1RSw5es5ZMfW0j2DfVQJILpCDu4LhjBuKt UkvtlcOlBNUklDsBNCDl2juULK/qJ3nJQfqT2KuGS1/mAsMlK4S1h2m+dE1nZgYv2Aeuaf IEgqzTpGAyNmfVza2LDVpHbezj5jwrbkttL5WCGXAk1cNxlBu1U2c2zFwRTAqw== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1788388845; a=rsa-sha256; cv=none; b=mDl6Y1eCZFc0VWWdOJhZIIGI5Iz64GsAp8AxMhNpzzLRPYzC8hioDNm0baeb+WlG1KM2id dkOr0HVgfhiBtPidwP/VMwbP13ZPLeV1BiTbLmbs/AdstSDbEyuJ+Niyi7V7WO+E4uc2HY OR1TpEaqV7rPXxERHJAZS/Kj3aIwqPVXu13U9LL4o8IIDaUbo6QDOgTY0yIojGSbm3+qHW 8Aup+gvte7FRtD1s4aM+LOdgkrKBzg306Rt1JZX6dsSelZYi4BeIl3xPpQLKbyMUv3eZ59 Y0gQdqOU3O1hasKFt/S4XBZQRrbvOA7fWjTF19ukm1gMQ3LLiwzPmfEN9hLTiA== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1788388845; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=xbmVfsige7Z4PA69KG/Aep6FpgD6C3gG8ijMeHo6QJE=; b=huXqsIk4pd0N+yzlwJnHZwi7hBOR9iWp3FpfzOox50vUjS+RY0EC2xIOkKxHRlGPje3r4l aEAk3VtyGo5Zg3saAQz6GIdy5DyYtjTQJCzpxGz76C7Z8Yaymw5oWcYix9nu4SSLKGGwh4 HOxKiOdlIfp85TtLl7t5uuM70ylFtocSZmLQtB55sLJOnnP6t++0ziNf3dMSiPbA6yGeoQ eKgaHFDc2pBUJtSV91Y98Ykt+7qGVq0aS3H/dE0VJWSJEgrKow36WydWWbngdKmQd/tuEc 55/JFy2SweOPJAjCwz4SkTN5dM+Kk6178hU8I8ox3Y2Af7zneRewf9EAcjsaPQ== Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) by mxrelay.nyi.freebsd.org (Postfix) with ESMTP id 4hZyP94BJqz12dZ for ; Wed, 02 Sep 2026 22:40:45 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from git (uid 1279) (envelope-from git@FreeBSD.org) id 3f96a by gitrepo.freebsd.org (DragonFly Mail Agent v0.13+ on gitrepo.freebsd.org); Wed, 02 Sep 2026 22:40:45 +0000 To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-branches@FreeBSD.org From: Mark Johnston Subject: git: b955d36a9b36 - stable/15 - sys: Add sys/ckdint.h List-Id: Commit messages for all branches of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-all List-Help: List-Post: List-Subscribe: List-Unsubscribe: X-BeenThere: dev-commits-src-all@freebsd.org Sender: owner-dev-commits-src-all@FreeBSD.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: markj X-Git-Repository: src X-Git-Refname: refs/heads/stable/15 X-Git-Reftype: branch X-Git-Commit: b955d36a9b36b04b9d434a12aac3dcd31a7574ad Auto-Submitted: auto-generated Date: Wed, 02 Sep 2026 22:40:45 +0000 Message-Id: <6a98a5ed.3f96a.49a111c1@gitrepo.freebsd.org> The branch stable/15 has been updated by markj: URL: https://cgit.FreeBSD.org/src/commit/?id=b955d36a9b36b04b9d434a12aac3dcd31a7574ad commit b955d36a9b36b04b9d434a12aac3dcd31a7574ad Author: Mark Johnston AuthorDate: 2026-08-17 17:06:04 +0000 Commit: Mark Johnston CommitDate: 2026-09-02 17:40:28 +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 (cherry picked from commit 37bd69d43c70346b9191f7ce07ee9ed783ce528f) --- 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 +#include #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 #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 + +#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_ */