On Tue, Apr 18, 2023 at 02:06:46PM +0200, Claudio Jeker wrote:
> This and the others are IIRC streight from pfctl. So if someone wants a
> free commit :) 

How about this. pfctl and bgpd are the same, except that the bgpd one
has a bsearch() nitems on top.  pfctl regress is happy.

Index: sbin/pfctl/pfctl_parser.c
===================================================================
RCS file: /cvs/src/sbin/pfctl/pfctl_parser.c,v
retrieving revision 1.347
diff -u -p -r1.347 pfctl_parser.c
--- sbin/pfctl/pfctl_parser.c   9 Nov 2022 23:00:00 -0000       1.347
+++ sbin/pfctl/pfctl_parser.c   18 Apr 2023 12:37:19 -0000
@@ -62,6 +62,10 @@
 #include "pfctl_parser.h"
 #include "pfctl.h"
 
+#ifndef nitems
+#define nitems(_a)     (sizeof((_a)) / sizeof((_a)[0]))
+#endif
+
 void            print_op (u_int8_t, const char *, const char *);
 void            print_port (u_int8_t, u_int16_t, u_int16_t, const char *, int);
 void            print_ugid (u_int8_t, id_t, id_t, const char *);
@@ -224,17 +228,15 @@ copy_satopfaddr(struct pf_addr *pfa, str
 const struct icmptypeent *
 geticmptypebynumber(u_int8_t type, sa_family_t af)
 {
-       unsigned int    i;
+       size_t  i;
 
        if (af != AF_INET6) {
-               for (i=0; i < (sizeof (icmp_type) / sizeof(icmp_type[0]));
-                   i++) {
+               for (i = 0; i < nitems(icmp_type); i++) {
                        if (type == icmp_type[i].type)
                                return (&icmp_type[i]);
                }
        } else {
-               for (i=0; i < (sizeof (icmp6_type) /
-                   sizeof(icmp6_type[0])); i++) {
+               for (i = 0; i < nitems(icmp6_type); i++) {
                        if (type == icmp6_type[i].type)
                                 return (&icmp6_type[i]);
                }
@@ -245,17 +247,15 @@ geticmptypebynumber(u_int8_t type, sa_fa
 const struct icmptypeent *
 geticmptypebyname(char *w, sa_family_t af)
 {
-       unsigned int    i;
+       size_t  i;
 
        if (af != AF_INET6) {
-               for (i=0; i < (sizeof (icmp_type) / sizeof(icmp_type[0]));
-                   i++) {
+               for (i = 0; i < nitems(icmp_type); i++) {
                        if (!strcmp(w, icmp_type[i].name))
                                return (&icmp_type[i]);
                }
        } else {
-               for (i=0; i < (sizeof (icmp6_type) /
-                   sizeof(icmp6_type[0])); i++) {
+               for (i = 0; i < nitems(icmp6_type); i++) {
                        if (!strcmp(w, icmp6_type[i].name))
                                return (&icmp6_type[i]);
                }
@@ -266,18 +266,16 @@ geticmptypebyname(char *w, sa_family_t a
 const struct icmpcodeent *
 geticmpcodebynumber(u_int8_t type, u_int8_t code, sa_family_t af)
 {
-       unsigned int    i;
+       size_t  i;
 
        if (af != AF_INET6) {
-               for (i=0; i < (sizeof (icmp_code) / sizeof(icmp_code[0]));
-                   i++) {
+               for (i = 0; i < nitems(icmp_code); i++) {
                        if (type == icmp_code[i].type &&
                            code == icmp_code[i].code)
                                return (&icmp_code[i]);
                }
        } else {
-               for (i=0; i < (sizeof (icmp6_code) /
-                   sizeof(icmp6_code[0])); i++) {
+               for (i = 0; i < nitems(icmp6_code); i++) {
                        if (type == icmp6_code[i].type &&
                            code == icmp6_code[i].code)
                                return (&icmp6_code[i]);
@@ -289,18 +287,16 @@ geticmpcodebynumber(u_int8_t type, u_int
 const struct icmpcodeent *
 geticmpcodebyname(u_long type, char *w, sa_family_t af)
 {
-       unsigned int    i;
+       size_t  i;
 
        if (af != AF_INET6) {
-               for (i=0; i < (sizeof (icmp_code) / sizeof(icmp_code[0]));
-                   i++) {
+               for (i = 0; i < nitems(icmp_code); i++) {
                        if (type == icmp_code[i].type &&
                            !strcmp(w, icmp_code[i].name))
                                return (&icmp_code[i]);
                }
        } else {
-               for (i=0; i < (sizeof (icmp6_code) /
-                   sizeof(icmp6_code[0])); i++) {
+               for (i = 0; i < nitems(icmp6_code); i++) {
                        if (type == icmp6_code[i].type &&
                            !strcmp(w, icmp6_code[i].name))
                                return (&icmp6_code[i]);
Index: usr.sbin/bgpd/parse.y
===================================================================
RCS file: /cvs/src/usr.sbin/bgpd/parse.y,v
retrieving revision 1.447
diff -u -p -r1.447 parse.y
--- usr.sbin/bgpd/parse.y       18 Apr 2023 12:11:27 -0000      1.447
+++ usr.sbin/bgpd/parse.y       18 Apr 2023 12:37:19 -0000
@@ -52,6 +52,10 @@
 #include "rde.h"
 #include "log.h"
 
+#ifndef nitems
+#define nitems(_a)     (sizeof((_a)) / sizeof((_a)[0]))
+#endif
+
 #define MACRO_NAME_LEN         128
 
 TAILQ_HEAD(files, file)                 files = TAILQ_HEAD_INITIALIZER(files);
@@ -3630,8 +3634,7 @@ lookup(char *s)
        };
        const struct keywords   *p;
 
-       p = bsearch(s, keywords, sizeof(keywords)/sizeof(keywords[0]),
-           sizeof(keywords[0]), kw_cmp);
+       p = bsearch(s, keywords, nitems(keywords), sizeof(keywords[0]), kw_cmp);
 
        if (p)
                return (p->k_val);
@@ -5951,19 +5954,17 @@ static const struct icmpcodeent icmp6_co
 static int
 geticmptypebyname(char *w, uint8_t aid)
 {
-       unsigned int    i;
+       size_t  i;
 
        switch (aid) {
        case AID_INET:
-               for (i = 0; i < (sizeof(icmp_type) / sizeof(icmp_type[0]));
-                   i++) {
+               for (i = 0; i < nitems(icmp_type); i++) {
                        if (!strcmp(w, icmp_type[i].name))
                                return (icmp_type[i].type);
                }
                break;
        case AID_INET6:
-               for (i = 0; i < (sizeof(icmp6_type) / sizeof(icmp6_type[0]));
-                   i++) {
+               for (i = 0; i < nitems(icmp6_type); i++) {
                        if (!strcmp(w, icmp6_type[i].name))
                                return (icmp6_type[i].type);
                }
@@ -5975,20 +5976,18 @@ geticmptypebyname(char *w, uint8_t aid)
 static int
 geticmpcodebyname(u_long type, char *w, uint8_t aid)
 {
-       unsigned int    i;
+       size_t  i;
 
        switch (aid) {
        case AID_INET:
-               for (i = 0; i < (sizeof(icmp_code) / sizeof(icmp_code[0]));
-                   i++) {
+               for (i = 0; i < nitems(icmp_code); i++) {
                        if (type == icmp_code[i].type &&
                            !strcmp(w, icmp_code[i].name))
                                return (icmp_code[i].code);
                }
                break;
        case AID_INET6:
-               for (i = 0; i < (sizeof(icmp6_code) / sizeof(icmp6_code[0]));
-                   i++) {
+               for (i = 0; i < nitems(icmp6_code); i++) {
                        if (type == icmp6_code[i].type &&
                            !strcmp(w, icmp6_code[i].name))
                                return (icmp6_code[i].code);

Reply via email to