From: David Gwynne Subject: Re: bgpd: move nitems() from parse.y to bgpd.h To: OpenBSD Tech Date: Thu, 30 Oct 2025 21:08:03 +1000 Ok On Thu, 30 Oct 2025, 20:46 Claudio Jeker, wrote: > Currently parse.y defines nitems() but much more code in bgpd could use > this define. So move it to bgpd.h. > > -- > :wq Claudio > > Index: bgpctl/output_ometric.c > =================================================================== > RCS file: /cvs/src/usr.sbin/bgpctl/output_ometric.c,v > diff -u -p -r1.15 output_ometric.c > --- bgpctl/output_ometric.c 29 Oct 2025 21:39:19 -0000 1.15 > +++ bgpctl/output_ometric.c 30 Oct 2025 10:42:04 -0000 > @@ -87,9 +87,8 @@ ometric_head(struct parse_result *arg) > */ > peer_info = ometric_new(OMT_INFO, "bgpd_peer", > "peer information"); > - peer_state = ometric_new_state(statenames, > - sizeof(statenames) / sizeof(statenames[0]), "bgpd_peer_state", > - "peer session state"); > + peer_state = ometric_new_state(statenames, nitems(statenames), > + "bgpd_peer_state", "peer session state"); > peer_state_raw = ometric_new(OMT_GAUGE, "bgpd_peer_state_raw", > "peer session state raw int value"); > peer_last_change = ometric_new(OMT_GAUGE, > Index: bgpd/bgpd.h > =================================================================== > RCS file: /cvs/src/usr.sbin/bgpd/bgpd.h,v > diff -u -p -r1.519 bgpd.h > --- bgpd/bgpd.h 21 Mar 2025 01:06:48 -0000 1.519 > +++ bgpd/bgpd.h 30 Oct 2025 10:39:57 -0000 > @@ -107,6 +107,10 @@ > #define CTASSERT(x) extern char _ctassert[(x) ? 1 : -1 ] \ > __attribute__((__unused__)) > > +#ifndef nitems > +#define nitems(_a) (sizeof((_a)) / sizeof((_a)[0])) > +#endif > + > /* > * Note that these numeric assignments differ from the numbers commonly > * used in route origin validation context. > Index: bgpd/logmsg.c > =================================================================== > RCS file: /cvs/src/usr.sbin/bgpd/logmsg.c,v > diff -u -p -r1.16 logmsg.c > --- bgpd/logmsg.c 9 Sep 2025 12:42:04 -0000 1.16 > +++ bgpd/logmsg.c 30 Oct 2025 10:39:57 -0000 > @@ -166,14 +166,14 @@ log_notification(const struct peer *peer > p = log_fmt_peer(&peer->conf); > switch (errcode) { > case ERR_HEADER: > - if (subcode >= sizeof(suberr_header_names) / sizeof(char > *) || > + if (subcode >= nitems(suberr_header_names) || > suberr_header_names[subcode] == NULL) > uk = 1; > else > suberrname = suberr_header_names[subcode]; > break; > case ERR_OPEN: > - if (subcode >= sizeof(suberr_open_names) / sizeof(char *) > || > + if (subcode >= nitems(suberr_open_names) || > suberr_open_names[subcode] == NULL) > uk = 1; > else > @@ -192,7 +192,7 @@ log_notification(const struct peer *peer > } > break; > case ERR_UPDATE: > - if (subcode >= sizeof(suberr_update_names) / sizeof(char > *) || > + if (subcode >= nitems(suberr_update_names) || > suberr_update_names[subcode] == NULL) > uk = 1; > else > @@ -200,7 +200,7 @@ log_notification(const struct peer *peer > dump = 1; > break; > case ERR_CEASE: > - if (subcode >= sizeof(suberr_cease_names) / sizeof(char *) > || > + if (subcode >= nitems(suberr_cease_names) || > suberr_cease_names[subcode] == NULL) > uk = 1; > else > @@ -229,14 +229,14 @@ log_notification(const struct peer *peer > uk = 1; > break; > case ERR_FSM: > - if (subcode >= sizeof(suberr_fsm_names) / sizeof(char *) || > + if (subcode >= nitems(suberr_fsm_names) || > suberr_fsm_names[subcode] == NULL) > uk = 1; > else > suberrname = suberr_fsm_names[subcode]; > break; > case ERR_RREFRESH: > - if (subcode >= sizeof(suberr_rrefresh_names) / sizeof(char > *) || > + if (subcode >= nitems(suberr_rrefresh_names) || > suberr_rrefresh_names[subcode] == NULL) > uk = 1; > else > Index: bgpd/parse.y > =================================================================== > RCS file: /cvs/src/usr.sbin/bgpd/parse.y,v > diff -u -p -r1.482 parse.y > --- bgpd/parse.y 27 Feb 2025 14:15:35 -0000 1.482 > +++ bgpd/parse.y 30 Oct 2025 10:39:57 -0000 > @@ -52,10 +52,6 @@ > #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); > Index: bgpd/session.c > =================================================================== > RCS file: /cvs/src/usr.sbin/bgpd/session.c,v > diff -u -p -r1.526 session.c > --- bgpd/session.c 21 Aug 2025 15:15:25 -0000 1.526 > +++ bgpd/session.c 30 Oct 2025 10:39:57 -0000 > @@ -1876,7 +1876,7 @@ session_stop(struct peer *peer, uint8_t > break; > default: > /* session not open, no need to send notification */ > - if (subcode >= sizeof(suberr_cease_names) / sizeof(char *) > || > + if (subcode >= nitems(suberr_cease_names) || > suberr_cease_names[subcode] == NULL) > log_peer_warnx(&peer->conf, "session stop: %s, " > "unknown subcode %u", errnames[ERR_CEASE], > subcode); > >