tomoyo: fix clang pointer arithmetic warning
Subsystems (2)
PATCH 1/1 tomoyo: fix clang pointer arithmetic warning
From: Arnd Bergmann <[email protected]>
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.
--- security/tomoyo/securityfs_if.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
security/tomoyo/securityfs_if.c +1 -1
@@ -223,7 +223,7 @@ static const struct file_operations tomoyo_operations = {static void __init tomoyo_create_entry (const char *name,const umode_t mode,struct dentry *parent,const u8 key) { -securityfs_create_file (name, mode, parent, ((u8 *)NULL ) + key, +securityfs_create_file (name, mode, parent, (u8 *)(uintptr_t )key, &tomoyo_operations); }
Signature · 1 line
2.27.0
Tetsuo Handa
maintainerThank you for a patch. I have two questions.
Quoted 11 lines · Arnd Bergmann
From: Arnd Bergmann <[email protected]>
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_tto avoid the warning.
security/tomoyo/securityfs_if.c static const struct file_operations tomoyo_operations = { · line 227
Quoted 3 lines
security/tomoyo/securityfs_if.c +1
-1-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" ?
(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?Quoted 15 lines · Tetsuo Handa
Thank you for a patch. I have two questions.
Quoted 10 lines · Arnd Bergmann
From: Arnd Bergmann <[email protected]>
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_tto avoid the warning.
Quoted 5 lines
security/tomoyo/securityfs_if.c static const struct file_operations tomoyo_operations = { · line 227
Quoted 3 lines
security/tomoyo/securityfs_if.c +1
-1-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.
Quoted 10 lines
(2)
tomoyo_open()hasconst 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 fromtomoyo_open()as well. Does clang warn ifconst 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
Tetsuo Handa
maintainerOK. I will apply the following patch to my tree. Thank you.
From: Arnd Bergmann <[email protected]>
Subject: [PATCH] tomoyo: fix clang pointer arithmetic warning
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.
--- security/tomoyo/securityfs_if.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)
security/tomoyo/securityfs_if.c +3 -3
@@ -131,8 +131,8 @@ static const struct file_operations tomoyo_self_operations = {*/ static int tomoyo_open (struct inode *inode,struct file *file) { -const int key = ((u8 *)file_inode (file)->i_private) - - ((u8 *)NULL ); +const u8 key = (uintptr_t )file_inode (file)->i_private; +return tomoyo_open_control (key, file); }
@@ -223,7 +223,7 @@ static const struct file_operations tomoyo_operations = {static void __init tomoyo_create_entry (const char *name,const umode_t mode,struct dentry *parent,const u8 key) { -securityfs_create_file (name, mode, parent, ((u8 *)NULL ) + key, +securityfs_create_file (name, mode, parent, (void *) (uintptr_t ) key, &tomoyo_operations); }
Signature · 1 line
2.18.4
Quoted 5 lines · Arnd Bergmann
From: Arnd Bergmann <[email protected]>
clang warns about additions on NULL pointers being undefined in C:
Quoted 8 lines
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_tto avoid the warning.Fixes 9590837b89aa ("Common functions for TOMOYO Linux.")Signed-off-by Arnd Bergmann <[email protected]>
Thanks for the patch.
Quoted 20 lines
--- security/tomoyo/securityfs_if.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)security/tomoyo/securityfs_if.c +1
-2@@ -223,7 +223,7 @@ static const struct file_operations tomoyo_operations = {static void __init tomoyo_create_entry (const char *name,const umode_t mode,struct dentry *parent,const u8 key) { -securityfs_create_file (name, mode, parent, ((u8 *)NULL ) + key, +securityfs_create_file (name, mode, parent, (u8 *)(uintptr_t )key, &tomoyo_operations); } --2.27.0
Signature · 2 lines
Thanks, ~Nick Desaulniers