From f683cbd582df4ed15a4d10fa56218e480be90016 Mon Sep 17 00:00:00 2001 From: "Todd C. Miller" Date: Thu, 23 Jun 2016 10:58:07 -0600 Subject: [PATCH] Add definition of nitems for those without it and use it throughout. --- include/compat/charclass.h | 2 +- include/sudo_compat.h | 7 +++++++ lib/util/aix.c | 2 +- lib/util/getaddrinfo.c | 9 +-------- lib/util/sudo_debug.c | 2 +- plugins/sudoers/insults.h | 4 ++-- plugins/sudoers/regress/parser/check_base64.c | 2 +- plugins/sudoers/regress/parser/check_fill.c | 10 ++++------ plugins/sudoers/sudoers_debug.c | 2 +- 9 files changed, 19 insertions(+), 21 deletions(-) diff --git a/include/compat/charclass.h b/include/compat/charclass.h index 4fcf15eac..00e16fce2 100644 --- a/include/compat/charclass.h +++ b/include/compat/charclass.h @@ -36,4 +36,4 @@ static struct cclass { { NULL, NULL } }; -#define NCCLASSES (sizeof(cclasses) / sizeof(cclasses[0]) - 1) +#define NCCLASSES (nitems(cclasses) - 1) diff --git a/include/sudo_compat.h b/include/sudo_compat.h index 9ed797c31..ee65655a6 100644 --- a/include/sudo_compat.h +++ b/include/sudo_compat.h @@ -237,6 +237,13 @@ typedef struct sigaction sigaction_t; # define SA_RESTART 0 #endif +/* + * The nitems macro may be defined in sys/param.h + */ +#ifndef nitems +# define nitems(_a) (sizeof((_a)) / sizeof((_a)[0])) +#endif + /* * If dirfd() does not exists, hopefully dd_fd does. */ diff --git a/lib/util/aix.c b/lib/util/aix.c index ef9d041cf..c477cf8ce 100644 --- a/lib/util/aix.c +++ b/lib/util/aix.c @@ -90,7 +90,7 @@ aix_setlimits(char *user) * For each resource limit, get the soft/hard values for the user * and set those values via setrlimit64(). Must be run as euid 0. */ - for (n = 0; n < sizeof(aix_limits) / sizeof(aix_limits[0]); n++) { + for (n = 0; n < nitems(aix_limits); n++) { /* * We have two strategies, depending on whether or not the * hard limit has been defined. diff --git a/lib/util/getaddrinfo.c b/lib/util/getaddrinfo.c index b63640534..046e4db94 100644 --- a/lib/util/getaddrinfo.c +++ b/lib/util/getaddrinfo.c @@ -144,13 +144,6 @@ static const char * const gai_errors[] = { # define sin_set_length(s) /* empty */ #endif -/* - * Used for iterating through arrays. ARRAY_SIZE returns the number of - * elements in the array (useful for a < upper bound in a for loop). - */ -#define ARRAY_SIZE(array) (sizeof(array) / sizeof((array)[0])) - - /* * Return a constant string for a given EAI_* error code or a string * indicating an unknown error. @@ -158,7 +151,7 @@ static const char * const gai_errors[] = { const char * sudo_gai_strerror(int ecode) { - if (ecode < 1 || (size_t) ecode > ARRAY_SIZE(gai_errors)) + if (ecode < 1 || (size_t) ecode > nitems(gai_errors)) return "Unknown error"; else return gai_errors[ecode - 1]; diff --git a/lib/util/sudo_debug.c b/lib/util/sudo_debug.c index 3b81c56a5..6e04278b9 100644 --- a/lib/util/sudo_debug.c +++ b/lib/util/sudo_debug.c @@ -81,7 +81,7 @@ static const char *const sudo_debug_default_subsystems[] = { NULL }; -#define NUM_DEF_SUBSYSTEMS (sizeof(sudo_debug_default_subsystems) / sizeof(sudo_debug_default_subsystems[0]) - 1) +#define NUM_DEF_SUBSYSTEMS (nitems(sudo_debug_default_subsystems) - 1) /* * For multiple programs/plugins there is a per-program instance diff --git a/plugins/sudoers/insults.h b/plugins/sudoers/insults.h index c54b3e409..31f1a4eb2 100644 --- a/plugins/sudoers/insults.h +++ b/plugins/sudoers/insults.h @@ -42,14 +42,14 @@ char *insults[] = { # include "ins_csops.h" # endif - (char *) 0 + NULL }; /* * How may I insult you? Let me count the ways... */ -#define NOFINSULTS (sizeof(insults) / sizeof(insults[0]) - 1) +#define NOFINSULTS (nitems(insults) - 1) /* * return a pseudo-random insult. diff --git a/plugins/sudoers/regress/parser/check_base64.c b/plugins/sudoers/regress/parser/check_base64.c index 7c9afa6fe..7af85bb97 100644 --- a/plugins/sudoers/regress/parser/check_base64.c +++ b/plugins/sudoers/regress/parser/check_base64.c @@ -74,7 +74,7 @@ struct base64_test { int main(int argc, char *argv[]) { - const int ntests = (sizeof(test_strings) / sizeof(test_strings[0])); + const int ntests = nitems(test_strings); int i, errors = 0; unsigned char buf[32]; size_t len; diff --git a/plugins/sudoers/regress/parser/check_fill.c b/plugins/sudoers/regress/parser/check_fill.c index 8d59e5538..95fdcae2a 100644 --- a/plugins/sudoers/regress/parser/check_fill.c +++ b/plugins/sudoers/regress/parser/check_fill.c @@ -175,13 +175,11 @@ main(int argc, char *argv[]) initprogname(argc > 0 ? argv[0] : "check_fill"); - errors += do_tests(check_fill, txt_data, sizeof(txt_data) / sizeof(txt_data[0])); - errors += do_tests(check_fill_cmnd, cmd_data, sizeof(cmd_data) / sizeof(cmd_data[0])); - errors += do_tests(check_fill_args, args_data, sizeof(args_data) / sizeof(args_data[0])); + errors += do_tests(check_fill, txt_data, nitems(txt_data)); + errors += do_tests(check_fill_cmnd, cmd_data, nitems(cmd_data)); + errors += do_tests(check_fill_args, args_data, nitems(args_data)); - ntests = sizeof(txt_data) / sizeof(txt_data[0]) + - sizeof(cmd_data) / sizeof(cmd_data[0]) + - sizeof(args_data) / sizeof(args_data[0]); + ntests = nitems(txt_data) + nitems(cmd_data) + nitems(args_data); printf("%s: %d tests run, %d errors, %d%% success rate\n", getprogname(), ntests, errors, (ntests - errors) * 100 / ntests); diff --git a/plugins/sudoers/sudoers_debug.c b/plugins/sudoers/sudoers_debug.c index 9dc8727fd..3fbae4710 100644 --- a/plugins/sudoers/sudoers_debug.c +++ b/plugins/sudoers/sudoers_debug.c @@ -59,7 +59,7 @@ static const char *const sudoers_subsystem_names[] = { NULL }; -#define NUM_SUBSYSTEMS (sizeof(sudoers_subsystem_names) / sizeof(sudoers_subsystem_names[0]) - 1) +#define NUM_SUBSYSTEMS (nitems(sudoers_subsystem_names) - 1) /* Subsystem IDs assigned at registration time. */ unsigned int sudoers_subsystem_ids[NUM_SUBSYSTEMS]; -- 2.40.0