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;
};
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_v2(struct json_container *jsonc, int indent, bool minimal, bool memfatal, bool quiet);
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, 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))
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_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 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);

View File

@@ -111,7 +111,7 @@ sudo_closefrom(int lowfd)
if (fcntl(lowfd, F_CLOSEM, 0) != -1)
return;
#elif defined(HAVE_CLOSE_RANGE)
if (close_range(lowfd, ~0U, 0) != -1)
if (close_range((unsigned int)lowfd, ~0U, 0) != -1)
return;
#elif defined(HAVE_PROC_PIDINFO)
len = proc_pidinfo(pid, PROC_PIDLISTFDS, 0, NULL, 0);
@@ -166,7 +166,7 @@ sudo_closefrom(int lowfd)
struct dirent *dent;
while ((dent = readdir(dirp)) != NULL) {
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)) {
(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);
struct digest_function *func = NULL;
struct sudo_digest *dig;
int i;
unsigned int i;
for (i = 0; digest_functions[i].digest_len != 0; i++) {
if (digest_type == i) {
@@ -134,7 +134,7 @@ int
sudo_digest_getlen_v1(unsigned int digest_type)
{
debug_decl(sudo_digest_getlen, SUDO_DEBUG_UTIL);
int i;
unsigned int i;
for (i = 0; digest_functions[i].digest_len != 0; i++) {
if (digest_type == i)

View File

@@ -25,8 +25,9 @@
#include <sys/resource.h>
#include <stdlib.h>
#include <limits.h>
#include <poll.h>
#include <stdlib.h>
#include <time.h>
#include "sudo_compat.h"
@@ -35,6 +36,12 @@
#include "sudo_debug.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
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_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) {
sudo_debug_printf(SUDO_DEBUG_ERROR|SUDO_DEBUG_LINENO,
"%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) {
struct rlimit rlim;
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,
"%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) {
sudo_debug_printf(SUDO_DEBUG_ERROR|SUDO_DEBUG_LINENO,
"%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);
va_end(ap2);
if (buflen >= ssizeof(static_buf)) {
buf = malloc(++buflen);
if (buf != NULL)
(void)vsnprintf(buf, buflen, fmt, ap);
else
/* Not enough room in static buf, allocate dynamically. */
if (vasprintf(&buf, fmt, ap) == -1)
buf = static_buf;
}
if (buflen > 0) {
msgs[nmsgs].msg_type = SUDO_CONV_ERROR_MSG;
msgs[nmsgs++].msg = ": ";
msgs[nmsgs].msg_type = SUDO_CONV_ERROR_MSG;
msgs[nmsgs++].msg = buf;
}
}
if (errstr != NULL) {
msgs[nmsgs].msg_type = SUDO_CONV_ERROR_MSG;
msgs[nmsgs++].msg = ": ";

View File

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

View File

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

View File

@@ -61,7 +61,7 @@ sudo_parse_gids_v1(const char *gidstr, const gid_t *basegid, GETGROUPS_T **gidsp
ngids++;
/* Allocate and fill in array. */
if (ngids != 0) {
gids = reallocarray(NULL, ngids, sizeof(GETGROUPS_T));
gids = reallocarray(NULL, (size_t)ngids, sizeof(GETGROUPS_T));
if (gids == NULL) {
sudo_warnx(U_("%s: %s"), __func__, U_("unable to allocate memory"));
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);
jsonc->buflen += len;
jsonc->buflen += (unsigned int)len;
jsonc->buf[jsonc->buflen] = '\0';
debug_return_bool(true);
@@ -123,7 +123,7 @@ static bool
json_append_string(struct json_container *jsonc, const char *str)
{
const char hex[] = "0123456789abcdef";
unsigned char ch;
char ch;
debug_decl(json_append_string, SUDO_DEBUG_UTIL);
if (!json_append_buf(jsonc, "\""))
@@ -157,7 +157,7 @@ json_append_string(struct json_container *jsonc, const char *str)
ch = 't';
break;
default:
if (iscntrl(ch)) {
if (iscntrl((unsigned char)ch)) {
/* Escape control characters like \u0000 */
*cp++ = '\\';
*cp++ = 'u';
@@ -180,8 +180,8 @@ json_append_string(struct json_container *jsonc, const char *str)
}
bool
sudo_json_init_v2(struct json_container *jsonc, int indent, bool minimal,
bool memfatal, bool quiet)
sudo_json_init_v2(struct json_container *jsonc, unsigned int indent,
bool minimal, bool memfatal, bool quiet)
{
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
sudo_json_init_v1(struct json_container *jsonc, int indent, bool minimal,
bool memfatal)
sudo_json_init_v1(struct json_container *jsonc, unsigned int indent,
bool minimal, bool memfatal)
{
return sudo_json_init_v2(jsonc, indent, minimal, memfatal, false);
}

View File

@@ -36,14 +36,17 @@
void
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);
if (cols < 0)
cols = 0;
lbuf->output = output;
lbuf->continuation = continuation;
lbuf->indent = indent;
lbuf->cols = cols;
lbuf->cols = (unsigned short)cols;
lbuf->error = 0;
lbuf->len = 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) {
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;
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);
}
lbuf->buf = new_buf;
lbuf->size = new_size;
lbuf->size = (unsigned int)new_size;
}
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.
* Returns the length of buf, not counting the terminating NUL byte.
*/
static int
escape(unsigned char ch, char *buf)
static unsigned int
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. */
switch (len) {
case 5:
buf[4] = (ch & 7) + '0';
ch >>= 3;
buf[4] = (uch & 7) + '0';
uch >>= 3;
FALLTHROUGH;
case 4:
buf[3] = (ch & 7) + '0';
ch >>= 3;
buf[3] = (uch & 7) + '0';
uch >>= 3;
FALLTHROUGH;
case 3:
buf[2] = (ch & 7) + '0';
buf[2] = (uch & 7) + '0';
buf[1] = '0';
buf[0] = '#';
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)
s = "(NULL)";
while ((cp = strpbrk(s, set)) != NULL) {
len = (int)(cp - s);
len = (unsigned int)(cp - s);
if (!sudo_lbuf_expand(lbuf, len + 2))
goto done;
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;
}
if (*s != '\0') {
len = strlen(s);
len = (unsigned int)strlen(s);
if (!sudo_lbuf_expand(lbuf, len))
goto done;
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;
va_list ap;
const char *s;
size_t len;
unsigned int len;
debug_decl(sudo_lbuf_append, SUDO_DEBUG_UTIL);
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) {
/* Convert the numeric part to an integer */
char numbuf[(((sizeof(int) * 8) + 2) / 3) + 2];
len = num_end - num_start;
len = (unsigned int)(num_end - num_start);
if (len >= sizeof(numbuf)) {
errno = EINVAL;
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)
s = "(NULL)";
len = strlen(s);
len = (unsigned int)strlen(s);
if (!sudo_lbuf_expand(lbuf, len)) {
va_end(arg_copy);
goto done;
@@ -333,7 +338,7 @@ sudo_lbuf_append_v1(struct sudo_lbuf *lbuf, const char *fmt, ...)
if (fmt[0] == '%' && fmt[1] == 's') {
if ((s = va_arg(ap, char *)) == NULL)
s = "(NULL)";
len = strlen(s);
len = (unsigned int)strlen(s);
if (!sudo_lbuf_expand(lbuf, len))
goto done;
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;
while (cp != NULL && *cp != '\0') {
char *ep = NULL;
size_t need = len - (cp - line);
size_t need = len - (size_t)(cp - line);
if (need > have) {
have -= contlen; /* subtract for continuation char */
if ((ep = memrchr(cp, ' ', have)) == NULL)
ep = memchr(cp + have, ' ', need - have);
if (ep != NULL)
need = (ep - cp);
need = (size_t)(ep - cp);
}
if (cp != line) {
if (is_comment) {
@@ -459,7 +464,7 @@ sudo_lbuf_print_v1(struct sudo_lbuf *lbuf)
lbuf->output("\n");
cp++;
} else {
len = lbuf->len - (cp - lbuf->buf);
len = lbuf->len - (size_t)(cp - lbuf->buf);
if ((ep = memchr(cp, '\n', len)) != NULL)
len = (size_t)(ep - cp);
if (len)

View File

@@ -142,7 +142,7 @@ reopen:
goto bad;
}
/* 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);
goto bad;
}
@@ -164,7 +164,7 @@ reopen:
}
} else {
/* 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);
goto bad;
}

View File

@@ -48,8 +48,8 @@
ssize_t
sudo_parseln_v2(char **bufp, size_t *bufsizep, unsigned int *lineno, FILE *fp, int flags)
{
size_t linesize = 0, total = 0;
ssize_t len;
ssize_t len, total = 0;
size_t bufsize, linesize = 0;
char *cp, *line = NULL;
bool continued, comment;
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++)
len--;
if (*bufp == NULL || total + len >= *bufsizep) {
const size_t size = total + len + 1;
const size_t newsize = sudo_pow2_roundup(size);
bufsize = (size_t)(total + len + 1);
if (*bufp == NULL || bufsize > *bufsizep) {
const size_t newsize = sudo_pow2_roundup(bufsize);
void *newbuf;
if (newsize < size) {
if (newsize < bufsize) {
/* overflow */
errno = ENOMEM;
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;
*bufsizep = newsize;
}
memcpy(*bufp + total, cp, len + 1);
memcpy(*bufp + total, cp, (size_t)(len + 1));
total += len;
} while (continued);
free(line);

View File

@@ -46,15 +46,15 @@ static int
parse_num(const char *str, char **endp)
{
debug_decl(check_pattern, SUDO_DEBUG_UTIL);
const unsigned int lastval = INT_MAX / 10;
const unsigned int remainder = INT_MAX % 10;
unsigned int result = 0;
unsigned char ch;
const int lastval = INT_MAX / 10;
const int remainder = INT_MAX % 10;
int result = 0;
char ch;
while ((ch = *str++) != '\0') {
if (ch == '\\' && isdigit((unsigned int)str[0]))
if (ch == '\\' && isdigit((unsigned char)str[0]))
ch = *str++;
else if (!isdigit(ch))
else if (!isdigit((unsigned char)ch))
break;
ch -= '0';
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_debug_file *debug_file;
struct plugin_info *info;
size_t nwritten;
ssize_t nwritten;
int fd;
initprogname("fuzz_sudo_conf");
@@ -85,7 +85,7 @@ LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
if (fd == -1)
return 0;
nwritten = write(fd, data, size);
if (nwritten != size) {
if (nwritten == -1) {
close(fd);
return 0;
}

View File

@@ -46,7 +46,7 @@ main(int argc, char *argv[])
/* Build up test data. */
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++) {
/* lower case */
test_data[i].value = i;

View File

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

View File

@@ -43,7 +43,7 @@ static struct strtoidx_data {
{ "4294967295", 0, NULL, NULL, EINVAL },
{ "4294967296", 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
{ "-2", (id_t)4294967294U, NULL, NULL, 0 },
#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.
*/
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 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);
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) {
/* We always set name, even on error. */
memcpy(name, cp, namesize - 1);

View File

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

View File

@@ -89,7 +89,7 @@ sudo_sig2str(int signo, char *signame)
if (islower((unsigned char)signame[0])) {
size_t 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;
}

View File

@@ -94,7 +94,7 @@ sudo_str2sig(const char *signame, int *result)
/* Could be a signal number encoded as a string. */
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)
return -1;
*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. */
while (n-- != 0 && *dst != '\0')
dst++;
dlen = dst - odst;
dlen = (size_t)(dst - odst);
n = dsize - dlen;
if (n-- == 0)
@@ -64,6 +64,6 @@ sudo_strlcat(char *dst, const char *src, size_t dsize)
}
*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 */

View File

@@ -59,6 +59,6 @@ sudo_strlcpy(char *dst, const char *src, size_t dsize)
continue;
}
return(src - osrc - 1); /* count does not include NUL */
return((size_t)(src - osrc) - 1); /* count does not include NUL */
}
#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;
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) {
/*
* 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;
long long lastval, result = 0;
const char *cp = str;
unsigned char ch;
int remainder;
char sign;
char ch, sign;
if (minval > maxval) {
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. */
do {
ch = *cp++;
} while (isspace(ch));
} while (isspace((unsigned char)ch));
switch (ch) {
case '-':
sign = '-';
@@ -86,20 +85,20 @@ sudo_strtonumx(const char *str, long long minval, long long maxval, char **endp,
*/
if (sign == '-') {
lastval = minval / 10;
remainder = -(minval % 10);
remainder = -(int)(minval % 10);
if (remainder < 0) {
lastval += 1;
remainder += 10;
}
for (;; ch = *cp++) {
if (!isdigit(ch))
if (!isdigit((unsigned char)ch))
break;
ch -= '0';
if (result < lastval || (result == lastval && ch > remainder)) {
/* Skip remaining digits. */
do {
ch = *cp++;
} while (isdigit(ch));
} while (isdigit((unsigned char)ch));
errval = STN_TOOSMALL;
break;
} else {
@@ -112,16 +111,16 @@ sudo_strtonumx(const char *str, long long minval, long long maxval, char **endp,
errval = STN_TOOBIG;
} else {
lastval = maxval / 10;
remainder = maxval % 10;
remainder = (int)(maxval % 10);
for (;; ch = *cp++) {
if (!isdigit(ch))
if (!isdigit((unsigned char)ch))
break;
ch -= '0';
if (result > lastval || (result == lastval && ch > remainder)) {
/* Skip remaining digits. */
do {
ch = *cp++;
} while (isdigit(ch));
} while (isdigit((unsigned char)ch));
errval = STN_TOOBIG;
break;
} else {

View File

@@ -432,7 +432,7 @@ set_var_max_groups(const char *strval, const char *conf_file,
int max_groups;
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) {
sudo_warnx(U_("invalid max groups \"%s\" in %s, line %u"), strval,
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. */
fd = sudo_open_conf_path(_PATH_SUDO_CONF, conf_file,
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) {
case SUDO_PATH_SECURE:
/* 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;
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)
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_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. */
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;
}
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;
}
}