Skip to contentSkip to conversation

IB/qib: Fix null pointer subtraction compiler warning

PATCH Applied Jason Gunthorpe 2 participants 3 messages Sep 3, 2021 – Sep 8, 2021 InfiniBand

lore
Share & tools
lore

Jason Gunthorpe

author maintainer
#0 lore raw

PATCH 1/1 IB/qib: Fix null pointer subtraction compiler warning

Quoted 1 line
Quoted 1 line

drivers/infiniband/hw/qib/qib_sysfs.c:411:1: warning: performing pointer subtraction with a null pointer has undefined behavior

+[-Wnull-pointer-subtraction]
QIB_DIAGC_ATTR(rc_resends);
^~~~~~~~~~~~~~~~~~~~~~~~~~
drivers/infiniband/hw/qib/qib_sysfs.c:408:51: note: expanded from macro 'QIB_DIAGC_ATTR'

                   .counter = &((struct qib_ibport *)0)->rvp.n_##N - (u64 *)0,    \

Use offsetof and accomplish the type check using static_assert.

Fixes 4a7aaf88c89f ("RDMA/qib: Use attributes for the port sysfs")
---
 drivers/infiniband/hw/qib/qib_sysfs.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/infiniband/hw/qib/qib_sysfs.c +3 -1
index d57e49de6650be..452e2355d24eeb 100644
@@ -403,9 +403,11 @@ static ssize_t diagc_attr_store(struct ib_device *ibdev, u32 port_num,
 }
 
 #define QIB_DIAGC_ATTR(N)                                                      \
+	static_assert(&((struct qib_ibport *)0)->rvp.n_##N != (u64 *)NULL);    \
 	static struct qib_diagc_attr qib_diagc_attr_##N = {                    \
 		.attr = __ATTR(N, 0664, diagc_attr_show, diagc_attr_store),    \
-		.counter = &((struct qib_ibport *)0)->rvp.n_##N - (u64 *)0,    \
+		.counter =                                                     \
+			offsetof(struct qib_ibport, rvp.n_##N) / sizeof(u64)   \
 	}
 
 QIB_DIAGC_ATTR(rc_resends);

base-commit: 6a217437f9f5482a3f6f2dc5fcd27cf0f62409ac

Signature · 1 line
2.33.0
Acked-by on v1 1/1
#1 lore raw
Quoted 34 lines · Jason Gunthorpe
Quoted 1 line
Quoted 1 line

drivers/infiniband/hw/qib/qib_sysfs.c:411:1: warning: performing pointer subtraction with a null pointer has undefined behavior

+[-Wnull-pointer-subtraction]

   QIB_DIAGC_ATTR(rc_resends);
   ^~~~~~~~~~~~~~~~~~~~~~~~~~
   drivers/infiniband/hw/qib/qib_sysfs.c:408:51: note: expanded from macro 'QIB_DIAGC_ATTR'
                   .counter = &((struct qib_ibport *)0)->rvp.n_##N - (u64 *)0,    \

Use offsetof and accomplish the type check using static_assert.

Fixes 4a7aaf88c89f ("RDMA/qib: Use attributes for the port sysfs")
---
 drivers/infiniband/hw/qib/qib_sysfs.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/infiniband/hw/qib/qib_sysfs.c +3 -1
index d57e49de6650be..452e2355d24eeb 100644
@@ -403,9 +403,11 @@ static ssize_t diagc_attr_store(struct ib_device *ibdev, u32 port_num,
 }
 
 #define QIB_DIAGC_ATTR(N)                                                      \
+	static_assert(&((struct qib_ibport *)0)->rvp.n_##N != (u64 *)NULL);    \
 	static struct qib_diagc_attr qib_diagc_attr_##N = {                    \
 		.attr = __ATTR(N, 0664, diagc_attr_show, diagc_attr_store),    \
-		.counter = &((struct qib_ibport *)0)->rvp.n_##N - (u64 *)0,    \
+		.counter =                                                     \
+			offsetof(struct qib_ibport, rvp.n_##N) / sizeof(u64)   \
 	}
 
 QIB_DIAGC_ATTR(rc_resends);
Quoted 2 lines

base-commit: 6a217437f9f5482a3f6f2dc5fcd27cf0f62409ac

Looks fine.

author maintainer Applied
#2 lore raw
In reply to Jason Gunthorpe
Quoted 9 lines · Jason Gunthorpe
Quoted 1 line
Quoted 1 line

drivers/infiniband/hw/qib/qib_sysfs.c:411:1: warning: performing pointer subtraction with a null pointer has undefined behavior

+[-Wnull-pointer-subtraction]

   QIB_DIAGC_ATTR(rc_resends);
   ^~~~~~~~~~~~~~~~~~~~~~~~~~
   drivers/infiniband/hw/qib/qib_sysfs.c:408:51: note: expanded from macro 'QIB_DIAGC_ATTR'
                   .counter = &((struct qib_ibport *)0)->rvp.n_##N - (u64 *)0,    \

Use offsetof and accomplish the type check using static_assert.

Quoted 7 lines
Fixes 4a7aaf88c89f ("RDMA/qib: Use attributes for the port sysfs")
---
 drivers/infiniband/hw/qib/qib_sysfs.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

Applied to for-rc

Jason