Skip to contentSkip to conversation

tomoyo: fix clang pointer arithmetic warning

PATCH In review Arnd Bergmann 3 participants 5 messages Oct 26, 2020 – Nov 11, 2020

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

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.

Fixes 9590837b89aa ("Common functions for TOMOYO Linux.")
---
 security/tomoyo/securityfs_if.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
security/tomoyo/securityfs_if.c +1 -1
index 546281c5b233..0a5f00073ef1 100644
@@ -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
maintainer
#1 lore raw

Thank 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_t to 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?
#2 lore raw
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_t to 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() 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

maintainer
#3 lore raw

OK. 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
index 546281c5b233..065f4941c4d8 100644
@@ -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
Reviewed-by on v1 1/1
#4 lore raw
In reply to Arnd Bergmann
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_t to avoid the warning.

Fixes 9590837b89aa ("Common functions for TOMOYO Linux.")

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
index 546281c5b233..0a5f00073ef1 100644
@@ -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