Skip to contentSkip to conversation

char: hide sparse expensive pointer subtraction warning in specialix.c

PATCH In review Harvey Harrison 2 participants 3 messages Feb 22, 2008 Char and misc drivers

lore
Share & tools
lore
#0 lore raw

PATCH 1/2 char: hide sparse expensive pointer subtraction warning in specialix.c

The warnings come from two static inlines so the ugliness in hiding them is well contained.
drivers/char/specialix.c:238:19: warning: potentially expensive pointer subtraction drivers/char/specialix.c:245:19: warning: potentially expensive pointer subtraction

---
Andrew, take it or leave it, I just did this to make it easier to look at the other sparse issues....but without this sparse produces > 100 of these warnings for this file.

 drivers/char/specialix.c |    6 +++---
 1 files changed, 3 insertions(+), 3 deletions(-)
drivers/char/specialix.c +3 -3
index c0e08c7..5487f37 100644
@@ -228,21 +228,21 @@ static inline int sx_paranoia_check(struct specialix_port const * port,
 /* Get board number from pointer */
 static inline int board_No (struct specialix_board * bp)
 {
-	return bp - sx_board;
+	return ((char *)bp - (char *)sx_board) * sizeof(*bp);
 }
 
 
 /* Get port number from pointer */
 static inline int port_No (struct specialix_port const * port)
 {
-	return SX_PORT(port - sx_port);
+	return SX_PORT(((char *)port - (char *)sx_port) * sizeof(*port));
 }
 
 
 /* Get pointer to board from pointer to port */
 static inline struct specialix_board * port_Board(struct specialix_port const * port)
 {
-	return &sx_board[SX_BOARD(port - sx_port)];
+	return &sx_board[SX_BOARD(port_No(port))];
 }
 
 
Signature · 1 line
1.5.4.2.200.g99e75
#1 lore raw

PATCH 1/2 char: hide sparse expensive pointer subtraction warning in specialix.c

The warnings come from two static inlines so the ugliness in hiding them is well contained.
drivers/char/specialix.c:238:19: warning: potentially expensive pointer subtraction drivers/char/specialix.c:245:19: warning: potentially expensive pointer subtraction

---
Bernd noticed that I'm an idiot. I was only hiding them to see what else was in the file. This patch is at least functionally correct.

Brown-paper-bagged-by: Harvey Harrison

 drivers/char/specialix.c |    6 +++---
 1 files changed, 3 insertions(+), 3 deletions(-)
drivers/char/specialix.c +3 -3
index c0e08c7..34c7de6 100644
@@ -228,21 +228,21 @@ static inline int sx_paranoia_check(struct specialix_port const * port,
 /* Get board number from pointer */
 static inline int board_No (struct specialix_board * bp)
 {
-	return bp - sx_board;
+	return ((char *)bp - (char *)sx_board) / sizeof(*bp);
 }
 
 
 /* Get port number from pointer */
 static inline int port_No (struct specialix_port const * port)
 {
-	return SX_PORT(port - sx_port);
+	return SX_PORT(((char *)port - (char *)sx_port) / sizeof(*port));
 }
 
 
 /* Get pointer to board from pointer to port */
 static inline struct specialix_board * port_Board(struct specialix_port const * port)
 {
-	return &sx_board[SX_BOARD(port - sx_port)];
+	return &sx_board[SX_BOARD(port_No(port))];
 }
 
 
Signature · 1 line
1.5.4.2.200.g99e75
#2 lore raw
In reply to Harvey Harrison
Quoted 12 lines · Harvey Harrison

Andrew, take it or leave it, I just did this to make it easier to look at the other sparse issues....but without this sparse produces > 100 of these warnings for this file.

 drivers/char/specialix.c |    6 +++---
 1 files changed, 3 insertions(+), 3 deletions(-)
drivers/char/specialix.c +0 -0
index c0e08c7..5487f37 100644

drivers/char/specialix.c static inline int sx_paranoia_check(struct specialix_port const * port, · line 232

Quoted 6 lines
drivers/char/specialix.c +1 -1
@@ -228,21 +228,21 @@ static inline int sx_paranoia_check(struct specialix_port const * port,
 /* Get board number from pointer */
 static inline int board_No (struct specialix_board * bp)
 {
-	return bp - sx_board;
+	return ((char *)bp - (char *)sx_board) * sizeof(*bp);
 }

You've casted addresses to char then multiplied. You need to divide.

Maybe it'd be better to change your sparse settings to not print this warning rather than change normally readable code to avoid the warning.

I like the control name "ptr-subtraction-blows".

$ make C=1 CHECKFLAGS="-Wno-ptr-subtraction-blows" drivers/char/specialix.o
  CHK     include/linux/version.h
  CHK     include/linux/utsrelease.h
  CALL    scripts/checksyscalls.sh
  CHECK   drivers/char/specialix.c
drivers/char/specialix.c:2112:3: warning: context imbalance in 'sx_throttle' - unexpected unlock
  CC      drivers/char/specialix.o
drivers/char/specialix.c:2488: warning: ‘specialx_pci_tbl’ defined but not used