[PATCH] tomoyo: fix clang pointer arithmetic warning
Arnd Bergmann
arnd at kernel.org
Wed Oct 28 13:57:21 UTC 2020
On Wed, Oct 28, 2020 at 2:22 PM Tetsuo Handa
<penguin-kernel at i-love.sakura.ne.jp> wrote:
>
> Thank you for a patch. I have two questions.
>
> On 2020/10/27 6:52, Arnd Bergmann wrote:
> > From: Arnd Bergmann <arnd at arndb.de>
> >
> > clang warns about additions on NULL pointers being undefined in C:
> >
> > security/tomoyo/securityfs_if.c:226:59: warning: arithmetic on a null pointer treated as a cast from integer to pointer is a GNU extension [-Wnull-pointer-arithmetic]
> > securityfs_create_file(name, mode, parent, ((u8 *) NULL) + key,
> >
> > Change the code to instead use a cast through uintptr_t to avoid
> > the warning.
> >
>
> > - securityfs_create_file(name, mode, parent, ((u8 *) NULL) + key,
> > + securityfs_create_file(name, mode, parent, (u8 *)(uintptr_t)key,
> > &tomoyo_operations);
>
> (1) Does clang warn if "(void *)key" is used instead of "(u8 *)(uintptr_t)key" ?
Yes, both clang and gcc warn when you cast between a pointer and
an integer of a different size.
> (2) tomoyo_open() has
>
> const int key = ((u8 *) file_inode(file)->i_private) - ((u8 *) NULL);
>
> which decodes the "u8 key" passed to tomoyo_create_entry(). For symmetry,
> I'd like to remove NULL from tomoyo_open() as well. Does clang warn if
>
> const int key = (u8) (file_inode(file)->i_private);
>
> is used?
Yes, same thing, but
const int key = (uintptr_t)file_inode(file)->i_private;
works without warnings and seems clearer.
Arnd
More information about the Linux-security-module-archive
mailing list