libsudo_util: silence most -Wconversion warnings.

This commit is contained in:
Todd C. Miller
2023-07-07 15:07:04 -06:00
parent f7801f2160
commit 5768d374cc
29 changed files with 128 additions and 111 deletions

View File

@@ -66,8 +66,8 @@ struct json_container {
bool quiet; bool quiet;
}; };
sudo_dso_public bool sudo_json_init_v1(struct json_container *jsonc, int indent, bool minimal, bool memfatal); sudo_dso_public bool sudo_json_init_v1(struct json_container *jsonc, unsigned int indent, bool minimal, bool memfatal);
sudo_dso_public bool sudo_json_init_v2(struct json_container *jsonc, int indent, bool minimal, bool memfatal, bool quiet); sudo_dso_public bool sudo_json_init_v2(struct json_container *jsonc, unsigned int indent, bool minimal, bool memfatal, bool quiet);
#define sudo_json_init(_a, _b, _c, _d, _e) sudo_json_init_v2((_a), (_b), (_c), (_d), (_e)) #define sudo_json_init(_a, _b, _c, _d, _e) sudo_json_init_v2((_a), (_b), (_c), (_d), (_e))
sudo_dso_public void sudo_json_free_v1(struct json_container *jsonc); sudo_dso_public void sudo_json_free_v1(struct json_container *jsonc);

View File

@@ -41,7 +41,7 @@ typedef int (*sudo_lbuf_output_t)(const char *);
#define LBUF_ESC_BLANK 0x02 #define LBUF_ESC_BLANK 0x02
#define LBUF_ESC_QUOTE 0x04 #define LBUF_ESC_QUOTE 0x04
sudo_dso_public void sudo_lbuf_init_v1(struct sudo_lbuf *lbuf, sudo_lbuf_output_t output, int indent, const char *continuation, int cols); sudo_dso_public void sudo_lbuf_init_v1(struct sudo_lbuf *lbuf, sudo_lbuf_output_t output, unsigned int indent, const char *continuation, int cols);
sudo_dso_public void sudo_lbuf_destroy_v1(struct sudo_lbuf *lbuf); sudo_dso_public void sudo_lbuf_destroy_v1(struct sudo_lbuf *lbuf);
sudo_dso_public bool sudo_lbuf_append_v1(struct sudo_lbuf *lbuf, const char *fmt, ...) sudo_printflike(2, 3); sudo_dso_public bool sudo_lbuf_append_v1(struct sudo_lbuf *lbuf, const char *fmt, ...) sudo_printflike(2, 3);
sudo_dso_public bool sudo_lbuf_append_esc_v1(struct sudo_lbuf *lbuf, int flags, const char *fmt, ...) sudo_printflike(3, 4); sudo_dso_public bool sudo_lbuf_append_esc_v1(struct sudo_lbuf *lbuf, int flags, const char *fmt, ...) sudo_printflike(3, 4);

View File

@@ -111,7 +111,7 @@ sudo_closefrom(int lowfd)
if (fcntl(lowfd, F_CLOSEM, 0) != -1) if (fcntl(lowfd, F_CLOSEM, 0) != -1)
return; return;
#elif defined(HAVE_CLOSE_RANGE) #elif defined(HAVE_CLOSE_RANGE)
if (close_range(lowfd, ~0U, 0) != -1) if (close_range((unsigned int)lowfd, ~0U, 0) != -1)
return; return;
#elif defined(HAVE_PROC_PIDINFO) #elif defined(HAVE_PROC_PIDINFO)
len = proc_pidinfo(pid, PROC_PIDLISTFDS, 0, NULL, 0); len = proc_pidinfo(pid, PROC_PIDLISTFDS, 0, NULL, 0);
@@ -166,7 +166,7 @@ sudo_closefrom(int lowfd)
struct dirent *dent; struct dirent *dent;
while ((dent = readdir(dirp)) != NULL) { while ((dent = readdir(dirp)) != NULL) {
const char *errstr; const char *errstr;
int fd = sudo_strtonum(dent->d_name, lowfd, INT_MAX, &errstr); int fd = (int)sudo_strtonum(dent->d_name, lowfd, INT_MAX, &errstr);
if (errstr == NULL && fd != dirfd(dirp)) { if (errstr == NULL && fd != dirfd(dirp)) {
(void)closefrom_close(fd); (void)closefrom_close(fd);
} }

View File

@@ -89,7 +89,7 @@ sudo_digest_alloc_v1(unsigned int digest_type)
debug_decl(sudo_digest_alloc, SUDO_DEBUG_UTIL); debug_decl(sudo_digest_alloc, SUDO_DEBUG_UTIL);
struct digest_function *func = NULL; struct digest_function *func = NULL;
struct sudo_digest *dig; struct sudo_digest *dig;
int i; unsigned int i;
for (i = 0; digest_functions[i].digest_len != 0; i++) { for (i = 0; digest_functions[i].digest_len != 0; i++) {
if (digest_type == i) { if (digest_type == i) {
@@ -134,7 +134,7 @@ int
sudo_digest_getlen_v1(unsigned int digest_type) sudo_digest_getlen_v1(unsigned int digest_type)
{ {
debug_decl(sudo_digest_getlen, SUDO_DEBUG_UTIL); debug_decl(sudo_digest_getlen, SUDO_DEBUG_UTIL);
int i; unsigned int i;
for (i = 0; digest_functions[i].digest_len != 0; i++) { for (i = 0; digest_functions[i].digest_len != 0; i++) {
if (digest_type == i) if (digest_type == i)

View File

@@ -25,8 +25,9 @@
#include <sys/resource.h> #include <sys/resource.h>
#include <stdlib.h> #include <limits.h>
#include <poll.h> #include <poll.h>
#include <stdlib.h>
#include <time.h> #include <time.h>
#include "sudo_compat.h" #include "sudo_compat.h"
@@ -35,6 +36,12 @@
#include "sudo_debug.h" #include "sudo_debug.h"
#include "sudo_event.h" #include "sudo_event.h"
#if defined(OPEN_MAX) && OPEN_MAX > 256
# define SUDO_OPEN_MAX OPEN_MAX
#else
# define SUDO_OPEN_MAX 256
#endif
int int
sudo_ev_base_alloc_impl(struct sudo_event_base *base) sudo_ev_base_alloc_impl(struct sudo_event_base *base)
{ {
@@ -43,7 +50,7 @@ sudo_ev_base_alloc_impl(struct sudo_event_base *base)
base->pfd_high = -1; base->pfd_high = -1;
base->pfd_max = 32; base->pfd_max = 32;
base->pfds = reallocarray(NULL, base->pfd_max, sizeof(struct pollfd)); base->pfds = reallocarray(NULL, (size_t)base->pfd_max, sizeof(struct pollfd));
if (base->pfds == NULL) { if (base->pfds == NULL) {
sudo_debug_printf(SUDO_DEBUG_ERROR|SUDO_DEBUG_LINENO, sudo_debug_printf(SUDO_DEBUG_ERROR|SUDO_DEBUG_LINENO,
"%s: unable to allocate %d pollfds", __func__, base->pfd_max); "%s: unable to allocate %d pollfds", __func__, base->pfd_max);
@@ -75,7 +82,9 @@ sudo_ev_add_impl(struct sudo_event_base *base, struct sudo_event *ev)
if (nofile_max == -1) { if (nofile_max == -1) {
struct rlimit rlim; struct rlimit rlim;
if (getrlimit(RLIMIT_NOFILE, &rlim) == 0) { if (getrlimit(RLIMIT_NOFILE, &rlim) == 0) {
nofile_max = rlim.rlim_cur; nofile_max = (int)rlim.rlim_cur;
} else {
nofile_max = SUDO_OPEN_MAX;
} }
} }
@@ -95,7 +104,7 @@ sudo_ev_add_impl(struct sudo_event_base *base, struct sudo_event *ev)
} }
sudo_debug_printf(SUDO_DEBUG_INFO|SUDO_DEBUG_LINENO, sudo_debug_printf(SUDO_DEBUG_INFO|SUDO_DEBUG_LINENO,
"%s: pfd_max %d -> %d", __func__, base->pfd_max, new_max); "%s: pfd_max %d -> %d", __func__, base->pfd_max, new_max);
pfds = reallocarray(base->pfds, new_max, sizeof(struct pollfd)); pfds = reallocarray(base->pfds, (size_t)new_max, sizeof(struct pollfd));
if (pfds == NULL) { if (pfds == NULL) {
sudo_debug_printf(SUDO_DEBUG_ERROR|SUDO_DEBUG_LINENO, sudo_debug_printf(SUDO_DEBUG_ERROR|SUDO_DEBUG_LINENO,
"%s: unable to allocate %d pollfds", __func__, new_max); "%s: unable to allocate %d pollfds", __func__, new_max);

View File

@@ -206,17 +206,17 @@ warning(const char *errstr, const char *fmt, va_list ap)
buflen = vsnprintf(static_buf, sizeof(static_buf), fmt, ap2); buflen = vsnprintf(static_buf, sizeof(static_buf), fmt, ap2);
va_end(ap2); va_end(ap2);
if (buflen >= ssizeof(static_buf)) { if (buflen >= ssizeof(static_buf)) {
buf = malloc(++buflen); /* Not enough room in static buf, allocate dynamically. */
if (buf != NULL) if (vasprintf(&buf, fmt, ap) == -1)
(void)vsnprintf(buf, buflen, fmt, ap);
else
buf = static_buf; buf = static_buf;
} }
if (buflen > 0) {
msgs[nmsgs].msg_type = SUDO_CONV_ERROR_MSG; msgs[nmsgs].msg_type = SUDO_CONV_ERROR_MSG;
msgs[nmsgs++].msg = ": "; msgs[nmsgs++].msg = ": ";
msgs[nmsgs].msg_type = SUDO_CONV_ERROR_MSG; msgs[nmsgs].msg_type = SUDO_CONV_ERROR_MSG;
msgs[nmsgs++].msg = buf; msgs[nmsgs++].msg = buf;
} }
}
if (errstr != NULL) { if (errstr != NULL) {
msgs[nmsgs].msg_type = SUDO_CONV_ERROR_MSG; msgs[nmsgs].msg_type = SUDO_CONV_ERROR_MSG;
msgs[nmsgs++].msg = ": "; msgs[nmsgs++].msg = ": ";

View File

@@ -146,7 +146,7 @@ sudo_getentropy(void *buf, size_t len)
return (ret); return (ret);
#ifdef HAVE_OPENSSL #ifdef HAVE_OPENSSL
if (RAND_bytes(buf, len) == 1) if (RAND_bytes(buf, (int)len) == 1)
return (0); return (0);
#endif #endif

View File

@@ -73,7 +73,8 @@ sudo_getgrouplist2_v1(const char *name, GETGROUPS_T basegid,
#endif #endif
int ngroups; int ngroups;
#ifndef HAVE_GETGROUPLIST_2 #ifndef HAVE_GETGROUPLIST_2
int grpsize, tries; long grpsize;
int tries;
#endif #endif
debug_decl(sudo_getgrouplist2, SUDO_DEBUG_UTIL); debug_decl(sudo_getgrouplist2, SUDO_DEBUG_UTIL);
@@ -87,8 +88,8 @@ sudo_getgrouplist2_v1(const char *name, GETGROUPS_T basegid,
*ngroupsp = ngroups; *ngroupsp = ngroups;
debug_return_int(0); debug_return_int(0);
#else #else
grpsize = (int)sysconf(_SC_NGROUPS_MAX); grpsize = sysconf(_SC_NGROUPS_MAX);
if (grpsize < 0) if (grpsize < 0 || grpsize > INT_MAX)
grpsize = NGROUPS_MAX; grpsize = NGROUPS_MAX;
grpsize++; /* include space for the primary gid */ grpsize++; /* include space for the primary gid */
/* /*
@@ -97,10 +98,10 @@ sudo_getgrouplist2_v1(const char *name, GETGROUPS_T basegid,
*/ */
for (tries = 0; tries < 10; tries++) { for (tries = 0; tries < 10; tries++) {
free(groups); free(groups);
groups = reallocarray(NULL, grpsize, sizeof(*groups)); groups = reallocarray(NULL, (size_t)grpsize, sizeof(*groups));
if (groups == NULL) if (groups == NULL)
debug_return_int(-1); debug_return_int(-1);
ngroups = grpsize; ngroups = (int)grpsize;
if (getgrouplist(name, basegid, groups, &ngroups) != -1) { if (getgrouplist(name, basegid, groups, &ngroups) != -1) {
*groupsp = groups; *groupsp = groups;
*ngroupsp = ngroups; *ngroupsp = ngroups;

View File

@@ -61,7 +61,7 @@ sudo_parse_gids_v1(const char *gidstr, const gid_t *basegid, GETGROUPS_T **gidsp
ngids++; ngids++;
/* Allocate and fill in array. */ /* Allocate and fill in array. */
if (ngids != 0) { if (ngids != 0) {
gids = reallocarray(NULL, ngids, sizeof(GETGROUPS_T)); gids = reallocarray(NULL, (size_t)ngids, sizeof(GETGROUPS_T));
if (gids == NULL) { if (gids == NULL) {
sudo_warnx(U_("%s: %s"), __func__, U_("unable to allocate memory")); sudo_warnx(U_("%s: %s"), __func__, U_("unable to allocate memory"));
debug_return_int(-1); debug_return_int(-1);

View File

@@ -109,7 +109,7 @@ json_append_buf(struct json_container *jsonc, const char *str)
} }
memcpy(jsonc->buf + jsonc->buflen, str, len); memcpy(jsonc->buf + jsonc->buflen, str, len);
jsonc->buflen += len; jsonc->buflen += (unsigned int)len;
jsonc->buf[jsonc->buflen] = '\0'; jsonc->buf[jsonc->buflen] = '\0';
debug_return_bool(true); debug_return_bool(true);
@@ -123,7 +123,7 @@ static bool
json_append_string(struct json_container *jsonc, const char *str) json_append_string(struct json_container *jsonc, const char *str)
{ {
const char hex[] = "0123456789abcdef"; const char hex[] = "0123456789abcdef";
unsigned char ch; char ch;
debug_decl(json_append_string, SUDO_DEBUG_UTIL); debug_decl(json_append_string, SUDO_DEBUG_UTIL);
if (!json_append_buf(jsonc, "\"")) if (!json_append_buf(jsonc, "\""))
@@ -157,7 +157,7 @@ json_append_string(struct json_container *jsonc, const char *str)
ch = 't'; ch = 't';
break; break;
default: default:
if (iscntrl(ch)) { if (iscntrl((unsigned char)ch)) {
/* Escape control characters like \u0000 */ /* Escape control characters like \u0000 */
*cp++ = '\\'; *cp++ = '\\';
*cp++ = 'u'; *cp++ = 'u';
@@ -180,8 +180,8 @@ json_append_string(struct json_container *jsonc, const char *str)
} }
bool bool
sudo_json_init_v2(struct json_container *jsonc, int indent, bool minimal, sudo_json_init_v2(struct json_container *jsonc, unsigned int indent,
bool memfatal, bool quiet) bool minimal, bool memfatal, bool quiet)
{ {
debug_decl(sudo_json_init, SUDO_DEBUG_UTIL); debug_decl(sudo_json_init, SUDO_DEBUG_UTIL);
@@ -208,8 +208,8 @@ sudo_json_init_v2(struct json_container *jsonc, int indent, bool minimal,
} }
bool bool
sudo_json_init_v1(struct json_container *jsonc, int indent, bool minimal, sudo_json_init_v1(struct json_container *jsonc, unsigned int indent,
bool memfatal) bool minimal, bool memfatal)
{ {
return sudo_json_init_v2(jsonc, indent, minimal, memfatal, false); return sudo_json_init_v2(jsonc, indent, minimal, memfatal, false);
} }

View File

@@ -36,14 +36,17 @@
void void
sudo_lbuf_init_v1(struct sudo_lbuf *lbuf, sudo_lbuf_output_t output, sudo_lbuf_init_v1(struct sudo_lbuf *lbuf, sudo_lbuf_output_t output,
int indent, const char *continuation, int cols) unsigned int indent, const char *continuation, int cols)
{ {
debug_decl(sudo_lbuf_init, SUDO_DEBUG_UTIL); debug_decl(sudo_lbuf_init, SUDO_DEBUG_UTIL);
if (cols < 0)
cols = 0;
lbuf->output = output; lbuf->output = output;
lbuf->continuation = continuation; lbuf->continuation = continuation;
lbuf->indent = indent; lbuf->indent = indent;
lbuf->cols = cols; lbuf->cols = (unsigned short)cols;
lbuf->error = 0; lbuf->error = 0;
lbuf->len = 0; lbuf->len = 0;
lbuf->size = 0; lbuf->size = 0;
@@ -80,7 +83,8 @@ sudo_lbuf_expand(struct sudo_lbuf *lbuf, unsigned int extra)
} }
if (lbuf->len + extra + 1 > lbuf->size) { if (lbuf->len + extra + 1 > lbuf->size) {
size_t new_size = sudo_pow2_roundup(lbuf->len + extra + 1); const size_t size = lbuf->len + extra + 1;
size_t new_size = sudo_pow2_roundup(size);
char *new_buf; char *new_buf;
if (new_size > UINT_MAX || new_size < lbuf->size) { if (new_size > UINT_MAX || new_size < lbuf->size) {
@@ -99,7 +103,7 @@ sudo_lbuf_expand(struct sudo_lbuf *lbuf, unsigned int extra)
debug_return_bool(false); debug_return_bool(false);
} }
lbuf->buf = new_buf; lbuf->buf = new_buf;
lbuf->size = new_size; lbuf->size = (unsigned int)new_size;
} }
debug_return_bool(true); debug_return_bool(true);
} }
@@ -109,23 +113,24 @@ sudo_lbuf_expand(struct sudo_lbuf *lbuf, unsigned int extra)
* in buf, which must have at least 6 bytes available. * in buf, which must have at least 6 bytes available.
* Returns the length of buf, not counting the terminating NUL byte. * Returns the length of buf, not counting the terminating NUL byte.
*/ */
static int static unsigned int
escape(unsigned char ch, char *buf) escape(char ch, char *buf)
{ {
const int len = ch < 0100 ? (ch < 010 ? 3 : 4) : 5; unsigned char uch = (unsigned char)ch;
const unsigned int len = uch < 0100 ? (uch < 010 ? 3 : 4) : 5;
/* Work backwards from the least significant digit to most significant. */ /* Work backwards from the least significant digit to most significant. */
switch (len) { switch (len) {
case 5: case 5:
buf[4] = (ch & 7) + '0'; buf[4] = (uch & 7) + '0';
ch >>= 3; uch >>= 3;
FALLTHROUGH; FALLTHROUGH;
case 4: case 4:
buf[3] = (ch & 7) + '0'; buf[3] = (uch & 7) + '0';
ch >>= 3; uch >>= 3;
FALLTHROUGH; FALLTHROUGH;
case 3: case 3:
buf[2] = (ch & 7) + '0'; buf[2] = (uch & 7) + '0';
buf[1] = '0'; buf[1] = '0';
buf[0] = '#'; buf[0] = '#';
break; break;
@@ -233,7 +238,7 @@ sudo_lbuf_append_quoted_v1(struct sudo_lbuf *lbuf, const char *set, const char *
if ((s = va_arg(ap, char *)) == NULL) if ((s = va_arg(ap, char *)) == NULL)
s = "(NULL)"; s = "(NULL)";
while ((cp = strpbrk(s, set)) != NULL) { while ((cp = strpbrk(s, set)) != NULL) {
len = (int)(cp - s); len = (unsigned int)(cp - s);
if (!sudo_lbuf_expand(lbuf, len + 2)) if (!sudo_lbuf_expand(lbuf, len + 2))
goto done; goto done;
memcpy(lbuf->buf + lbuf->len, s, len); memcpy(lbuf->buf + lbuf->len, s, len);
@@ -243,7 +248,7 @@ sudo_lbuf_append_quoted_v1(struct sudo_lbuf *lbuf, const char *set, const char *
s = cp + 1; s = cp + 1;
} }
if (*s != '\0') { if (*s != '\0') {
len = strlen(s); len = (unsigned int)strlen(s);
if (!sudo_lbuf_expand(lbuf, len)) if (!sudo_lbuf_expand(lbuf, len))
goto done; goto done;
memcpy(lbuf->buf + lbuf->len, s, len); memcpy(lbuf->buf + lbuf->len, s, len);
@@ -280,7 +285,7 @@ sudo_lbuf_append_v1(struct sudo_lbuf *lbuf, const char *fmt, ...)
bool ret = false; bool ret = false;
va_list ap; va_list ap;
const char *s; const char *s;
size_t len; unsigned int len;
debug_decl(sudo_lbuf_append, SUDO_DEBUG_UTIL); debug_decl(sudo_lbuf_append, SUDO_DEBUG_UTIL);
if (sudo_lbuf_error(lbuf)) if (sudo_lbuf_error(lbuf))
@@ -298,7 +303,7 @@ sudo_lbuf_append_v1(struct sudo_lbuf *lbuf, const char *fmt, ...)
if (num_end[0] == '$' && num_end[1] == 's' && num_end > num_start) { if (num_end[0] == '$' && num_end[1] == 's' && num_end > num_start) {
/* Convert the numeric part to an integer */ /* Convert the numeric part to an integer */
char numbuf[(((sizeof(int) * 8) + 2) / 3) + 2]; char numbuf[(((sizeof(int) * 8) + 2) / 3) + 2];
len = num_end - num_start; len = (unsigned int)(num_end - num_start);
if (len >= sizeof(numbuf)) { if (len >= sizeof(numbuf)) {
errno = EINVAL; errno = EINVAL;
sudo_debug_printf(SUDO_DEBUG_ERROR|SUDO_DEBUG_LINENO, sudo_debug_printf(SUDO_DEBUG_ERROR|SUDO_DEBUG_LINENO,
@@ -317,7 +322,7 @@ sudo_lbuf_append_v1(struct sudo_lbuf *lbuf, const char *fmt, ...)
} }
if ((s = va_arg(arg_copy, char *)) == NULL) if ((s = va_arg(arg_copy, char *)) == NULL)
s = "(NULL)"; s = "(NULL)";
len = strlen(s); len = (unsigned int)strlen(s);
if (!sudo_lbuf_expand(lbuf, len)) { if (!sudo_lbuf_expand(lbuf, len)) {
va_end(arg_copy); va_end(arg_copy);
goto done; goto done;
@@ -333,7 +338,7 @@ sudo_lbuf_append_v1(struct sudo_lbuf *lbuf, const char *fmt, ...)
if (fmt[0] == '%' && fmt[1] == 's') { if (fmt[0] == '%' && fmt[1] == 's') {
if ((s = va_arg(ap, char *)) == NULL) if ((s = va_arg(ap, char *)) == NULL)
s = "(NULL)"; s = "(NULL)";
len = strlen(s); len = (unsigned int)strlen(s);
if (!sudo_lbuf_expand(lbuf, len)) if (!sudo_lbuf_expand(lbuf, len))
goto done; goto done;
memcpy(lbuf->buf + lbuf->len, s, len); memcpy(lbuf->buf + lbuf->len, s, len);
@@ -383,14 +388,14 @@ sudo_lbuf_println(struct sudo_lbuf *lbuf, char *line, size_t len)
have = lbuf->cols; have = lbuf->cols;
while (cp != NULL && *cp != '\0') { while (cp != NULL && *cp != '\0') {
char *ep = NULL; char *ep = NULL;
size_t need = len - (cp - line); size_t need = len - (size_t)(cp - line);
if (need > have) { if (need > have) {
have -= contlen; /* subtract for continuation char */ have -= contlen; /* subtract for continuation char */
if ((ep = memrchr(cp, ' ', have)) == NULL) if ((ep = memrchr(cp, ' ', have)) == NULL)
ep = memchr(cp + have, ' ', need - have); ep = memchr(cp + have, ' ', need - have);
if (ep != NULL) if (ep != NULL)
need = (ep - cp); need = (size_t)(ep - cp);
} }
if (cp != line) { if (cp != line) {
if (is_comment) { if (is_comment) {
@@ -459,7 +464,7 @@ sudo_lbuf_print_v1(struct sudo_lbuf *lbuf)
lbuf->output("\n"); lbuf->output("\n");
cp++; cp++;
} else { } else {
len = lbuf->len - (cp - lbuf->buf); len = lbuf->len - (size_t)(cp - lbuf->buf);
if ((ep = memchr(cp, '\n', len)) != NULL) if ((ep = memchr(cp, '\n', len)) != NULL)
len = (size_t)(ep - cp); len = (size_t)(ep - cp);
if (len) if (len)

View File

@@ -142,7 +142,7 @@ reopen:
goto bad; goto bad;
} }
/* Make sure the path we created is still a directory. */ /* Make sure the path we created is still a directory. */
if (!is_dir(dfd, path, ep - path, quiet)) { if (!is_dir(dfd, path, (int)(ep - path), quiet)) {
close(dfd); close(dfd);
goto bad; goto bad;
} }
@@ -164,7 +164,7 @@ reopen:
} }
} else { } else {
/* Already exists, make sure it is a directory. */ /* Already exists, make sure it is a directory. */
if (!is_dir(dfd, path, ep - path, quiet)) { if (!is_dir(dfd, path, (int)(ep - path), quiet)) {
close(dfd); close(dfd);
goto bad; goto bad;
} }

View File

@@ -48,8 +48,8 @@
ssize_t ssize_t
sudo_parseln_v2(char **bufp, size_t *bufsizep, unsigned int *lineno, FILE *fp, int flags) sudo_parseln_v2(char **bufp, size_t *bufsizep, unsigned int *lineno, FILE *fp, int flags)
{ {
size_t linesize = 0, total = 0; ssize_t len, total = 0;
ssize_t len; size_t bufsize, linesize = 0;
char *cp, *line = NULL; char *cp, *line = NULL;
bool continued, comment; bool continued, comment;
debug_decl(sudo_parseln, SUDO_DEBUG_UTIL); debug_decl(sudo_parseln, SUDO_DEBUG_UTIL);
@@ -90,12 +90,12 @@ sudo_parseln_v2(char **bufp, size_t *bufsizep, unsigned int *lineno, FILE *fp, i
for (cp = line; isblank((unsigned char)*cp); cp++) for (cp = line; isblank((unsigned char)*cp); cp++)
len--; len--;
if (*bufp == NULL || total + len >= *bufsizep) { bufsize = (size_t)(total + len + 1);
const size_t size = total + len + 1; if (*bufp == NULL || bufsize > *bufsizep) {
const size_t newsize = sudo_pow2_roundup(size); const size_t newsize = sudo_pow2_roundup(bufsize);
void *newbuf; void *newbuf;
if (newsize < size) { if (newsize < bufsize) {
/* overflow */ /* overflow */
errno = ENOMEM; errno = ENOMEM;
sudo_debug_printf(SUDO_DEBUG_ERROR|SUDO_DEBUG_LINENO, sudo_debug_printf(SUDO_DEBUG_ERROR|SUDO_DEBUG_LINENO,
@@ -114,7 +114,7 @@ sudo_parseln_v2(char **bufp, size_t *bufsizep, unsigned int *lineno, FILE *fp, i
*bufp = newbuf; *bufp = newbuf;
*bufsizep = newsize; *bufsizep = newsize;
} }
memcpy(*bufp + total, cp, len + 1); memcpy(*bufp + total, cp, (size_t)(len + 1));
total += len; total += len;
} while (continued); } while (continued);
free(line); free(line);

View File

@@ -46,15 +46,15 @@ static int
parse_num(const char *str, char **endp) parse_num(const char *str, char **endp)
{ {
debug_decl(check_pattern, SUDO_DEBUG_UTIL); debug_decl(check_pattern, SUDO_DEBUG_UTIL);
const unsigned int lastval = INT_MAX / 10; const int lastval = INT_MAX / 10;
const unsigned int remainder = INT_MAX % 10; const int remainder = INT_MAX % 10;
unsigned int result = 0; int result = 0;
unsigned char ch; char ch;
while ((ch = *str++) != '\0') { while ((ch = *str++) != '\0') {
if (ch == '\\' && isdigit((unsigned int)str[0])) if (ch == '\\' && isdigit((unsigned char)str[0]))
ch = *str++; ch = *str++;
else if (!isdigit(ch)) else if (!isdigit((unsigned char)ch))
break; break;
ch -= '0'; ch -= '0';
if (result > lastval || (result == lastval && ch > remainder)) { if (result > lastval || (result == lastval && ch > remainder)) {

View File

@@ -73,7 +73,7 @@ LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
struct sudo_conf_debug *debug_spec; struct sudo_conf_debug *debug_spec;
struct sudo_debug_file *debug_file; struct sudo_debug_file *debug_file;
struct plugin_info *info; struct plugin_info *info;
size_t nwritten; ssize_t nwritten;
int fd; int fd;
initprogname("fuzz_sudo_conf"); initprogname("fuzz_sudo_conf");
@@ -85,7 +85,7 @@ LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
if (fd == -1) if (fd == -1)
return 0; return 0;
nwritten = write(fd, data, size); nwritten = write(fd, data, size);
if (nwritten != size) { if (nwritten == -1) {
close(fd); close(fd);
return 0; return 0;
} }

View File

@@ -46,7 +46,7 @@ main(int argc, char *argv[])
/* Build up test data. */ /* Build up test data. */
ntests = 256 + 256 + 3; ntests = 256 + 256 + 3;
test_data = calloc(sizeof(*test_data), ntests); test_data = calloc(sizeof(*test_data), (size_t)ntests);
for (i = 0; i < 256; i++) { for (i = 0; i < 256; i++) {
/* lower case */ /* lower case */
test_data[i].value = i; test_data[i].value = i;

View File

@@ -52,7 +52,7 @@ sudo_dso_public int main(int argc, char *argv[]);
*/ */
static int static int
check(int fd, char const *kind, char const *path, char const *prefix, check(int fd, char const *kind, char const *path, char const *prefix,
size_t plen, char const *suffix, size_t slen, int tlen) size_t plen, char const *suffix, size_t slen, size_t tlen)
{ {
struct stat sb, fsb; struct stat sb, fsb;
char const *p; char const *p;
@@ -87,7 +87,7 @@ check(int fd, char const *kind, char const *path, char const *prefix,
} }
static void static void
try_mkdtemp(char *p, char const *prefix, int len) try_mkdtemp(char *p, char const *prefix, size_t len)
{ {
size_t plen = strlen(prefix); size_t plen = strlen(prefix);
int fd, tries, ok; int fd, tries, ok;
@@ -106,7 +106,7 @@ try_mkdtemp(char *p, char const *prefix, int len)
} }
static void static void
try_mkstemps(char *p, char const *prefix, int len, char const *suffix) try_mkstemps(char *p, char const *prefix, size_t len, char const *suffix)
{ {
size_t plen = strlen(prefix); size_t plen = strlen(prefix);
size_t slen = strlen(suffix); size_t slen = strlen(suffix);
@@ -116,7 +116,7 @@ try_mkstemps(char *p, char const *prefix, int len, char const *suffix)
memcpy(p, prefix, plen); memcpy(p, prefix, plen);
memset(p + plen, 'X', len); memset(p + plen, 'X', len);
memcpy(p + plen + len, suffix, slen + 1); memcpy(p + plen + len, suffix, slen + 1);
fd = mkstemps(p, slen); fd = mkstemps(p, (int)slen);
ok = check(fd, "mkstemp", p, prefix, plen, suffix, slen, len); ok = check(fd, "mkstemp", p, prefix, plen, suffix, slen, len);
close(fd); close(fd);
unlink(p); unlink(p);
@@ -131,9 +131,9 @@ main(int argc, char *argv[])
{ {
char cwd[PATH_MAX + 1]; char cwd[PATH_MAX + 1];
char *p; char *p;
size_t clen; size_t clen, i;
long pg; size_t pg;
int ch, i; int ch;
initprogname(argc > 0 ? argv[0] : "mktemp_test"); initprogname(argc > 0 ? argv[0] : "mktemp_test");
@@ -150,7 +150,7 @@ main(int argc, char *argv[])
argc -= optind; argc -= optind;
argv += optind; argv += optind;
pg = sysconf(_SC_PAGESIZE); pg = (size_t)sysconf(_SC_PAGESIZE);
if (getcwd(cwd, sizeof cwd - 1) == NULL) if (getcwd(cwd, sizeof cwd - 1) == NULL)
sudo_fatal("getcwd"); sudo_fatal("getcwd");
clen = strlen(cwd); clen = strlen(cwd);
@@ -159,10 +159,10 @@ main(int argc, char *argv[])
#ifdef MAP_ANON #ifdef MAP_ANON
p = mmap(NULL, pg * 3, PROT_READ | PROT_WRITE, MAP_PRIVATE|MAP_ANON, -1, 0); p = mmap(NULL, pg * 3, PROT_READ | PROT_WRITE, MAP_PRIVATE|MAP_ANON, -1, 0);
#else #else
i = open("/dev/zero", O_RDWR); ch = open("/dev/zero", O_RDWR);
if (i == -1) if (ch == -1)
sudo_fatal("/dev/zero"); sudo_fatal("/dev/zero");
p = mmap(NULL, pg * 3, PROT_READ | PROT_WRITE, MAP_PRIVATE, i, 0); p = mmap(NULL, pg * 3, PROT_READ | PROT_WRITE, MAP_PRIVATE, ch, 0);
#endif #endif
if (p == MAP_FAILED) if (p == MAP_FAILED)
sudo_fatal("mmap"); sudo_fatal("mmap");
@@ -170,8 +170,7 @@ main(int argc, char *argv[])
sudo_fatal("mprotect"); sudo_fatal("mprotect");
p += pg; p += pg;
i = MAX_TEMPLATE_LEN + 1; for (i = MAX_TEMPLATE_LEN; i != 0; i--) {
while (i-- > 0) {
/* try first at the start of a page, no prefix */ /* try first at the start of a page, no prefix */
try_mkdtemp(p, "", i); try_mkdtemp(p, "", i);
/* now at the end of the page, no prefix */ /* now at the end of the page, no prefix */

View File

@@ -43,7 +43,7 @@ static struct strtoidx_data {
{ "4294967295", 0, NULL, NULL, EINVAL }, { "4294967295", 0, NULL, NULL, EINVAL },
{ "4294967296", 0, NULL, NULL, ERANGE }, { "4294967296", 0, NULL, NULL, ERANGE },
{ "-2147483649", 0, NULL, NULL, ERANGE }, { "-2147483649", 0, NULL, NULL, ERANGE },
{ "-2", -2, NULL, NULL, 0 }, { "-2", (id_t)-2, NULL, NULL, 0 },
#if SIZEOF_ID_T != SIZEOF_LONG_LONG #if SIZEOF_ID_T != SIZEOF_LONG_LONG
{ "-2", (id_t)4294967294U, NULL, NULL, 0 }, { "-2", (id_t)4294967294U, NULL, NULL, 0 },
#endif #endif

View File

@@ -116,7 +116,7 @@ sudo_secure_fd_v1(int fd, unsigned int type, uid_t uid, gid_t gid,
* Sets error to SUDO_PATH_SECURE on success, and a value < 0 on failure. * Sets error to SUDO_PATH_SECURE on success, and a value < 0 on failure.
*/ */
static int static int
sudo_secure_open(const char *path, int type, uid_t uid, gid_t gid, sudo_secure_open(const char *path, unsigned int type, uid_t uid, gid_t gid,
struct stat *sb, int *error) struct stat *sb, int *error)
{ {
struct stat stat_buf; struct stat stat_buf;
@@ -179,7 +179,7 @@ sudo_open_conf_path_v1(const char *path, char *name, size_t namesize,
for (cp = sudo_strsplit(path, path_end, ":", &ep); for (cp = sudo_strsplit(path, path_end, ":", &ep);
cp != NULL; cp = sudo_strsplit(NULL, path_end, ":", &ep)) { cp != NULL; cp = sudo_strsplit(NULL, path_end, ":", &ep)) {
const size_t len = ep - cp; const size_t len = (size_t)(ep - cp);
if (len >= namesize) { if (len >= namesize) {
/* We always set name, even on error. */ /* We always set name, even on error. */
memcpy(name, cp, namesize - 1); memcpy(name, cp, namesize - 1);

View File

@@ -36,13 +36,17 @@
int int
sudo_setgroups_v1(int ngids, const GETGROUPS_T *gids) sudo_setgroups_v1(int ngids, const GETGROUPS_T *gids)
{ {
int maxgids, ret; long maxgids;
int ret;
debug_decl(sudo_setgroups, SUDO_DEBUG_UTIL); debug_decl(sudo_setgroups, SUDO_DEBUG_UTIL);
if (ngids < 0)
debug_return_int(-1);
ret = setgroups(ngids, (GETGROUPS_T *)gids); ret = setgroups(ngids, (GETGROUPS_T *)gids);
if (ret == -1 && errno == EINVAL) { if (ret == -1 && errno == EINVAL) {
/* Too many groups, try again with fewer. */ /* Too many groups, try again with fewer. */
maxgids = (int)sysconf(_SC_NGROUPS_MAX); maxgids = sysconf(_SC_NGROUPS_MAX);
if (maxgids == -1) if (maxgids == -1)
maxgids = NGROUPS_MAX; maxgids = NGROUPS_MAX;
if (ngids > maxgids) if (ngids > maxgids)

View File

@@ -89,7 +89,7 @@ sudo_sig2str(int signo, char *signame)
if (islower((unsigned char)signame[0])) { if (islower((unsigned char)signame[0])) {
size_t i; size_t i;
for (i = 0; signame[i] != '\0'; i++) for (i = 0; signame[i] != '\0'; i++)
signame[i] = toupper((unsigned char)signame[i]); signame[i] = (char)toupper((unsigned char)signame[i]);
} }
return 0; return 0;
} }

View File

@@ -94,7 +94,7 @@ sudo_str2sig(const char *signame, int *result)
/* Could be a signal number encoded as a string. */ /* Could be a signal number encoded as a string. */
if (isdigit((unsigned char)signame[0])) { if (isdigit((unsigned char)signame[0])) {
signo = sudo_strtonum(signame, 0, NSIG - 1, &errstr); signo = (int)sudo_strtonum(signame, 0, NSIG - 1, &errstr);
if (errstr != NULL) if (errstr != NULL)
return -1; return -1;
*result = signo; *result = signo;

View File

@@ -50,7 +50,7 @@ sudo_strlcat(char *dst, const char *src, size_t dsize)
/* Find the end of dst and adjust bytes left but don't go past end. */ /* Find the end of dst and adjust bytes left but don't go past end. */
while (n-- != 0 && *dst != '\0') while (n-- != 0 && *dst != '\0')
dst++; dst++;
dlen = dst - odst; dlen = (size_t)(dst - odst);
n = dsize - dlen; n = dsize - dlen;
if (n-- == 0) if (n-- == 0)
@@ -64,6 +64,6 @@ sudo_strlcat(char *dst, const char *src, size_t dsize)
} }
*dst = '\0'; *dst = '\0';
return(dlen + (src - osrc)); /* count does not include NUL */ return(dlen + (size_t)(src - osrc)); /* count does not include NUL */
} }
#endif /* HAVE_STRLCAT */ #endif /* HAVE_STRLCAT */

View File

@@ -59,6 +59,6 @@ sudo_strlcpy(char *dst, const char *src, size_t dsize)
continue; continue;
} }
return(src - osrc - 1); /* count does not include NUL */ return((size_t)(src - osrc) - 1); /* count does not include NUL */
} }
#endif /* HAVE_STRLCPY */ #endif /* HAVE_STRLCPY */

View File

@@ -74,7 +74,7 @@ sudo_strtoidx_v1(const char *p, const char *sep, char **endp, const char **errst
id_t ret; id_t ret;
debug_decl(sudo_strtoid, SUDO_DEBUG_UTIL); debug_decl(sudo_strtoid, SUDO_DEBUG_UTIL);
ret = sudo_strtonumx(p, INT_MIN, UINT_MAX, &ep, &errstr); ret = (id_t)sudo_strtonumx(p, INT_MIN, UINT_MAX, &ep, &errstr);
if (errstr == NULL) { if (errstr == NULL) {
/* /*
* Disallow id -1 (UINT_MAX), which means "no change" * Disallow id -1 (UINT_MAX), which means "no change"

View File

@@ -49,9 +49,8 @@ sudo_strtonumx(const char *str, long long minval, long long maxval, char **endp,
enum strtonum_err errval = STN_INITIAL; enum strtonum_err errval = STN_INITIAL;
long long lastval, result = 0; long long lastval, result = 0;
const char *cp = str; const char *cp = str;
unsigned char ch;
int remainder; int remainder;
char sign; char ch, sign;
if (minval > maxval) { if (minval > maxval) {
errval = STN_INVALID; errval = STN_INVALID;
@@ -61,7 +60,7 @@ sudo_strtonumx(const char *str, long long minval, long long maxval, char **endp,
/* Trim leading space and check sign, if any. */ /* Trim leading space and check sign, if any. */
do { do {
ch = *cp++; ch = *cp++;
} while (isspace(ch)); } while (isspace((unsigned char)ch));
switch (ch) { switch (ch) {
case '-': case '-':
sign = '-'; sign = '-';
@@ -86,20 +85,20 @@ sudo_strtonumx(const char *str, long long minval, long long maxval, char **endp,
*/ */
if (sign == '-') { if (sign == '-') {
lastval = minval / 10; lastval = minval / 10;
remainder = -(minval % 10); remainder = -(int)(minval % 10);
if (remainder < 0) { if (remainder < 0) {
lastval += 1; lastval += 1;
remainder += 10; remainder += 10;
} }
for (;; ch = *cp++) { for (;; ch = *cp++) {
if (!isdigit(ch)) if (!isdigit((unsigned char)ch))
break; break;
ch -= '0'; ch -= '0';
if (result < lastval || (result == lastval && ch > remainder)) { if (result < lastval || (result == lastval && ch > remainder)) {
/* Skip remaining digits. */ /* Skip remaining digits. */
do { do {
ch = *cp++; ch = *cp++;
} while (isdigit(ch)); } while (isdigit((unsigned char)ch));
errval = STN_TOOSMALL; errval = STN_TOOSMALL;
break; break;
} else { } else {
@@ -112,16 +111,16 @@ sudo_strtonumx(const char *str, long long minval, long long maxval, char **endp,
errval = STN_TOOBIG; errval = STN_TOOBIG;
} else { } else {
lastval = maxval / 10; lastval = maxval / 10;
remainder = maxval % 10; remainder = (int)(maxval % 10);
for (;; ch = *cp++) { for (;; ch = *cp++) {
if (!isdigit(ch)) if (!isdigit((unsigned char)ch))
break; break;
ch -= '0'; ch -= '0';
if (result > lastval || (result == lastval && ch > remainder)) { if (result > lastval || (result == lastval && ch > remainder)) {
/* Skip remaining digits. */ /* Skip remaining digits. */
do { do {
ch = *cp++; ch = *cp++;
} while (isdigit(ch)); } while (isdigit((unsigned char)ch));
errval = STN_TOOBIG; errval = STN_TOOBIG;
break; break;
} else { } else {

View File

@@ -432,7 +432,7 @@ set_var_max_groups(const char *strval, const char *conf_file,
int max_groups; int max_groups;
debug_decl(set_var_max_groups, SUDO_DEBUG_UTIL); debug_decl(set_var_max_groups, SUDO_DEBUG_UTIL);
max_groups = sudo_strtonum(strval, 1, 1024, NULL); max_groups = (int)sudo_strtonum(strval, 1, 1024, NULL);
if (max_groups <= 0) { if (max_groups <= 0) {
sudo_warnx(U_("invalid max groups \"%s\" in %s, line %u"), strval, sudo_warnx(U_("invalid max groups \"%s\" in %s, line %u"), strval,
conf_file, lineno); conf_file, lineno);
@@ -672,7 +672,7 @@ sudo_conf_read_v1(const char *path, int conf_types)
/* _PATH_SUDO_CONF is a colon-separated list of path. */ /* _PATH_SUDO_CONF is a colon-separated list of path. */
fd = sudo_open_conf_path(_PATH_SUDO_CONF, conf_file, fd = sudo_open_conf_path(_PATH_SUDO_CONF, conf_file,
sizeof(conf_file), NULL); sizeof(conf_file), NULL);
error = sudo_secure_fd(fd, S_IFREG, ROOT_UID, -1, &sb); error = sudo_secure_fd(fd, S_IFREG, ROOT_UID, (gid_t)-1, &sb);
switch (error) { switch (error) {
case SUDO_PATH_SECURE: case SUDO_PATH_SECURE:
/* OK! */ /* OK! */

View File

@@ -201,10 +201,10 @@ sudo_debug_new_output(struct sudo_debug_instance *instance,
const int new_size = round_nfds(output->fd + 1) / NBBY; const int new_size = round_nfds(output->fd + 1) / NBBY;
unsigned char *new_fds; unsigned char *new_fds;
new_fds = realloc(sudo_debug_fds, new_size); new_fds = realloc(sudo_debug_fds, (size_t)new_size);
if (new_fds == NULL) if (new_fds == NULL)
goto oom; goto oom;
memset(new_fds + old_size, 0, new_size - old_size); memset(new_fds + old_size, 0, (size_t)(new_size - old_size));
sudo_debug_fds = new_fds; sudo_debug_fds = new_fds;
sudo_debug_fds_size = new_size * NBBY; sudo_debug_fds_size = new_size * NBBY;
} }

View File

@@ -65,11 +65,11 @@ sudo_get_ttysize_v2(int fd, int *rowp, int *colp)
/* Fall back on $LINES and $COLUMNS. */ /* Fall back on $LINES and $COLUMNS. */
if ((p = getenv("LINES")) == NULL || if ((p = getenv("LINES")) == NULL ||
(*rowp = sudo_strtonum(p, 1, INT_MAX, NULL)) <= 0) { (*rowp = (int)sudo_strtonum(p, 1, INT_MAX, NULL)) <= 0) {
*rowp = 24; *rowp = 24;
} }
if ((p = getenv("COLUMNS")) == NULL || if ((p = getenv("COLUMNS")) == NULL ||
(*colp = sudo_strtonum(p, 1, INT_MAX, NULL)) <= 0) { (*colp = (int)sudo_strtonum(p, 1, INT_MAX, NULL)) <= 0) {
*colp = 80; *colp = 80;
} }
} }