From 1181e0590c9f059c40b71718d4fb3b6c339f65db Mon Sep 17 00:00:00 2001 From: "Dmitry V. Levin" Date: Mon, 16 Mar 2020 21:02:18 +0000 Subject: Use PAM_ARRAY_SIZE Replace all instances of sizeof(x) / sizeof(*x) with PAM_ARRAY_SIZE(x) which is less error-prone and implements an additional type check. * libpam/pam_handlers.c: Include "pam_inline.h". (_pam_open_config_file): Use PAM_ARRAY_SIZE. * modules/pam_exec/pam_exec.c: Include "pam_inline.h". (call_exec): Use PAM_ARRAY_SIZE. * modules/pam_namespace/pam_namespace.c: Include "pam_inline.h". (filter_mntopts): Use PAM_ARRAY_SIZE. * modules/pam_timestamp/hmacfile.c: Include "pam_inline.h". (testvectors): Use PAM_ARRAY_SIZE. * modules/pam_xauth/pam_xauth.c: Include "pam_inline.h". (run_coprocess, pam_sm_open_session): Use PAM_ARRAY_SIZE. * tests/tst-pam_get_item.c: Include "pam_inline.h". (main): Use PAM_ARRAY_SIZE. * tests/tst-pam_set_item.c: Likewise. * xtests/tst-pam_pwhistory1.c: Likewise. * xtests/tst-pam_time1.c: Likewise. --- libpam/pam_handlers.c | 3 ++- modules/pam_exec/pam_exec.c | 3 ++- modules/pam_namespace/pam_namespace.c | 5 +++-- modules/pam_timestamp/hmacfile.c | 4 +++- modules/pam_xauth/pam_xauth.c | 5 +++-- tests/tst-pam_get_item.c | 10 +++++----- tests/tst-pam_set_item.c | 8 ++++---- xtests/tst-pam_pwhistory1.c | 12 ++++++------ xtests/tst-pam_time1.c | 7 +++---- 9 files changed, 31 insertions(+), 26 deletions(-) diff --git a/libpam/pam_handlers.c b/libpam/pam_handlers.c index 5dff58c2..9c733681 100644 --- a/libpam/pam_handlers.c +++ b/libpam/pam_handlers.c @@ -7,6 +7,7 @@ */ #include "pam_private.h" +#include "pam_inline.h" #include #include @@ -315,7 +316,7 @@ _pam_open_config_file(pam_handle_t *pamh return PAM_ABORT; } - for (i = 0; i < sizeof (pamd_dirs)/sizeof (char *); i++) { + for (i = 0; i < PAM_ARRAY_SIZE(pamd_dirs); i++) { if (asprintf (&p, pamd_dirs[i], service) < 0) { pam_syslog(pamh, LOG_CRIT, "asprintf failed"); return PAM_BUF_ERR; diff --git a/modules/pam_exec/pam_exec.c b/modules/pam_exec/pam_exec.c index 6cad16e4..f1a1bc29 100644 --- a/modules/pam_exec/pam_exec.c +++ b/modules/pam_exec/pam_exec.c @@ -59,6 +59,7 @@ #include #include #include +#include "pam_inline.h" #define ENV_ITEM(n) { (n), #n } static struct { @@ -414,7 +415,7 @@ call_exec (const char *pam_type, pam_handle_t *pamh, envlist = pam_getenvlist(pamh); for (envlen = 0; envlist[envlen] != NULL; ++envlen) /* nothing */ ; - nitems = sizeof(env_items) / sizeof(*env_items); + nitems = PAM_ARRAY_SIZE(env_items); /* + 2 because of PAM_TYPE and NULL entry */ tmp = realloc(envlist, (envlen + nitems + 2) * sizeof(*envlist)); if (tmp == NULL) diff --git a/modules/pam_namespace/pam_namespace.c b/modules/pam_namespace/pam_namespace.c index 155d3965..51f7ac15 100644 --- a/modules/pam_namespace/pam_namespace.c +++ b/modules/pam_namespace/pam_namespace.c @@ -35,6 +35,7 @@ #define _ATFILE_SOURCE #include "pam_cc_compat.h" +#include "pam_inline.h" #include "pam_namespace.h" #include "argv_parse.h" @@ -260,7 +261,7 @@ static int filter_mntopts(const char *opts, char **filtered, do { size_t len; - int i; + unsigned int i; end = strchr(opts, ','); if (end == NULL) { @@ -269,7 +270,7 @@ static int filter_mntopts(const char *opts, char **filtered, len = end - opts; } - for (i = 0; i < (int)(sizeof(mntflags)/sizeof(mntflags[0])); i++) { + for (i = 0; i < PAM_ARRAY_SIZE(mntflags); i++) { if (mntflags[i].len != len) continue; if (memcmp(mntflags[i].name, opts, len) == 0) { diff --git a/modules/pam_timestamp/hmacfile.c b/modules/pam_timestamp/hmacfile.c index 69d39afa..371f814e 100644 --- a/modules/pam_timestamp/hmacfile.c +++ b/modules/pam_timestamp/hmacfile.c @@ -33,6 +33,8 @@ * OF THE POSSIBILITY OF SUCH DAMAGE. */ +#include "pam_inline.h" + #include #include #include @@ -104,7 +106,7 @@ testvectors(void) "e8e99d0f45237d786d6bbaa7965c7808bbff1a91", }, }; - for (i = 0; i < sizeof(vectors) / sizeof(vectors[0]); i++) { + for (i = 0; i < PAM_ARRAY_SIZE(vectors); i++) { hmac = NULL; hmac_len = 0; hmac_sha1_generate(&hmac, &hmac_len, diff --git a/modules/pam_xauth/pam_xauth.c b/modules/pam_xauth/pam_xauth.c index ea99bb03..f74a0c98 100644 --- a/modules/pam_xauth/pam_xauth.c +++ b/modules/pam_xauth/pam_xauth.c @@ -64,6 +64,7 @@ #endif #include "pam_cc_compat.h" +#include "pam_inline.h" #define DATANAME "pam_xauth_cookie_file" #define XAUTHENV "XAUTHORITY" @@ -172,7 +173,7 @@ run_coprocess(pam_handle_t *pamh, const char *input, char **output, /* Convert the varargs list into a regular array of strings. */ va_start(ap, command); args[0] = command; - for (j = 1; j < ((sizeof(args) / sizeof(args[0])) - 1); j++) { + for (j = 1; j < PAM_ARRAY_SIZE(args) - 1; j++) { args[j] = va_arg(ap, const char*); if (args[j] == NULL) { break; @@ -399,7 +400,7 @@ pam_sm_open_session (pam_handle_t *pamh, int flags UNUSED, if (xauth == NULL) { size_t j; - for (j = 0; j < sizeof(xauthpaths)/sizeof(xauthpaths[0]); j++) { + for (j = 0; j < PAM_ARRAY_SIZE(xauthpaths); j++) { if (access(xauthpaths[j], X_OK) == 0) { xauth = xauthpaths[j]; break; diff --git a/tests/tst-pam_get_item.c b/tests/tst-pam_get_item.c index e6c98a0f..2b0cc7e2 100644 --- a/tests/tst-pam_get_item.c +++ b/tests/tst-pam_get_item.c @@ -39,6 +39,7 @@ #include #include +#include "pam_inline.h" struct mapping { int type; @@ -67,7 +68,8 @@ main (void) const char *user = "root"; struct pam_conv conv; pam_handle_t *pamh; - int retval, num, i; + int retval; + unsigned int i; const void *value; /* 1: Call with NULL as pam handle */ @@ -89,9 +91,7 @@ main (void) /* 2: check for valid item types. Expected return value is PAM_SUCCESS, except it has to fail. */ - num = sizeof(items) / sizeof(struct mapping); - - for (i = 0; i < num; i++) + for (i = 0; i < PAM_ARRAY_SIZE(items); i++) { retval = pam_get_item (pamh, items[i].type, &value); @@ -115,7 +115,7 @@ main (void) } /* 4: check for valid item types, but NULL as value address. */ - for (i = 0; i < num; i++) + for (i = 0; i < PAM_ARRAY_SIZE(items); i++) { retval = pam_get_item (pamh, items[i].type, NULL); diff --git a/tests/tst-pam_set_item.c b/tests/tst-pam_set_item.c index f8f271d1..bd17e11a 100644 --- a/tests/tst-pam_set_item.c +++ b/tests/tst-pam_set_item.c @@ -40,6 +40,7 @@ #include #include +#include "pam_inline.h" struct mapping { int type; @@ -68,7 +69,8 @@ main (void) const char *user = "root"; struct pam_conv conv; pam_handle_t *pamh; - int retval, num, i; + int retval; + unsigned int i; /* 1: Call with NULL as pam handle */ retval = pam_set_item (NULL, PAM_SERVICE, "dummy"); @@ -108,9 +110,7 @@ main (void) } /* 4: try to replace all items */ - num = sizeof(items) / sizeof(struct mapping); - - for (i = 0; i < num; i++) + for (i = 0; i < PAM_ARRAY_SIZE(items); i++) { retval = pam_set_item (pamh, items[i].type, items[i].new_value); diff --git a/xtests/tst-pam_pwhistory1.c b/xtests/tst-pam_pwhistory1.c index 5c3246fa..1641c298 100644 --- a/xtests/tst-pam_pwhistory1.c +++ b/xtests/tst-pam_pwhistory1.c @@ -46,8 +46,9 @@ #include #include #include +#include "pam_inline.h" -static int in_test; +static unsigned int in_test; static const char *passwords[] = { "pamhistory01", "pamhistory02", "pamhistory03", @@ -121,15 +122,14 @@ main(int argc, char *argv[]) if (argc > 1 && strcmp (argv[1], "-d") == 0) debug = 1; - for (in_test = 0; - in_test < (int)(sizeof (passwords)/sizeof (char *)); in_test++) + for (in_test = 0; in_test < PAM_ARRAY_SIZE(passwords); in_test++) { retval = pam_start("tst-pam_pwhistory1", user, &conv, &pamh); if (retval != PAM_SUCCESS) { if (debug) - fprintf (stderr, "pwhistory1-%d: pam_start returned %d\n", + fprintf (stderr, "pwhistory1-%u: pam_start returned %d\n", in_test, retval); return 1; } @@ -140,7 +140,7 @@ main(int argc, char *argv[]) if (retval != PAM_SUCCESS) { if (debug) - fprintf (stderr, "pwhistory1-%d: pam_chauthtok returned %d\n", + fprintf (stderr, "pwhistory1-%u: pam_chauthtok returned %d\n", in_test, retval); return 1; } @@ -150,7 +150,7 @@ main(int argc, char *argv[]) if (retval != PAM_MAXTRIES) { if (debug) - fprintf (stderr, "pwhistory1-%d: pam_chauthtok returned %d\n", + fprintf (stderr, "pwhistory1-%u: pam_chauthtok returned %d\n", in_test, retval); return 1; } diff --git a/xtests/tst-pam_time1.c b/xtests/tst-pam_time1.c index 9fc0669e..18dfeba9 100644 --- a/xtests/tst-pam_time1.c +++ b/xtests/tst-pam_time1.c @@ -49,6 +49,7 @@ #include #include #include +#include "pam_inline.h" struct test_t { @@ -65,8 +66,6 @@ static struct test_t tests[] = { {"y", 6}, }; -static int num_tests = sizeof (tests) / sizeof (struct test_t); - static struct pam_conv conv = { NULL, NULL }; @@ -77,12 +76,12 @@ main(int argc, char *argv[]) pam_handle_t *pamh = NULL; int retval; int debug = 0; - int i; + unsigned int i; if (argc > 1 && strcmp (argv[1], "-d") == 0) debug = 1; - for (i = 0; i < num_tests; i++) + for (i = 0; i < PAM_ARRAY_SIZE(tests); i++) { retval = pam_start("tst-pam_time1", tests[i].user, &conv, &pamh); if (retval != PAM_SUCCESS) -- cgit v1.2.3