Add definition of nitems for those without it and use it throughout.

This commit is contained in:
Todd C. Miller
2016-06-23 10:58:07 -06:00
parent ff2cd3cf27
commit f683cbd582
9 changed files with 19 additions and 21 deletions

View File

@@ -36,4 +36,4 @@ static struct cclass {
{ NULL, NULL } { NULL, NULL }
}; };
#define NCCLASSES (sizeof(cclasses) / sizeof(cclasses[0]) - 1) #define NCCLASSES (nitems(cclasses) - 1)

View File

@@ -237,6 +237,13 @@ typedef struct sigaction sigaction_t;
# define SA_RESTART 0 # define SA_RESTART 0
#endif #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. * If dirfd() does not exists, hopefully dd_fd does.
*/ */

View File

@@ -90,7 +90,7 @@ aix_setlimits(char *user)
* For each resource limit, get the soft/hard values for the 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. * 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 * We have two strategies, depending on whether or not the
* hard limit has been defined. * hard limit has been defined.

View File

@@ -144,13 +144,6 @@ static const char * const gai_errors[] = {
# define sin_set_length(s) /* empty */ # define sin_set_length(s) /* empty */
#endif #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 * Return a constant string for a given EAI_* error code or a string
* indicating an unknown error. * indicating an unknown error.
@@ -158,7 +151,7 @@ static const char * const gai_errors[] = {
const char * const char *
sudo_gai_strerror(int ecode) 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"; return "Unknown error";
else else
return gai_errors[ecode - 1]; return gai_errors[ecode - 1];

View File

@@ -81,7 +81,7 @@ static const char *const sudo_debug_default_subsystems[] = {
NULL 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 * For multiple programs/plugins there is a per-program instance

View File

@@ -42,14 +42,14 @@ char *insults[] = {
# include "ins_csops.h" # include "ins_csops.h"
# endif # endif
(char *) 0 NULL
}; };
/* /*
* How may I insult you? Let me count the ways... * 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. * return a pseudo-random insult.

View File

@@ -74,7 +74,7 @@ struct base64_test {
int int
main(int argc, char *argv[]) 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; int i, errors = 0;
unsigned char buf[32]; unsigned char buf[32];
size_t len; size_t len;

View File

@@ -175,13 +175,11 @@ main(int argc, char *argv[])
initprogname(argc > 0 ? argv[0] : "check_fill"); 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, txt_data, nitems(txt_data));
errors += do_tests(check_fill_cmnd, cmd_data, sizeof(cmd_data) / sizeof(cmd_data[0])); errors += do_tests(check_fill_cmnd, cmd_data, nitems(cmd_data));
errors += do_tests(check_fill_args, args_data, sizeof(args_data) / sizeof(args_data[0])); errors += do_tests(check_fill_args, args_data, nitems(args_data));
ntests = sizeof(txt_data) / sizeof(txt_data[0]) + ntests = nitems(txt_data) + nitems(cmd_data) + nitems(args_data);
sizeof(cmd_data) / sizeof(cmd_data[0]) +
sizeof(args_data) / sizeof(args_data[0]);
printf("%s: %d tests run, %d errors, %d%% success rate\n", getprogname(), printf("%s: %d tests run, %d errors, %d%% success rate\n", getprogname(),
ntests, errors, (ntests - errors) * 100 / ntests); ntests, errors, (ntests - errors) * 100 / ntests);

View File

@@ -59,7 +59,7 @@ static const char *const sudoers_subsystem_names[] = {
NULL 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. */ /* Subsystem IDs assigned at registration time. */
unsigned int sudoers_subsystem_ids[NUM_SUBSYSTEMS]; unsigned int sudoers_subsystem_ids[NUM_SUBSYSTEMS];