From 22079c3072d3548d44b32d805cc12329727a3c8c Mon Sep 17 00:00:00 2001 From: Rose <83477269+AtariDreams@users.noreply.github.com> Date: Thu, 18 May 2023 14:38:18 -0400 Subject: [PATCH] Avoid compiler casting warnings Part 2 This saves instructions that are related to casting as well as compiler warnings. --- lib/util/mktemp.c | 2 +- lib/util/regress/tailq/hltq_test.c | 2 +- lib/util/sudo_debug.c | 5 +++-- lib/util/ttyname_dev.c | 2 +- plugins/group_file/plugin_test.c | 4 ++-- plugins/sudoers/editor.c | 2 +- plugins/sudoers/filedigest.c | 14 ++++++-------- plugins/sudoers/match_addr.c | 6 +++--- plugins/sudoers/match_digest.c | 4 ++-- .../sudoers/regress/env_match/check_env_pattern.c | 4 ++-- plugins/sudoers/regress/exptilde/check_exptilde.c | 2 +- plugins/sudoers/sudoreplay.c | 4 ++-- plugins/sudoers/testsudoers.c | 2 +- plugins/sudoers/tsdump.c | 2 +- plugins/sudoers/visudo.c | 6 +++--- src/exec_monitor.c | 2 +- src/limits.c | 10 +++++----- src/sesh.c | 4 ++-- src/sudo.c | 8 ++++---- src/sudo.h | 2 +- src/sudo_edit.c | 2 +- 21 files changed, 44 insertions(+), 45 deletions(-) diff --git a/lib/util/mktemp.c b/lib/util/mktemp.c index 1540e511e..ef5e7de1c 100644 --- a/lib/util/mktemp.c +++ b/lib/util/mktemp.c @@ -90,7 +90,7 @@ mktemp_internal(int dfd, char *path, int slen, int mode, int flags) cp = start; do { unsigned short rbuf[16]; - unsigned int i; + size_t i; /* * Avoid lots of arc4random() calls by using diff --git a/lib/util/regress/tailq/hltq_test.c b/lib/util/regress/tailq/hltq_test.c index 2a1b2bb09..447f884dd 100644 --- a/lib/util/regress/tailq/hltq_test.c +++ b/lib/util/regress/tailq/hltq_test.c @@ -201,5 +201,5 @@ main(int argc, char *argv[]) getprogname(), ntests, errors, (ntests - errors) * 100 / ntests); } - exit(errors); + return errors; } diff --git a/lib/util/sudo_debug.c b/lib/util/sudo_debug.c index c06ba8cde..d8f07ffe4 100644 --- a/lib/util/sudo_debug.c +++ b/lib/util/sudo_debug.c @@ -304,7 +304,8 @@ sudo_debug_register_v2(const char *program, const char *const subsystems[], } if (instance == NULL) { - unsigned int i, j, max_id = NUM_DEF_SUBSYSTEMS - 1; + size_t i; + unsigned int j, max_id = NUM_DEF_SUBSYSTEMS - 1; /* Fill in subsystem name -> id mapping as needed. */ if (ids != NULL) { @@ -348,7 +349,7 @@ sudo_debug_register_v2(const char *program, const char *const subsystems[], } else { /* Check for matching instance but different ids[]. */ if (ids != NULL && instance->subsystem_ids != ids) { - unsigned int i; + size_t i; for (i = 0; subsystems[i] != NULL; i++) ids[i] = instance->subsystem_ids[i]; diff --git a/lib/util/ttyname_dev.c b/lib/util/ttyname_dev.c index 08b9777a4..a18e0f68d 100644 --- a/lib/util/ttyname_dev.c +++ b/lib/util/ttyname_dev.c @@ -121,7 +121,7 @@ sudo_ttyname_scan(const char *dir, dev_t rdev, char *name, size_t namelen) char *ret = NULL; struct dirent *dp; struct stat sb; - unsigned int i; + size_t i; DIR *d = NULL; debug_decl(sudo_ttyname_scan, SUDO_DEBUG_UTIL); diff --git a/plugins/group_file/plugin_test.c b/plugins/group_file/plugin_test.c index 3d94e6817..9b638b707 100644 --- a/plugins/group_file/plugin_test.c +++ b/plugins/group_file/plugin_test.c @@ -200,7 +200,7 @@ main(int argc, char *argv[]) if (group_plugin_load(plugin) != 1) { fprintf(stderr, "unable to load plugin: %s\n", plugin); - exit(EXIT_FAILURE); + return EXIT_FAILURE; } for (i = 0; argv[i] != NULL; i++) { @@ -215,6 +215,6 @@ main(int argc, char *argv[]) } group_plugin_unload(); - exit(EXIT_SUCCESS); + return EXIT_SUCCESS; } diff --git a/plugins/sudoers/editor.c b/plugins/sudoers/editor.c index 6f78d95d1..ed46a44ce 100644 --- a/plugins/sudoers/editor.c +++ b/plugins/sudoers/editor.c @@ -225,7 +225,7 @@ find_editor(int nfiles, char * const *files, int *argc_out, char ***argv_out, { char *editor_path = NULL; const char *ev[3]; - unsigned int i; + size_t i; debug_decl(find_editor, SUDOERS_DEBUG_UTIL); /* diff --git a/plugins/sudoers/filedigest.c b/plugins/sudoers/filedigest.c index e59cc7a6a..6954e7c5a 100644 --- a/plugins/sudoers/filedigest.c +++ b/plugins/sudoers/filedigest.c @@ -48,18 +48,13 @@ sudo_filedigest(int fd, const char *file, unsigned int digest_type, *digest_len = sudo_digest_getlen(digest_type); if (*digest_len == (size_t)-1) { sudo_warnx(U_("unsupported digest type %u for %s"), digest_type, file); - goto bad; - } - - if ((dig = sudo_digest_alloc(digest_type)) == NULL) { - sudo_warnx(U_("%s: %s"), __func__, U_("unable to allocate memory")); - goto bad; + debug_return_ptr(NULL); } if ((fd2 = dup(fd)) == -1) { sudo_debug_printf(SUDO_DEBUG_INFO, "unable to dup %s: %s", file, strerror(errno)); - goto bad; + debug_return_ptr(NULL); } if ((fp = fdopen(fd2, "r")) == NULL) { sudo_debug_printf(SUDO_DEBUG_INFO, "unable to fdopen %s: %s", @@ -71,7 +66,10 @@ sudo_filedigest(int fd, const char *file, unsigned int digest_type, sudo_warnx(U_("%s: %s"), __func__, U_("unable to allocate memory")); goto bad; } - + if ((dig = sudo_digest_alloc(digest_type)) == NULL) { + sudo_warnx(U_("%s: %s"), __func__, U_("unable to allocate memory")); + goto bad; + } while ((nread = fread(buf, 1, sizeof(buf), fp)) != 0) { sudo_digest_update(dig, buf, nread); } diff --git a/plugins/sudoers/match_addr.c b/plugins/sudoers/match_addr.c index 714c41cdc..16d464d7e 100644 --- a/plugins/sudoers/match_addr.c +++ b/plugins/sudoers/match_addr.c @@ -50,7 +50,7 @@ addr_matches_if(const char *n) union sudo_in_addr_un addr; struct interface *ifp; #ifdef HAVE_STRUCT_IN6_ADDR - unsigned int j; + size_t j; #endif unsigned int family; debug_decl(addr_matches_if, SUDOERS_DEBUG_MATCH); @@ -98,11 +98,11 @@ addr_matches_if(const char *n) static bool addr_matches_if_netmask(const char *n, const char *m) { - unsigned int i; + size_t i; union sudo_in_addr_un addr, mask; struct interface *ifp; #ifdef HAVE_STRUCT_IN6_ADDR - unsigned int j; + size_t j; #endif unsigned int family; const char *errstr; diff --git a/plugins/sudoers/match_digest.c b/plugins/sudoers/match_digest.c index 9aa196da1..4dc49829d 100644 --- a/plugins/sudoers/match_digest.c +++ b/plugins/sudoers/match_digest.c @@ -84,9 +84,9 @@ digest_matches(int fd, const char *path, } if (strlen(digest->digest_str) == digest_len * 2) { /* Convert ascii hex to binary. */ - unsigned int i; + size_t i; for (i = 0; i < digest_len; i++) { - const int h = sudo_hexchar(&digest->digest_str[i + i]); + const int h = sudo_hexchar(&digest->digest_str[2 * i]); if (h == -1) goto bad_format; sudoers_digest[i] = (unsigned char)h; diff --git a/plugins/sudoers/regress/env_match/check_env_pattern.c b/plugins/sudoers/regress/env_match/check_env_pattern.c index ce2c267e8..3becabb05 100644 --- a/plugins/sudoers/regress/env_match/check_env_pattern.c +++ b/plugins/sudoers/regress/env_match/check_env_pattern.c @@ -38,7 +38,7 @@ main(int argc, char *argv[]) if (argc > 1) { if ((fp = fopen(argv[1], "r")) == NULL) { perror(argv[1]); - exit(EXIT_FAILURE); + return EXIT_FAILURE; } } @@ -72,5 +72,5 @@ main(int argc, char *argv[]) getprogname(), tests, tests == 1 ? "" : "s", errors, (tests - errors) * 100 / tests); } - exit(errors); + return errors; } diff --git a/plugins/sudoers/regress/exptilde/check_exptilde.c b/plugins/sudoers/regress/exptilde/check_exptilde.c index 93f9b4b65..94746c23b 100644 --- a/plugins/sudoers/regress/exptilde/check_exptilde.c +++ b/plugins/sudoers/regress/exptilde/check_exptilde.c @@ -101,5 +101,5 @@ main(int argc, char *argv[]) getprogname(), ntests, errors, (ntests - errors) * 100 / ntests); } - exit(errors); + return errors; } diff --git a/plugins/sudoers/sudoreplay.c b/plugins/sudoers/sudoreplay.c index 5766fe3b2..c79df453c 100644 --- a/plugins/sudoers/sudoreplay.c +++ b/plugins/sudoers/sudoreplay.c @@ -225,7 +225,7 @@ main(int argc, char *argv[]) /* Read sudo.conf and initialize the debug subsystem. */ if (sudo_conf_read(NULL, SUDO_CONF_DEBUG) == -1) - exit(EXIT_FAILURE); + return EXIT_FAILURE; sudo_debug_register(getprogname(), NULL, NULL, sudo_conf_debug_files(getprogname()), -1); @@ -1479,7 +1479,7 @@ find_sessions(const char *dir, regex_t *re, const char *user, const char *tty) struct stat sb; struct sudo_lbuf lbuf; size_t sdlen, sessions_len = 0, sessions_size = 0; - unsigned int i; + size_t i; int len; char pathbuf[PATH_MAX], **sessions = NULL; #ifdef HAVE_STRUCT_DIRENT_D_TYPE diff --git a/plugins/sudoers/testsudoers.c b/plugins/sudoers/testsudoers.c index 23af90c5c..860c3a517 100644 --- a/plugins/sudoers/testsudoers.c +++ b/plugins/sudoers/testsudoers.c @@ -371,7 +371,7 @@ done: sudo_freepwcache(); sudo_freegrcache(); sudo_debug_exit_int(__func__, __FILE__, __LINE__, sudo_debug_subsys, exitcode); - exit(exitcode); + return exitcode; } static void diff --git a/plugins/sudoers/tsdump.c b/plugins/sudoers/tsdump.c index 9b6ba4617..e66aca967 100644 --- a/plugins/sudoers/tsdump.c +++ b/plugins/sudoers/tsdump.c @@ -81,7 +81,7 @@ main(int argc, char *argv[]) /* Initialize the debug subsystem. */ if (sudo_conf_read(NULL, SUDO_CONF_DEBUG) == -1) - exit(EXIT_FAILURE); + return EXIT_FAILURE; sudoers_debug_register(getprogname(), sudo_conf_debug_files(getprogname())); while ((ch = getopt(argc, argv, "f:u:")) != -1) { diff --git a/plugins/sudoers/visudo.c b/plugins/sudoers/visudo.c index 36122180b..5b2612bdf 100644 --- a/plugins/sudoers/visudo.c +++ b/plugins/sudoers/visudo.c @@ -167,11 +167,11 @@ main(int argc, char *argv[]) /* Read debug and plugin sections of sudo.conf. */ if (sudo_conf_read(NULL, SUDO_CONF_DEBUG|SUDO_CONF_PLUGINS) == -1) - exit(EXIT_FAILURE); + return EXIT_FAILURE; /* Initialize the debug subsystem. */ if (!sudoers_debug_register(getprogname(), sudo_conf_debug_files(getprogname()))) - exit(EXIT_FAILURE); + return EXIT_FAILURE; /* Parse sudoers plugin options, if any. */ parse_sudoers_options(); @@ -294,7 +294,7 @@ main(int argc, char *argv[]) sudoers_conf.sudoers_path = path_sudoers; init_parser(NULL, &sudoers_conf); if ((sudoersin = open_sudoers(path_sudoers, &sudoers, true, NULL)) == NULL) - exit(EXIT_FAILURE); + return EXIT_FAILURE; sudoers_setlocale(SUDOERS_LOCALE_SUDOERS, &oldlocale); (void) sudoersparse(); (void) update_defaults(&parsed_policy, NULL, diff --git a/src/exec_monitor.c b/src/exec_monitor.c index 950ad366c..7a7974e24 100644 --- a/src/exec_monitor.c +++ b/src/exec_monitor.c @@ -321,7 +321,7 @@ mon_backchannel_cb(int fd, int what, void *v) * Note that the backchannel is a *blocking* socket. */ n = recv(fd, &cstmp, sizeof(cstmp), MSG_WAITALL); - if (n != sizeof(cstmp)) { + if (n != ssizeof(cstmp)) { if (n == -1) { if (errno == EINTR || errno == EAGAIN) debug_return; diff --git a/src/limits.c b/src/limits.c index 41c3225c8..0a0776eef 100644 --- a/src/limits.c +++ b/src/limits.c @@ -671,11 +671,11 @@ set_policy_rlimits(void) debug_return; } -int -serialize_rlimits(char **info, unsigned int info_max) +size_t +serialize_rlimits(char **info, size_t info_max) { char *str; - unsigned int idx, nstored = 0; + size_t idx, nstored = 0; debug_decl(serialize_rlimits, SUDO_DEBUG_UTIL); for (idx = 0; idx < nitems(saved_limits); idx++) { @@ -706,9 +706,9 @@ serialize_rlimits(char **info, unsigned int info_max) goto oom; info[nstored++] = str; } - debug_return_int(nstored); + debug_return_size_t(nstored); oom: while (nstored) free(info[--nstored]); - debug_return_int(-1); + debug_return_size_t(-1); } diff --git a/src/sesh.c b/src/sesh.c index d91b8b9b2..0415e7b5f 100644 --- a/src/sesh.c +++ b/src/sesh.c @@ -157,7 +157,7 @@ main(int argc, char *argv[], char *envp[]) /* Read sudo.conf and initialize the debug subsystem. */ if (sudo_conf_read(NULL, SUDO_CONF_DEBUG) == -1) - exit(EXIT_FAILURE); + return EXIT_FAILURE; sudo_debug_register(getprogname(), NULL, NULL, sudo_conf_debug_files(getprogname()), -1); @@ -197,7 +197,7 @@ main(int argc, char *argv[], char *envp[]) if (rundir != NULL && chdir(rundir) == -1) { sudo_warnx(U_("unable to change directory to %s"), rundir); if (!ISSET(flags, CD_CWD_OPTIONAL)) - exit(EXIT_FAILURE); + return EXIT_FAILURE; } /* Make a copy of the command to execute. */ diff --git a/src/sudo.c b/src/sudo.c index ee56655f4..7d3d9d149 100644 --- a/src/sudo.c +++ b/src/sudo.c @@ -500,11 +500,11 @@ static char ** get_user_info(struct user_details *ud) { char *cp, **info, path[PATH_MAX]; - unsigned int info_max = 32 + RLIM_NLIMITS; - unsigned int i = 0; + size_t info_max = 32 + RLIM_NLIMITS; + size_t i = 0, n; mode_t mask; struct passwd *pw; - int ttyfd, n; + int ttyfd; debug_decl(get_user_info, SUDO_DEBUG_UTIL); /* @@ -629,7 +629,7 @@ get_user_info(struct user_details *ud) goto oom; n = serialize_rlimits(&info[i + 1], info_max - (i + 1)); - if (n == -1) + if (n == (size_t)-1) goto oom; i += n; diff --git a/src/sudo.h b/src/sudo.h index f015cf90d..892e522d4 100644 --- a/src/sudo.h +++ b/src/sudo.h @@ -343,7 +343,7 @@ void restore_nproc(void); void set_policy_rlimits(void); void unlimit_nproc(void); void unlimit_sudo(void); -int serialize_rlimits(char **info, unsigned int info_max); +size_t serialize_rlimits(char **info, size_t info_max); bool parse_policy_rlimit(const char *str); /* exec_ptrace.c */ diff --git a/src/sudo_edit.c b/src/sudo_edit.c index dc7d6840c..1ee86426f 100644 --- a/src/sudo_edit.c +++ b/src/sudo_edit.c @@ -71,7 +71,7 @@ set_tmpdir(const struct sudo_cred *user_cred) _PATH_TMP }; struct sudo_cred saved_cred; - unsigned int i; + size_t i; size_t len; int dfd; debug_decl(set_tmpdir, SUDO_DEBUG_EDIT);