diff --git a/src/connect.c b/src/connect.c index 0704000f..60b0294a 100644 --- a/src/connect.c +++ b/src/connect.c @@ -331,8 +331,10 @@ connect_to_ip (const ip_address *ip, int port, const char *print) if (bufsize < 512) bufsize = 512; /* avoid pathologically small values */ #ifdef SO_RCVBUF - setsockopt (sock, SOL_SOCKET, SO_RCVBUF, - (void *)&bufsize, (socklen_t)sizeof (bufsize)); + if (setsockopt (sock, SOL_SOCKET, SO_RCVBUF, + (void *) &bufsize, (socklen_t) sizeof (bufsize))) + logprintf (LOG_NOTQUIET, _("setsockopt SO_RCVBUF failed: %s\n"), + strerror (errno)); #endif /* When we add limit_rate support for writing, which is useful for POST, we should also set SO_SNDBUF here. */ @@ -471,7 +473,9 @@ bind_local (const ip_address *bind_address, int *port) return -1; #ifdef SO_REUSEADDR - setsockopt (sock, SOL_SOCKET, SO_REUSEADDR, setopt_ptr, setopt_size); + if (setsockopt (sock, SOL_SOCKET, SO_REUSEADDR, setopt_ptr, setopt_size)) + logprintf (LOG_NOTQUIET, _("setsockopt SO_REUSEADDR failed: %s\n"), + strerror (errno)); #endif xzero (ss); diff --git a/src/ftp.c b/src/ftp.c index d5d00324..39f20fa0 100644 --- a/src/ftp.c +++ b/src/ftp.c @@ -2363,7 +2363,12 @@ Already have correct symlink %s -> %s\n\n"), (f->type == FT_PLAINFILE) && opt.preserve_perm) { if (f->perms) - chmod (actual_target, f->perms); + { + if (chmod (actual_target, f->perms)) + logprintf (LOG_NOTQUIET, + _("Failed to set permissions for %s.\n"), + actual_target); + } else DEBUGP (("Unrecognized permissions for %s.\n", actual_target)); } diff --git a/src/http.c b/src/http.c index 6ac795bf..51e895b9 100644 --- a/src/http.c +++ b/src/http.c @@ -4625,7 +4625,7 @@ digest_authentication_encode (const char *au, const char *user, { "algorithm", &algorithm } }; char cnonce[16] = ""; - char *res; + char *res = NULL; int res_len; size_t res_size; param_token name, value; @@ -4648,29 +4648,22 @@ digest_authentication_encode (const char *au, const char *user, } } - if (qop != NULL && strcmp (qop,"auth")) + if (qop && strcmp (qop, "auth")) { logprintf (LOG_NOTQUIET, _("Unsupported quality of protection '%s'.\n"), qop); - xfree (qop); /* force freeing mem and return */ + xfree (qop); /* force freeing mem and continue */ } - else if (algorithm != NULL && strcmp (algorithm,"MD5") && strcmp (algorithm,"MD5-sess")) + else if (algorithm && strcmp (algorithm,"MD5") && strcmp (algorithm,"MD5-sess")) { logprintf (LOG_NOTQUIET, _("Unsupported algorithm '%s'.\n"), algorithm); - xfree (qop); /* force freeing mem and return */ + xfree (algorithm); /* force freeing mem and continue */ } if (!realm || !nonce || !user || !passwd || !path || !method) { *auth_err = ATTRMISSING; - - xfree (realm); - xfree (opaque); - xfree (nonce); - xfree (qop); - xfree (algorithm); - - return NULL; - } + goto cleanup; + } /* Calculate the digest value. */ { @@ -4791,6 +4784,7 @@ digest_authentication_encode (const char *au, const char *user, } } +cleanup: xfree (realm); xfree (opaque); xfree (nonce); diff --git a/src/init.c b/src/init.c index b7f573a9..6729c5a8 100644 --- a/src/init.c +++ b/src/init.c @@ -872,6 +872,10 @@ static bool setval_internal (int comind, const char *com, const char *val) { assert (0 <= comind && ((size_t) comind) < countof (commands)); + + if (comind < 0 || comind >= countof (commands)) + return NULL; + DEBUGP (("Setting %s (%s) to %s\n", com, commands[comind].name, val)); return commands[comind].action (com, val, commands[comind].place); } diff --git a/src/main.c b/src/main.c index 97ab2668..ac6ee2c6 100644 --- a/src/main.c +++ b/src/main.c @@ -1454,7 +1454,8 @@ main (int argc, char **argv) append_to_log = true; break; case OPT__EXECUTE: - run_command (optarg); + if (optarg) /* check silences static analyzer */ + run_command (optarg); break; case OPT__NO: { diff --git a/src/retr.c b/src/retr.c index 05668a01..de3a5213 100644 --- a/src/retr.c +++ b/src/retr.c @@ -634,7 +634,7 @@ retr_rate (wgint bytes, double secs) double dlrate = calc_rate (bytes, secs, &units); /* Use more digits for smaller numbers (regardless of unit used), e.g. "1022", "247", "12.5", "2.38". */ - sprintf (res, "%.*f %s", + snprintf (res, sizeof(res), "%.*f %s", dlrate >= 99.95 ? 0 : dlrate >= 9.995 ? 1 : 2, dlrate, !opt.report_bps ? rate_names[units]: rate_names_bits[units]); @@ -1147,7 +1147,7 @@ retrieve_from_file (const char *file, bool html, int *count) Removing file due to --delete-after in retrieve_from_file():\n")); logprintf (LOG_VERBOSE, _("Removing %s.\n"), filename); if (unlink (filename)) - logprintf (LOG_NOTQUIET, "unlink: %s\n", strerror (errno)); + logprintf (LOG_NOTQUIET, "Failed to unlink %s: (%d) %s\n", filename, errno, strerror (errno)); dt &= ~RETROKF; } @@ -1248,8 +1248,8 @@ rotate_backups(const char *fname) #endif int maxlen = strlen (fname) + sizeof (SEP) + numdigit (opt.backups) + AVSL; - char *from = (char *)alloca (maxlen); - char *to = (char *)alloca (maxlen); + char *from = alloca (maxlen); + char *to = alloca (maxlen); struct_stat sb; int i; @@ -1267,17 +1267,21 @@ rotate_backups(const char *fname) */ if (i == opt.backups) { - sprintf (to, "%s%s%d%s", fname, SEP, i, AVS); + snprintf (to, sizeof(to), "%s%s%d%s", fname, SEP, i, AVS); delete (to); } #endif - sprintf (to, "%s%s%d", fname, SEP, i); - sprintf (from, "%s%s%d", fname, SEP, i - 1); - rename (from, to); + snprintf (to, maxlen, "%s%s%d", fname, SEP, i); + snprintf (from, maxlen, "%s%s%d", fname, SEP, i - 1); + if (rename (from, to)) + logprintf (LOG_NOTQUIET, "Failed to rename %s to %s: (%d) %s\n", + from, to, errno, strerror (errno)); } - sprintf (to, "%s%s%d", fname, SEP, 1); - rename(fname, to); + snprintf (to, maxlen, "%s%s%d", fname, SEP, 1); + if (rename(fname, to)) + logprintf (LOG_NOTQUIET, "Failed to rename %s to %s: (%d) %s\n", + fname, to, errno, strerror (errno)); } static bool no_proxy_match (const char *, const char **); diff --git a/src/url.c b/src/url.c index ea25fa7a..d15fef50 100644 --- a/src/url.c +++ b/src/url.c @@ -1275,7 +1275,9 @@ mkalldirs (const char *path) name exists, we just remove it and create the directory anyway. */ DEBUGP (("Removing %s because of directory danger!\n", t)); - unlink (t); + if (unlink (t)) + logprintf (LOG_NOTQUIET, "Failed to unlink %s (%d): %s\n", + t, errno, strerror(errno)); } } res = make_directory (t); diff --git a/src/utils.c b/src/utils.c index e42bb8f7..d8b3c828 100644 --- a/src/utils.c +++ b/src/utils.c @@ -235,14 +235,20 @@ xstrdup_lower (const char *s) /* Copy the string formed by two pointers (one on the beginning, other on the char after the last char) to a new, malloc-ed location. - 0-terminate it. */ + 0-terminate it. + If both pointers are NULL, the function returns an empty string. */ char * strdupdelim (const char *beg, const char *end) { - char *res = xmalloc (end - beg + 1); - memcpy (res, beg, end - beg); - res[end - beg] = '\0'; - return res; + if (beg && beg <= end) + { + char *res = xmalloc (end - beg + 1); + memcpy (res, beg, end - beg); + res[end - beg] = '\0'; + return res; + } + + return xstrdup(""); } /* Parse a string containing comma-separated elements, and return a @@ -1327,7 +1333,7 @@ merge_vecs (char **v1, char **v2) for (j = 0; v2[j]; j++) ; /* Reallocate v1. */ - v1 = xrealloc (v1, (i + j + 1) * sizeof (char **)); + v1 = xrealloc (v1, (i + j + 1) * sizeof (char *)); memcpy (v1 + i, v2, (j + 1) * sizeof (char *)); xfree (v2); return v1; @@ -2425,8 +2431,9 @@ stable_sort (void *base, size_t nmemb, size_t size, { if (size > 1) { - void *temp = alloca (nmemb * size * sizeof (void *)); + void *temp = malloc (nmemb * size * sizeof (void *)); mergesort_internal (base, temp, size, 0, nmemb - 1, cmpfun); + xfree(temp); } }