From c6987aa26ea12569e65bc7ab18e702b06ab8d715 Mon Sep 17 00:00:00 2001 From: "Todd C. Miller" Date: Mon, 21 Aug 2023 12:50:31 -0600 Subject: [PATCH] Cast int to size_t before adding instead of casting the result. Quiets PVS-Studio warning V1028. --- plugins/sample/sample_plugin.c | 3 ++- plugins/sudoers/editor.c | 2 +- plugins/sudoers/group_plugin.c | 2 +- plugins/sudoers/policy.c | 2 +- plugins/sudoers/sudoreplay.c | 4 ++-- src/exec_ptrace.c | 4 ++-- 6 files changed, 9 insertions(+), 8 deletions(-) diff --git a/plugins/sample/sample_plugin.c b/plugins/sample/sample_plugin.c index a324d5ef5..0e68e9662 100644 --- a/plugins/sample/sample_plugin.c +++ b/plugins/sample/sample_plugin.c @@ -286,7 +286,8 @@ find_editor(int nfiles, char * const files[], char **argv_out[]) } if (editor_path != editor) free(editor); - nargv = reallocarray(NULL, (size_t)(nargc + 1 + nfiles + 1), sizeof(char *)); + nargv = reallocarray(NULL, (size_t)nargc + 1 + (size_t)nfiles + 1, + sizeof(char *)); if (nargv == NULL) { sudo_log(SUDO_CONV_ERROR_MSG, "unable to allocate memory\n"); free(editor_path); diff --git a/plugins/sudoers/editor.c b/plugins/sudoers/editor.c index 8900bfb9c..424c4aeda 100644 --- a/plugins/sudoers/editor.c +++ b/plugins/sudoers/editor.c @@ -158,7 +158,7 @@ resolve_editor(const char *ed, size_t edlen, int nfiles, char * const *files, nargc++; if (nfiles != 0) nargc += nfiles + 1; - nargv = reallocarray(NULL, (size_t)(nargc + 1), sizeof(char *)); + nargv = reallocarray(NULL, (size_t)nargc + 1, sizeof(char *)); if (nargv == NULL) goto oom; sudoers_gc_add(GC_PTR, nargv); diff --git a/plugins/sudoers/group_plugin.c b/plugins/sudoers/group_plugin.c index 3a9eee6ed..19878f7b9 100644 --- a/plugins/sudoers/group_plugin.c +++ b/plugins/sudoers/group_plugin.c @@ -206,7 +206,7 @@ group_plugin_load(const struct sudoers_context *ctx, const char *plugin_info) } } if (ac != 0) { - argv = reallocarray(NULL, (size_t)(ac + 1), sizeof(char *)); + argv = reallocarray(NULL, (size_t)ac + 1, sizeof(char *)); if (argv == NULL) { sudo_warnx(U_("%s: %s"), __func__, U_("unable to allocate memory")); diff --git a/plugins/sudoers/policy.c b/plugins/sudoers/policy.c index 8452d2042..282efc479 100644 --- a/plugins/sudoers/policy.c +++ b/plugins/sudoers/policy.c @@ -796,7 +796,7 @@ sudoers_policy_store_result(struct sudoers_context *ctx, bool accepted, /* We reserve an extra spot in the list for the effective gid. */ glsize = sizeof("runas_groups=") - 1 + - ((size_t)(gidlist->ngids + 1) * (MAX_UID_T_LEN + 1)); + (((size_t)gidlist->ngids + 1) * (MAX_UID_T_LEN + 1)); gid_list = malloc(glsize); if (gid_list == NULL) { sudo_gidlist_delref(gidlist); diff --git a/plugins/sudoers/sudoreplay.c b/plugins/sudoers/sudoreplay.c index 2960b43e5..8db83833c 100644 --- a/plugins/sudoers/sudoreplay.c +++ b/plugins/sudoers/sudoreplay.c @@ -1551,11 +1551,11 @@ find_sessions(const char *dir, regex_t *re, const char *user, const char *tty) /* Check for dir with a log file. */ if (lstat(pathbuf, &sb) == 0 && S_ISREG(sb.st_mode)) { - pathbuf[sdlen + (size_t)(len - 4)] = '\0'; + pathbuf[sdlen + (size_t)len - 4] = '\0'; list_session(&lbuf, pathbuf, re, user, tty); } else { /* Strip off "/log" and recurse if a non-log dir. */ - pathbuf[sdlen + (size_t)(len - 4)] = '\0'; + pathbuf[sdlen + (size_t)len - 4] = '\0'; if (checked_type || (lstat(pathbuf, &sb) == 0 && S_ISDIR(sb.st_mode))) find_sessions(pathbuf, re, user, tty); diff --git a/src/exec_ptrace.c b/src/exec_ptrace.c index accab32f7..196b342c3 100644 --- a/src/exec_ptrace.c +++ b/src/exec_ptrace.c @@ -1884,7 +1884,7 @@ ptrace_intercept_execve(pid_t pid, struct intercept_closure *closure) */ if (argv_mismatch) { /* argv pointers */ - space += (size_t)(argc + 1 + regs.compat) * regs.wordsize; + space += ((size_t)argc + 1 + regs.compat) * regs.wordsize; /* argv strings */ for (argc = 0; closure->run_argv[argc] != NULL; argc++) { @@ -1905,7 +1905,7 @@ ptrace_intercept_execve(pid_t pid, struct intercept_closure *closure) set_sc_arg2(®s, sp); /* Skip over argv pointers (plus NULL) for string table. */ - strtab += (size_t)(argc + 1 + regs.compat) * regs.wordsize; + strtab += ((size_t)argc + 1 + regs.compat) * regs.wordsize; nwritten = ptrace_write_vec(pid, ®s, closure->run_argv, sp, strtab);