Re: [PATCH] overflow: Introduce struct_offset() to get offset of member

Kees Cook
2025-11-28T02:00:03Z

On November 27, 2025 5:43:42 PM PST, Steven Rostedt <rostedt@goodmis.org> wrote:
>On Wed, 26 Nov 2025 23:58:01 -0800
>Kees Cook <kees@kernel.org> wrote:
>
>> > +/**
>> > + * struct_offset() - Calculate the offset of a member within a struct
>> > + * @p: Pointer to the struct
>> > + * @member: Name of the member to get the offset of
>> > + *
>> > + * Calculates the offset of a particular @member of the structure pointed
>> > + * to by @p.
>> > + *
>> > + * Return: number of bytes to the location of @member.
>> > + */
>> > +#define struct_offset(p, member) (offsetof(typeof(*(p)), member))
>>
>> I wonder if the kerndoc for this and offsetof() should reference each
>> other? "For a type instead of a pointer, use offsetof()" etc...
>
>I know I pushed this to my for-next branch already, but it's the top
>patch. Looking at my code, I actually have a lot of places that use the
>offsetof() for a structure variable and not a pointer to a structure.
>
>Thus, I wonder if it is better to have this as:
>
>#define struct_offset(s, member) (offsetof(typeof(s), member))

I'd rather it keep the same API style as struct_size() if it's going to share the naming style.

If you have an instance and not a pointer, just slap on a & :)

--
Kees Cook