Skip to contentSkip to conversation

net: Fix wrong sizeof

PATCH In review Jean Delvare 7 participants 7 messages Oct 2, 2009 – Oct 26, 2009

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

PATCH 1/1 net: Fix wrong sizeof

Which is why I have always preferred sizeof(struct foo) over sizeof(var).

---
Stable team, the non-documentation part of this fix applies to 2.6.31, 2.6.30 and 2.6.27.

 Documentation/networking/timestamping/timestamping.c |    2 +-
 drivers/net/iseries_veth.c                           |    2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)
Documentation/networking/timestamping/timestamping.c +1 -1
@@ -381,7 +381,7 @@ int main(int argc, char **argv)
 	memset(&hwtstamp, 0, sizeof(hwtstamp));
 	strncpy(hwtstamp.ifr_name, interface, sizeof(hwtstamp.ifr_name));
 	hwtstamp.ifr_data = (void *)&hwconfig;
-	memset(&hwconfig, 0, sizeof(&hwconfig));
+	memset(&hwconfig, 0, sizeof(hwconfig));
 	hwconfig.tx_type =
 		(so_timestamping_flags & SOF_TIMESTAMPING_TX_HARDWARE) ?
 		HWTSTAMP_TX_ON : HWTSTAMP_TX_OFF;
drivers/net/iseries_veth.c +1 -1
@@ -495,7 +495,7 @@ static void veth_take_cap_ack(struct vet
 			   cnx->remote_lp);
 	} else {
 		memcpy(&cnx->cap_ack_event, event,
-		       sizeof(&cnx->cap_ack_event));
+		       sizeof(cnx->cap_ack_event));
 		cnx->state |= VETH_STATE_GOTCAPACK;
 		veth_kick_statemachine(cnx);
 	}
Signature · 1 line
Jean Delvare
Acked-by on v1 1/1
#1 lore raw
Quoted 5 lines · On Fri, 2 Oct 2009 11:30:38 +0200 Jean Delvare wrote:

Which is why I have always preferred sizeof(struct foo) over sizeof(var).

I also prefer to use sizeof(struct xyz) in my non-kernel code instead of sizeof(var).

Quoted 38 lines

---
Stable team, the non-documentation part of this fix applies to 2.6.31, 2.6.30 and 2.6.27.

 Documentation/networking/timestamping/timestamping.c |    2 +-
 drivers/net/iseries_veth.c                           |    2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)
Documentation/networking/timestamping/timestamping.c +1 -1
@@ -381,7 +381,7 @@ int main(int argc, char **argv)
 	memset(&hwtstamp, 0, sizeof(hwtstamp));
 	strncpy(hwtstamp.ifr_name, interface, sizeof(hwtstamp.ifr_name));
 	hwtstamp.ifr_data = (void *)&hwconfig;
-	memset(&hwconfig, 0, sizeof(&hwconfig));
+	memset(&hwconfig, 0, sizeof(hwconfig));
 	hwconfig.tx_type =
 		(so_timestamping_flags & SOF_TIMESTAMPING_TX_HARDWARE) ?
 		HWTSTAMP_TX_ON : HWTSTAMP_TX_OFF;
drivers/net/iseries_veth.c +1 -1
@@ -495,7 +495,7 @@ static void veth_take_cap_ack(struct vet
 			   cnx->remote_lp);
 	} else {
 		memcpy(&cnx->cap_ack_event, event,
-		       sizeof(&cnx->cap_ack_event));
+		       sizeof(cnx->cap_ack_event));
 		cnx->state |= VETH_STATE_GOTCAPACK;
 		veth_kick_statemachine(cnx);
 	}
Signature · 6 lines
-- 
Jean Delvare
--
To unsubscribe from this list: send the line "unsubscribe netdev" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html

---
~Randy

maintainer
#2 lore raw
In reply to Jean Delvare

From: Jean Delvare <[email protected]>
Date: Fri, 2 Oct 2009 11:30:38 +0200

Quoted 5 lines

Which is why I have always preferred sizeof(struct foo) over sizeof(var).

Any time you see "&" in a sizeof() expression, it's almost certainly a bug. Something for the folks with automated tools to look for if they haven't already :-)

I'll apply this, thanks.

#3 lore raw
Quoted 3 lines · David Miller

Any time you see "&" in a sizeof() expression, it's almost certainly a bug. Something for the folks with automated tools to look for if they haven't already :-)

Your remark prompted me to find four more instances of such bugs (none of which in the networking bits). I have submitted patches.

Thank you.

Jan

maintainer
#4 lore raw

PATCH 1/1 scripts/checkpatch.pl: Add WARN on sizeof(&

Quoted 7 lines · Jan Ceuleers
Quoted 3 lines · David Miller

Any time you see "&" in a sizeof() expression, it's almost certainly a bug. Something for the folks with automated tools to look for if they haven't already :-)

Your remark prompted me to find four more instances of such bugs (none of which in the networking bits). I have submitted patches.

Perhaps a checkpatch test.

---

sizeof(&foo) is frequently an error. Warn on its use.

scripts/checkpatch.pl +5 -0
index 87bbb8b..596dd72 100755
@@ -2534,6 +2534,11 @@ sub process {
 			WARN("plain inline is preferred over $1\n" . $herecurr);
 		}
 
+# check for sizeof(&)
+		if ($line =~ /\bsizeof\s*\(\s*\&/) {
+			WARN("sizeof(& should be avoided\n" . $herecurr);
+		}
+
 # check for new externs in .c files.
 		if ($realfile =~ /\.c$/ && defined $stat &&
 		    $stat =~ /^.\s*(?:extern\s+)?$Type\s+($Ident)(\s*)\(/s)
Acked-by on v1 1/1
#5 lore raw

Re: [PATCH] scripts/checkpatch.pl: Add WARN on sizeof(&

Quoted 10 lines · Joe Perches
Quoted 7 lines · Jan Ceuleers
Quoted 3 lines · David Miller

Any time you see "&" in a sizeof() expression, it's almost certainly a bug.  Something for the folks with automated tools to look for if they haven't already :-)

Your remark prompted me to find four more instances of such bugs (none of which in the networking bits). I have submitted patches.

Perhaps a checkpatch test.

A quick grep shows we have more:

grep -e '\bsizeof[:space:]*([:space:]*\&.*)' -nr ./ ./drivers/media/video/em28xx/em28xx-cards.c:2237:
memset(&dev->info, 0, sizeof(&dev->info)); ./drivers/media/video/saa7164/saa7164-cmd.c:350:
memset(&response_t, 0, sizeof(&response_t));
./drivers/media/dvb/dvb-usb/ce6230.c:108: memset(&req, 0, sizeof(&req));
./drivers/video/backlight/lcd.c:59: memset(&ld->fb_notif, 0,
sizeof(&ld->fb_notif));

Quoted 6 lines

---

sizeof(&foo) is frequently an error.  Warn on its use.

This patch looks fine for me,

Quoted 19 lines
scripts/checkpatch.pl +5 -0
index 87bbb8b..596dd72 100755
@@ -2534,6 +2534,11 @@ sub process {
                        WARN("plain inline is preferred over $1\n" . $herecurr);
                }
 
+# check for sizeof(&)
+               if ($line =~ /\bsizeof\s*\(\s*\&/) {
+                       WARN("sizeof(& should be avoided\n" . $herecurr);
+               }
+
  # check for new externs in .c files.

               if ($realfile =~ /\.c$/ && defined $stat && $stat =~ /^.\s*(?:extern\s+)?$Type\s+($Ident)(\s*)\(/s)

maintainer
#6 lore raw

Re: [PATCH] scripts/checkpatch.pl: Add WARN on sizeof(&

In reply to Joe Perches
Quoted 9 lines · Joe Perches
Quoted 7 lines · Jan Ceuleers
Quoted 3 lines · David Miller

Any time you see "&" in a sizeof() expression, it's almost certainly a bug. Something for the folks with automated tools to look for if they haven't already :-)

Your remark prompted me to find four more instances of such bugs (none of which in the networking bits). I have submitted patches.

Quoted 7 lines

Perhaps a checkpatch test.

---

sizeof(&foo) is frequently an error. Warn on its use.

Seems reasonable. A slightly modified version of this is now in my tree. Thanks!

http://www.kernel.org/pub/linux/kernel/people/apw/checkpatch/checkpatch.pl-testing

-apw