diff mbox

[iproute,v3] Use ARRAY_SIZE macro everywhere

Message ID 1458661973-31210-1-git-send-email-phil@nwl.cc
State Superseded, archived
Delegated to: stephen hemminger
Headers show

Commit Message

Phil Sutter March 22, 2016, 3:52 p.m. UTC
This patch was generated by the following semantic patch (a trimmed down
version of what is shipped with Linux sources):

@@
type T;
T[] E;
@@
(
- (sizeof(E)/sizeof(*E))
+ ARRAY_SIZE(E)
|
- (sizeof(E)/sizeof(E[...]))
+ ARRAY_SIZE(E)
|
- (sizeof(E)/sizeof(T))
+ ARRAY_SIZE(E)
)

The only manual adjustment was to include utils.h in misc/nstat.c to make
the macro known there.

Signed-off-by: Phil Sutter <phil@nwl.cc>
---
Changes since v1:
- Rebased onto current master to avoid merge conflicts.

Changes since v2:
- Patch recreated from scratch.
---
 bridge/link.c | 2 +-
 misc/nstat.c  | 2 +-
 misc/ss.c     | 2 +-
 3 files changed, 3 insertions(+), 3 deletions(-)

Comments

stephen hemminger March 27, 2016, 5:42 p.m. UTC | #1
On Tue, 22 Mar 2016 16:52:53 +0100
Phil Sutter <phil@nwl.cc> wrote:

> This patch was generated by the following semantic patch (a trimmed down
> version of what is shipped with Linux sources):
> 
> @@
> type T;
> T[] E;
> @@
> (
> - (sizeof(E)/sizeof(*E))
> + ARRAY_SIZE(E)
> |
> - (sizeof(E)/sizeof(E[...]))
> + ARRAY_SIZE(E)
> |
> - (sizeof(E)/sizeof(T))
> + ARRAY_SIZE(E)
> )
> 
> The only manual adjustment was to include utils.h in misc/nstat.c to make
> the macro known there.
> 
> Signed-off-by: Phil Sutter <phil@nwl.cc>
> ---
> Changes since v1:
> - Rebased onto current master to avoid merge conflicts.
> 
> Changes since v2:
> - Patch recreated from scratch.
> ---
>  bridge/link.c | 2 +-
>  misc/nstat.c  | 2 +-
>  misc/ss.c     | 2 +-
>  3 files changed, 3 insertions(+), 3 deletions(-)

You need to test build after coccinelle!

nstat.c: In function ‘useless_number’:
nstat.c:98:2: warning: implicit declaration of function ‘ARRAY_SIZE’ [-Wimplicit-function-declaration]
  for (i = 0; i < ARRAY_SIZE(useless_numbers); i++)
  ^
gcc -Wall -Wstrict-prototypes  -Wmissing-prototypes -Wmissing-declarations -Wold-style-definition -Wformat=2 -O2 -I../include -DRESOLVE_HOSTNAMES -DLIBDIR=\"/usr/lib\" -DCONFDIR=\"/etc/iproute2\" -D_GNU_SOURCE -D_FILE_OFFSET_BITS=64 -D_LARGEFILE_SOURCE -D_LARGEFILE64_SOURCE   -DHAVE_SELINUX -DHAVE_SETNS   -c -o ssfilter.o ssfilter.c
gcc   lnstat.o lnstat_util.o  ../lib/libnetlink.a ../lib/libutil.a -lselinux  -o lnstat
/tmp/ccoYmLJI.o: In function `useless_number':
nstat.c:(.text+0x40): undefined reference to `ARRAY_SIZE'

Please fix and resubmit
stephen hemminger June 29, 2016, 4:18 p.m. UTC | #2
On Tue, 28 Jun 2016 16:42:15 +0000
Phil Sutter <phil@nwl.cc> wrote:

> This patch was generated by the following semantic patch (a trimmed down
> version of what is shipped with Linux sources):
> 
> @@
> type T;
> T[] E;
> @@
> (
> - (sizeof(E)/sizeof(*E))
> + ARRAY_SIZE(E)
> |
> - (sizeof(E)/sizeof(E[...]))
> + ARRAY_SIZE(E)
> |
> - (sizeof(E)/sizeof(T))
> + ARRAY_SIZE(E)
> )
> 
> The only manual adjustment was to include utils.h in misc/nstat.c to make
> the macro known there.
> 
> Signed-off-by: Phil Sutter <phil@nwl.cc>

Applied
diff mbox

Patch

diff --git a/bridge/link.c b/bridge/link.c
index 353e1c3da45db..b347040ccf91d 100644
--- a/bridge/link.c
+++ b/bridge/link.c
@@ -319,7 +319,7 @@  static int brlink_modify(int argc, char **argv)
 		} else if (strcmp(*argv, "state") == 0) {
 			NEXT_ARG();
 			char *endptr;
-			size_t nstates = sizeof(port_states) / sizeof(*port_states);
+			size_t nstates = ARRAY_SIZE(port_states);
 
 			state = strtol(*argv, &endptr, 10);
 			if (!(**argv != '\0' && *endptr == '\0')) {
diff --git a/misc/nstat.c b/misc/nstat.c
index a9e0f20789e3c..4f3863ff99121 100644
--- a/misc/nstat.c
+++ b/misc/nstat.c
@@ -95,7 +95,7 @@  static int useless_number(const char *id)
 {
 	int i;
 
-	for (i = 0; i < sizeof(useless_numbers)/sizeof(*useless_numbers); i++)
+	for (i = 0; i < ARRAY_SIZE(useless_numbers); i++)
 		if (strcmp(id, useless_numbers[i]) == 0)
 			return 1;
 	return 0;
diff --git a/misc/ss.c b/misc/ss.c
index 192389cc82371..449c391579af1 100644
--- a/misc/ss.c
+++ b/misc/ss.c
@@ -666,7 +666,7 @@  static int get_slabstat(struct slabstat *s)
 	while (fgets(buf, sizeof(buf), fp) != NULL) {
 		int i;
 
-		for (i = 0; i < sizeof(slabstat_ids)/sizeof(slabstat_ids[0]); i++) {
+		for (i = 0; i < ARRAY_SIZE(slabstat_ids); i++) {
 			if (memcmp(buf, slabstat_ids[i], strlen(slabstat_ids[i])) == 0) {
 				sscanf(buf, "%*s%d", ((int *)s) + i);
 				cnt--;