From: Tomas Mraz Date: Mon, 15 Apr 2019 08:42:11 +0000 (+0200) Subject: anacron: fix types in comparisons X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=395a537433146cb9d32b184494a440074092d8f9;p=cronie anacron: fix types in comparisons This fixes warnings reported by -Wsign-compare compiler option. Signed-off-by: Sami Kerola Signed-off-by: Tomas Mraz --- diff --git a/anacron/log.c b/anacron/log.c index 2c4c8ba..850f2ac 100644 --- a/anacron/log.c +++ b/anacron/log.c @@ -79,7 +79,12 @@ make_msg(const char *fmt, va_list args) /* There's some confusion in the documentation about what vsnprintf * returns when the buffer overflows. Hmmm... */ len = vsnprintf(msg, sizeof(msg), fmt, args); - if (len >= sizeof(msg) - 1) + if (len < 0) { + msg[0] = '\0'; + strncat(msg, "(vsnprintf failed)", sizeof(msg)); + return; + } + if ((size_t) len >= sizeof(msg) - 1) strcpy(msg + sizeof(msg) - sizeof(truncated), truncated); } diff --git a/anacron/matchrx.c b/anacron/matchrx.c index e4c0d15..6906fa1 100644 --- a/anacron/matchrx.c +++ b/anacron/matchrx.c @@ -42,11 +42,13 @@ match_rx(const char *rx, char *string, unsigned int n_sub, /* char **substrings * This is not the most efficient, or elegant way of doing this. */ { - int r, n; + int r; + unsigned int n; regex_t crx; va_list va; char **substring; regmatch_t *sub_offsets; + sub_offsets = malloc(sizeof(regmatch_t) * (n_sub + 1)); if (sub_offsets == NULL) return -1; diff --git a/anacron/runjob.c b/anacron/runjob.c index 68681e4..a677463 100644 --- a/anacron/runjob.c +++ b/anacron/runjob.c @@ -53,7 +53,9 @@ temp_file(job_rec *jr) dir = P_tmpdir; len = snprintf(template, sizeof(template), "%s/$anacronXXXXXX", dir); - if (len >= sizeof(template)) + if (len < 0) + die_e("snprintf failed"); + else if ((size_t) len >= sizeof(template)) die_e("TMPDIR too long"); fdout = mkstemp(template); diff --git a/src/do_command.c b/src/do_command.c index be38127..a15f9be 100644 --- a/src/do_command.c +++ b/src/do_command.c @@ -427,8 +427,14 @@ static int child_process(entry * e, char **jobenv) { gethostname(hostname, MAXHOSTNAMELEN); if (MailCmd[0] == '\0') { - if (snprintf(mailcmd, sizeof mailcmd, MAILFMT, MAILARG, mailfrom) - >= sizeof mailcmd) { + int len; + + len = snprintf(mailcmd, sizeof mailcmd, MAILFMT, MAILARG, mailfrom); + if (len < 0) { + fprintf(stderr, "mailcmd snprintf failed\n"); + (void) _exit(ERROR_EXIT); + } + if (sizeof mailcmd <= (size_t) len) { fprintf(stderr, "mailcmd too long\n"); (void) _exit(ERROR_EXIT); } diff --git a/src/env.c b/src/env.c index 491f6f5..3336c51 100644 --- a/src/env.c +++ b/src/env.c @@ -23,6 +23,7 @@ #include #include +#include #include #include #include @@ -281,7 +282,7 @@ char *env_get(const char *name, char **envp) { while ((p = *envp++) != NULL) { if (!(q = strchr(p, '='))) continue; - if ((q - p) == len && !strncmp(p, name, len)) + if ((size_t)(q - p) == len && !strncmp(p, name, len)) return (q + 1); } return (NULL);