From 3d49904c6e3c36c2ca1ab3071793d9de2403b7ed Mon Sep 17 00:00:00 2001 From: tb Date: Tue, 18 Apr 2023 13:31:14 +0000 Subject: [PATCH] Rewrite some ugly for loops This fixes a few KNF issues and ugly line wrapping by using a local version of nitems(); fix two bsearch() on top. ok claudio --- sbin/pfctl/pfctl_parser.c | 38 +++++++++++++++++--------------------- usr.sbin/bgpd/parse.y | 29 ++++++++++++++--------------- 2 files changed, 31 insertions(+), 36 deletions(-) diff --git a/sbin/pfctl/pfctl_parser.c b/sbin/pfctl/pfctl_parser.c index 07ed75ceaea..226e73f74a9 100644 --- a/sbin/pfctl/pfctl_parser.c +++ b/sbin/pfctl/pfctl_parser.c @@ -1,4 +1,4 @@ -/* $OpenBSD: pfctl_parser.c,v 1.347 2022/11/09 23:00:00 sashan Exp $ */ +/* $OpenBSD: pfctl_parser.c,v 1.348 2023/04/18 13:31:14 tb Exp $ */ /* * Copyright (c) 2001 Daniel Hartmeier @@ -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, struct sockaddr *sa) 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_family_t af) 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 af) 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_int8_t code, sa_family_t af) 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]); diff --git a/usr.sbin/bgpd/parse.y b/usr.sbin/bgpd/parse.y index 5696adca947..34e5105ce4d 100644 --- a/usr.sbin/bgpd/parse.y +++ b/usr.sbin/bgpd/parse.y @@ -1,4 +1,4 @@ -/* $OpenBSD: parse.y,v 1.447 2023/04/18 12:11:27 claudio Exp $ */ +/* $OpenBSD: parse.y,v 1.448 2023/04/18 13:31:14 tb Exp $ */ /* * Copyright (c) 2002, 2003, 2004 Henning Brauer @@ -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); @@ -5527,8 +5530,8 @@ map_tos(char *s, int *val) }; const struct keywords *p; - p = bsearch(s, toswords, sizeof(toswords)/sizeof(toswords[0]), - sizeof(toswords[0]), kw_casecmp); + p = bsearch(s, toswords, nitems(toswords), sizeof(toswords[0]), + kw_casecmp); if (p) { *val = p->k_val; @@ -5951,19 +5954,17 @@ static const struct icmpcodeent icmp6_code[] = { 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); -- 2.20.1