* [PATCH] fs: Replace offsetof() with struct_size()
@ 2025-08-29 10:03 Xichao Zhao
2025-08-29 10:39 ` Jan Kara
0 siblings, 1 reply; 2+ messages in thread
From: Xichao Zhao @ 2025-08-29 10:03 UTC (permalink / raw)
To: viro, brauner; +Cc: jack, linux-fsdevel, linux-kernel, Xichao Zhao
When dealing with structures containing flexible arrays, struct_size()
provides additional compile-time checks compared to offsetof(). This
enhances code robustness and reduces the risk of potential errors.
Signed-off-by: Xichao Zhao <zhao.xichao@vivo.com>
---
fs/namei.c | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/fs/namei.c b/fs/namei.c
index 10f7caff7f0f..70a71b1b8abc 100644
--- a/fs/namei.c
+++ b/fs/namei.c
@@ -178,7 +178,7 @@ getname_flags(const char __user *filename, int flags)
* userland.
*/
if (unlikely(len == EMBEDDED_NAME_MAX)) {
- const size_t size = offsetof(struct filename, iname[1]);
+ const size_t size = struct_size(result, iname, 1);
kname = (char *)result;
/*
@@ -253,7 +253,7 @@ struct filename *getname_kernel(const char * filename)
if (len <= EMBEDDED_NAME_MAX) {
result->name = (char *)result->iname;
} else if (len <= PATH_MAX) {
- const size_t size = offsetof(struct filename, iname[1]);
+ const size_t size = struct_size(result, iname, 1);
struct filename *tmp;
tmp = kmalloc(size, GFP_KERNEL);
--
2.34.1
^ permalink raw reply related [flat|nested] 2+ messages in thread
* Re: [PATCH] fs: Replace offsetof() with struct_size()
2025-08-29 10:03 [PATCH] fs: Replace offsetof() with struct_size() Xichao Zhao
@ 2025-08-29 10:39 ` Jan Kara
0 siblings, 0 replies; 2+ messages in thread
From: Jan Kara @ 2025-08-29 10:39 UTC (permalink / raw)
To: Xichao Zhao; +Cc: viro, brauner, jack, linux-fsdevel, linux-kernel
On Fri 29-08-25 18:03:28, Xichao Zhao wrote:
> When dealing with structures containing flexible arrays, struct_size()
> provides additional compile-time checks compared to offsetof(). This
> enhances code robustness and reduces the risk of potential errors.
>
> Signed-off-by: Xichao Zhao <zhao.xichao@vivo.com>
Looks good. Feel free to add:
Reviewed-by: Jan Kara <jack@suse.cz>
Honza
> ---
> fs/namei.c | 4 ++--
> 1 file changed, 2 insertions(+), 2 deletions(-)
>
> diff --git a/fs/namei.c b/fs/namei.c
> index 10f7caff7f0f..70a71b1b8abc 100644
> --- a/fs/namei.c
> +++ b/fs/namei.c
> @@ -178,7 +178,7 @@ getname_flags(const char __user *filename, int flags)
> * userland.
> */
> if (unlikely(len == EMBEDDED_NAME_MAX)) {
> - const size_t size = offsetof(struct filename, iname[1]);
> + const size_t size = struct_size(result, iname, 1);
> kname = (char *)result;
>
> /*
> @@ -253,7 +253,7 @@ struct filename *getname_kernel(const char * filename)
> if (len <= EMBEDDED_NAME_MAX) {
> result->name = (char *)result->iname;
> } else if (len <= PATH_MAX) {
> - const size_t size = offsetof(struct filename, iname[1]);
> + const size_t size = struct_size(result, iname, 1);
> struct filename *tmp;
>
> tmp = kmalloc(size, GFP_KERNEL);
> --
> 2.34.1
>
--
Jan Kara <jack@suse.com>
SUSE Labs, CR
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2025-08-29 10:40 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-08-29 10:03 [PATCH] fs: Replace offsetof() with struct_size() Xichao Zhao
2025-08-29 10:39 ` Jan Kara
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).