lists.openwall.net   lists  /  announce  owl-users  owl-dev  john-users  john-dev  passwdqc-users  yescrypt  popa3d-users  /  oss-security  kernel-hardening  musl  sabotage  tlsify  passwords  /  crypt-dev  xvendor  /  Bugtraq  Full-Disclosure  linux-kernel  linux-netdev  linux-ext4  linux-hardening  linux-cve-announce  PHC 
Open Source and information security mailing list archives
 
Hash Suite: Windows password security audit tool. GUI, reports in PDF.
[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <20250509203430.3448-6-adobriyan@gmail.com>
Date: Fri,  9 May 2025 23:34:27 +0300
From: Alexey Dobriyan <adobriyan@...il.com>
To: corbet@....net
Cc: workflows@...r.kernel.org,
	linux-kernel@...r.kernel.org,
	Alexey Dobriyan <adobriyan@...il.com>
Subject: [PATCH 6/9] CodingStyle: recommend static_assert/_Static_assert

Linux's BUG_ON is done backwards (condition is inverted).
But it is a long story.

However C11/C23 allow to partially transition to what all normal
programmers are used to, namely assert().

Deprecate BUILD_BUG_ON, recommend static_assert/_Static_assert.
And then some day BUG_ON will be flipped as well.

Signed-off-by: Alexey Dobriyan <adobriyan@...il.com>
---
 Documentation/process/coding-style.rst | 23 +++++++++++++++++++----
 1 file changed, 19 insertions(+), 4 deletions(-)

diff --git a/Documentation/process/coding-style.rst b/Documentation/process/coding-style.rst
index 63c41125e713..f8d5151eb0d2 100644
--- a/Documentation/process/coding-style.rst
+++ b/Documentation/process/coding-style.rst
@@ -1302,11 +1302,26 @@ asked the kernel to crash if a WARN*() fires, and such users must be
 prepared to deal with the consequences of a system that is somewhat more
 likely to crash.
 
-Use BUILD_BUG_ON() for compile-time assertions
-**********************************************
+Use ``_Static_assert``/``static_assert`` to test things at compile time
+***********************************************************************
 
-The use of BUILD_BUG_ON() is acceptable and encouraged, because it is a
-compile-time assertion that has no effect at runtime.
+C99 has standardized ``_Static_assert`` keyword and C23 added ``static_assert``
+later making Linux specific ``BUILD_BUG_ON`` macro obsolete.
+
+Note that both forms of static assertions are classified as declaration and
+thus can be used (almost) anywhere in the code including top-level.
+Don't introduce fake functions to test things at compile time:
+
+.. code-block:: c
+
+	_Static_assert(sizeof(u32) == 4);
+	static_assert(sizeof(struct S) == 8);
+
+Also note that the second argument to ``static_assert`` can be omitted as an
+extension. Sometimes filename and line number of a compile error is all you
+need.
+
+See https://en.cppreference.com/w/c/language/_Static_assert for more information.
 
 References
 ----------
-- 
2.49.0


Powered by blists - more mailing lists

Powered by Openwall GNU/*/Linux Powered by OpenVZ