* [PATCH v2] docs: deprecated.rst: Add note to the use of struct_size() helper
@ 2020-06-04 18:44 Gustavo A. R. Silva
0 siblings, 0 replies; only message in thread
From: Gustavo A. R. Silva @ 2020-06-04 18:44 UTC (permalink / raw)
To: Jonathan Corbet, Kees Cook; +Cc: linux-doc, linux-kernel, Gustavo A. R. Silva
Add a note to educate people about the proper use of struct_size() when
the trailing array in the enclosing structure is a one-element array.
Zero-length and one-element arrays will soon be removed from the kernel,
but in the meantime, it's worth letting people know how to correctly
use struct_size() together with such constructs.
Note: this documentation will be updated once all zero-length and
one-element arrays have been completely removed from the kernel.
Signed-off-by: Gustavo A. R. Silva <gustavoars@kernel.org>
---
Changes in v2:
- Suggest stop using one-element arrays and switch to flexible arrays.
- Update changelog text.
Documentation/process/deprecated.rst | 12 ++++++++++++
1 file changed, 12 insertions(+)
diff --git a/Documentation/process/deprecated.rst b/Documentation/process/deprecated.rst
index 652e2aa02a66c..1af3a021f4b33 100644
--- a/Documentation/process/deprecated.rst
+++ b/Documentation/process/deprecated.rst
@@ -85,6 +85,18 @@ Instead, use the helper::
header = kzalloc(struct_size(header, item, count), GFP_KERNEL);
+NOTE: You might want to use the following form in case the trailing array
+is a one-element array, as unlike zero-length arrays and flexible-array
+members, `one-element arrays occupy at least as much space as a single
+object of the type <https://gcc.gnu.org/onlinedocs/gcc/Zero-Length.html>`_,
+hence they contribute to the size of the enclosing structure::
+
+ header = kzalloc(struct_size(header, item, count - 1), GFP_KERNEL);
+
+It's also worth noting that one-element arrays --together with zero-length
+arrays-- will soon be completely removed from the codebase and deprecated.
+So, you better STOP using them and switch to flexible arrays instead.
+
See array_size(), array3_size(), and struct_size(),
for more details as well as the related check_add_overflow() and
check_mul_overflow() family of functions.
--
2.27.0
^ permalink raw reply related [flat|nested] only message in thread
only message in thread, other threads:[~2020-06-04 18:39 UTC | newest]
Thread overview: (only message) (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2020-06-04 18:44 [PATCH v2] docs: deprecated.rst: Add note to the use of struct_size() helper Gustavo A. R. Silva
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).