From: Todd C. Miller Date: Fri, 19 Oct 2018 19:35:20 +0000 (-0600) Subject: Avoid some PVS-Studio false positives. X-Git-Tag: SUDO_1_8_26^2~36 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=8c94175ba157525fcbd1aff8495a7e0484b75826;p=sudo Avoid some PVS-Studio false positives. --- diff --git a/lib/util/key_val.c b/lib/util/key_val.c index 2477b7c2b..8ab8c24e0 100644 --- a/lib/util/key_val.c +++ b/lib/util/key_val.c @@ -44,7 +44,7 @@ sudo_new_key_val_v1(const char *key, const char *val) debug_decl(sudo_new_key_val, SUDO_DEBUG_UTIL) cp = str = malloc(key_len + 1 + val_len + 1); - if (str != NULL) { + if (cp != NULL) { memcpy(cp, key, key_len); cp += key_len; *cp++ = '='; diff --git a/plugins/sudoers/iolog_path.c b/plugins/sudoers/iolog_path.c index 16a453e6a..0758512bf 100644 --- a/plugins/sudoers/iolog_path.c +++ b/plugins/sudoers/iolog_path.c @@ -162,13 +162,14 @@ expand_iolog_path(const char *prefix, const char *dir, const char *file, /* Expanded path must be <= PATH_MAX */ if (prefix != NULL) prelen = strlen(prefix); - dst = path = malloc(prelen + PATH_MAX); + path = malloc(prelen + PATH_MAX); if (path == NULL) { sudo_warnx(U_("%s: %s"), __func__, U_("unable to allocate memory")); goto bad; } *path = '\0'; pathend = path + prelen + PATH_MAX; + dst = path; /* Copy prefix, if present. */ if (prefix != NULL) { diff --git a/plugins/sudoers/linux_audit.c b/plugins/sudoers/linux_audit.c index 5923396dc..10ddb3ef7 100644 --- a/plugins/sudoers/linux_audit.c +++ b/plugins/sudoers/linux_audit.c @@ -71,12 +71,12 @@ linux_audit_command(char *argv[], int result) /* Convert argv to a flat string. */ for (size = 0, av = argv; *av != NULL; av++) size += strlen(*av) + 1; - command = cp = malloc(size); + command = malloc(size); if (command == NULL) { sudo_warnx(U_("%s: %s"), __func__, U_("unable to allocate memory")); goto done; } - for (av = argv; *av != NULL; av++) { + for (av = argv, cp = command; *av != NULL; av++) { n = strlcpy(cp, *av, size - (cp - command)); if (n >= size - (cp - command)) { sudo_warnx(U_("internal error, %s overflow"), __func__); diff --git a/plugins/sudoers/toke_util.c b/plugins/sudoers/toke_util.c index 5e5e70f53..d6754b469 100644 --- a/plugins/sudoers/toke_util.c +++ b/plugins/sudoers/toke_util.c @@ -104,11 +104,12 @@ fill_cmnd(const char *src, size_t len) arg_len = arg_size = 0; dst = sudoerslval.command.cmnd = malloc(len + 1); - if (sudoerslval.command.cmnd == NULL) { + if (dst == NULL) { sudo_warnx(U_("%s: %s"), __func__, U_("unable to allocate memory")); sudoerserror(NULL); debug_return_bool(false); } + sudoerslval.command.args = NULL; /* Copy the string and collapse any escaped sudo-specific characters. */ for (i = 0; i < len; i++) { @@ -119,7 +120,6 @@ fill_cmnd(const char *src, size_t len) } *dst = '\0'; - sudoerslval.command.args = NULL; debug_return_bool(true); } diff --git a/src/preserve_fds.c b/src/preserve_fds.c index c782f0712..b92ed5fea 100644 --- a/src/preserve_fds.c +++ b/src/preserve_fds.c @@ -59,16 +59,18 @@ add_preserved_fd(struct preserved_fd_list *pfds, int fd) sudo_debug_printf(SUDO_DEBUG_DEBUG|SUDO_DEBUG_LINENO, "fd %d already preserved", fd); free(pfd_new); + pfd_new = NULL; break; } if (fd < pfd->highfd) { TAILQ_INSERT_BEFORE(pfd, pfd_new, entries); sudo_debug_printf(SUDO_DEBUG_DEBUG|SUDO_DEBUG_LINENO, "preserving fd %d", fd); + pfd_new = NULL; break; } } - if (pfd == NULL) { + if (pfd_new != NULL) { TAILQ_INSERT_TAIL(pfds, pfd_new, entries); sudo_debug_printf(SUDO_DEBUG_DEBUG|SUDO_DEBUG_LINENO, "preserving fd %d", fd);