From nobody Tue Feb 10 15:28:11 2026 X-Original-To: dev-commits-src-branches@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 4f9QSF17smz6RVfN for ; Tue, 10 Feb 2026 15:28:13 +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 "R13" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4f9QSD0Ylmz477G for ; Tue, 10 Feb 2026 15:28:11 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1770737292; 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=11a00stCGC02ncchVE7Rb/lAauPMLf0/dt2gSXX+uwQ=; b=ZoFH/8ArKQqND9ybCGw3dQc9EEztq5myWq0Mu9TP9HR7LwNbjHWXx6gq0Q6XbCb12XOXzY SpI3p81b3pukg1qXLog9UTlTkfyZrvmN2MjrSW7v/t3hdQ3eocYIdukrMD/f/OlVXFOGQa iw4xptIs69nXmn1jKsmDGGXv9xNcgOyx+8l/QsCtTfSKtM5+SPhneiopjcSoSaSLFMQg7d E2gFfEucDca+ElePWy65tf4MZ9DyZMHDx6ElAXNpouAZzNL5gef7VRCbQeW+7asdsO7VcI 6DFi+kn5sBeOZ4DzB6HMV8ndujA2YhKCvgVQOyj3FGNc7aH99G9LXNGoKL6n6Q== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1770737292; a=rsa-sha256; cv=none; b=Vtwh1FB9r/fVvaUNXDC4+lr5qOLlqNC4rtn1JEmRydRK768Qr2zCgAsStMy+JQeDJlHPMg +cYBGpLXHO8LtxRN2HZb4xUO6HPquMF9OvgDbOgohcVN08ZicBUVsY1nSJdOY4bWQpkP+J DMYm36KlyZ0loLHq9x4RRyRmct+//2+uDOIBiZMTo1T0cuvW/335cQffvuyIj+aGGyKk+u R9UQiMXKcMuWmtx2Du9prRZtOEZT0l/txXwAX263vwRTcYy/guMunAmT9O52LoFchRvOmk r+A0HlirZKapdj11iAmwbwKCIo842uCdPzN4rHoqf8dtPHw7otApGUzE1ZdBTg== 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=1770737292; 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=11a00stCGC02ncchVE7Rb/lAauPMLf0/dt2gSXX+uwQ=; b=yU5/58E76x8XZYUK+X489WW6nGxao4KBhgpaRy03dTqFkWohf2ox6SHsBMqP/oiJaXm2zP soGKVZS2qS9joMeC7k0r5dXzpGVTQU0vARhVvlXOrEm4ffrrTSgXcLcKBjiRUK7Wjrdt03 YzMLAe/l+XrSux7ZrarZF5uFtAWS2MNMa5yLGkGcKZd04xlMLjk68kDPJ7I+S5j3jfQ1a4 OzjSJ0/rmydHCCFysA7sFgiQNTBrtNFn4y6iK2ut5Tyly40iQAliYtzwdVbwPpFviPA+Tr ZnqrK5Ni94d4Klg/uyNgHtPL+Dau/JRds9c8dkxoHPijZx+sdRQl78cwxDzYKA== Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) by mxrelay.nyi.freebsd.org (Postfix) with ESMTP id 4f9QSC4b6Kz1NTt for ; Tue, 10 Feb 2026 15:28:11 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from git (uid 1279) (envelope-from git@FreeBSD.org) id 44d44 by gitrepo.freebsd.org (DragonFly Mail Agent v0.13+ on gitrepo.freebsd.org); Tue, 10 Feb 2026 15:28:11 +0000 To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-branches@FreeBSD.org From: Dag-Erling=?utf-8?Q? Sm=C3=B8rg?=rav Subject: git: fda539d1ea39 - stable/13 - include: Implement N2867. List-Id: Commits to the stable branches of the FreeBSD src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-branches List-Help: List-Post: List-Subscribe: List-Unsubscribe: X-BeenThere: dev-commits-src-branches@freebsd.org Sender: owner-dev-commits-src-branches@FreeBSD.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: des X-Git-Repository: src X-Git-Refname: refs/heads/stable/13 X-Git-Reftype: branch X-Git-Commit: fda539d1ea398ce7c8aa9e7fa25e9d3c53563b5b Auto-Submitted: auto-generated Date: Tue, 10 Feb 2026 15:28:11 +0000 Message-Id: <698b4e8b.44d44.501c9786@gitrepo.freebsd.org> The branch stable/13 has been updated by des: URL: https://cgit.FreeBSD.org/src/commit/?id=fda539d1ea398ce7c8aa9e7fa25e9d3c53563b5b commit fda539d1ea398ce7c8aa9e7fa25e9d3c53563b5b Author: Dag-Erling Smørgrav AuthorDate: 2023-09-07 06:14:54 +0000 Commit: Dag-Erling Smørgrav CommitDate: 2026-02-10 14:24:30 +0000 include: Implement N2867. This adds macros for checked addition, subtraction, and multiplication with semantics similar to the builtins gcc and clang have had for years. Reviewed by: kib, emaste Differential Revision: https://reviews.freebsd.org/D41734 (cherry picked from commit e6615b10347caf67f5bc12c9a8e30b8ddd9860ae) --- include/Makefile | 2 +- include/stdckdint.h | 40 +++++++++++++++++ share/man/man3/Makefile | 4 ++ share/man/man3/stdckdint.3 | 106 +++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 151 insertions(+), 1 deletion(-) diff --git a/include/Makefile b/include/Makefile index d86fe4cc5435..dd4bb01d6339 100644 --- a/include/Makefile +++ b/include/Makefile @@ -22,7 +22,7 @@ INCS= a.out.h ar.h assert.h bitstring.h byteswap.h \ pthread_np.h pwd.h ranlib.h readpassphrase.h regex.h \ res_update.h resolv.h runetype.h sched.h \ search.h semaphore.h setjmp.h \ - signal.h spawn.h stab.h stdalign.h stdbool.h stddef.h \ + signal.h spawn.h stab.h stdalign.h stdbool.h stdckdint.h stddef.h \ stdnoreturn.h stdio.h stdlib.h string.h stringlist.h \ strings.h sysexits.h tar.h termios.h tgmath.h \ time.h timeconv.h timers.h ttyent.h \ diff --git a/include/stdckdint.h b/include/stdckdint.h new file mode 100644 index 000000000000..af3074dded89 --- /dev/null +++ b/include/stdckdint.h @@ -0,0 +1,40 @@ +/*- + * Copyright (c) 2023 Dag-Erling Smørgrav + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#ifndef __STDC_VERSION_STDCKDINT_H__ +#define __STDC_VERSION_STDCKDINT_H__ 202311L + +#include + +#if __BSD_VISIBLE || __ISO_C_VISIBLE >= 2023 + +#if __GNUC_PREREQ__(5, 1) || __has_builtin(__builtin_add_overflow) +#define ckd_add(result, a, b) \ + (_Bool)__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) \ + (_Bool)__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) \ + (_Bool)__builtin_mul_overflow((a), (b), (result)) +#else +#define ckd_mul(result, a, b) \ + _Static_assert(0, "checked multiplication not supported") +#endif + +#endif + +#endif diff --git a/share/man/man3/Makefile b/share/man/man3/Makefile index 5aec58492b8f..7fff5eedd5fb 100644 --- a/share/man/man3/Makefile +++ b/share/man/man3/Makefile @@ -29,6 +29,7 @@ MAN= arb.3 \ snl.3 \ stats.3 \ stdarg.3 \ + stdckdint.3 \ sysexits.3 \ tgmath.3 \ timeradd.3 \ @@ -310,6 +311,9 @@ MLINKS+= stdarg.3 va_arg.3 \ stdarg.3 va_end.3 \ stdarg.3 varargs.3 \ stdarg.3 va_start.3 +MLINKS+= stdckdint.3 ckd_add.3 \ + stdckdint.3 ckd_sub.3 \ + stdckdint.3 ckd_mul.3 MLINKS+= timeradd.3 timerclear.3 \ timeradd.3 timercmp.3 \ timeradd.3 timerisset.3 \ diff --git a/share/man/man3/stdckdint.3 b/share/man/man3/stdckdint.3 new file mode 100644 index 000000000000..e3593472c08b --- /dev/null +++ b/share/man/man3/stdckdint.3 @@ -0,0 +1,106 @@ +.\"- +.\" Copyright (c) 2023 Dag-Erling Smørgrav +.\" +.\" SPDX-License-Identifier: BSD-2-Clause +.\" +.Dd September 5, 2023 +.Dt STDCKDINT 3 +.Os +.Sh NAME +.Nm stdckdint +.Nd checked integer arithmetic +.Sh SYNOPSIS +.In stdckdint.h +.Ft bool +.Fn ckd_add "type1 *result" "type2 a" "type3 b" +.Ft bool +.Fn ckd_sub "type1 *result" "type2 a" "type3 b" +.Ft bool +.Fn ckd_mul "type1 *result" "type2 a" "type3 b" +.Sh DESCRIPTION +The function-like macros +.Nm ckd_add , +.Nm ckd_sub , +and +.Nm ckd_mul +perform checked integer addition, subtraction, and multiplication, +respectively. +If the result of adding, subtracting, or multiplying +.Fa a +and +.Fa b +as if their respective types had infinite range fits in +.Ft type1 , +it is stored in the location pointed to by +.Fa result +and the macro evaluates to +.Dv false . +Otherwise, the macro evaluates to +.Dv true +and the contents of the location pointed to by +.Fa result +is the result of the operation wrapped to the range of +.Ft type1 . +.Sh RETURN VALUES +The +.Nm ckd_add , +.Nm ckd_sub , +and +.Nm ckd_mul +macros evaluate to +.Dv true +if the requested operation overflowed the result type and +.Dv false +otherwise. +.Sh EXAMPLES +.Bd -literal -offset indent +#include +#include +#include + +int main(void) +{ + int result; + + assert(!ckd_add(&result, INT_MAX, 0)); + assert(result == INT_MAX); + assert(ckd_add(&result, INT_MAX, 1)); + assert(result == INT_MIN); + + assert(!ckd_sub(&result, INT_MIN, 0)); + assert(result == INT_MIN); + assert(ckd_sub(&result, INT_MIN, 1)); + assert(result == INT_MAX); + + assert(!ckd_mul(&result, INT_MAX / 2, 2)); + assert(result == INT_MAX - 1); + assert(ckd_mul(&result, INT_MAX / 2 + 1, 2)); + assert(result == INT_MIN); + + return 0; +} +.Ed +.\" .Sh STANDARDS +.\" The +.\" .Nm ckd_add , +.\" .Nm ckd_sub , +.\" and +.\" .Nm ckd_mul +.\" macros conform to +.\" .St -isoC-23 . +.Sh HISTORY +The +.Nm ckd_add , +.Nm ckd_sub , +and +.Nm ckd_mul +macros were first introduced in +.Fx 14.0 . +.Sh AUTHORS +The +.Nm ckd_add , +.Nm ckd_sub , +and +.Nm ckd_mul +macros and this manual page were written by +.An Dag-Erling Sm\(/orgrav Aq Mt des@FreeBSD.org .