diff --git a/include/sudo_util.h b/include/sudo_util.h index 8212a3b16..3702b9705 100644 --- a/include/sudo_util.h +++ b/include/sudo_util.h @@ -262,7 +262,8 @@ sudo_dso_public bool sudo_regex_compile_v1(void *v, const char *pattern, const c /* roundup.c */ sudo_dso_public unsigned int sudo_pow2_roundup_v1(unsigned int len); -#define sudo_pow2_roundup(_a) sudo_pow2_roundup_v1((_a)) +sudo_dso_public size_t sudo_pow2_roundup_v2(size_t len); +#define sudo_pow2_roundup(_a) sudo_pow2_roundup_v2((_a)) /* secure_path.c */ #define SUDO_PATH_SECURE 0 diff --git a/lib/util/lbuf.c b/lib/util/lbuf.c index b659ff1a7..f8851ee86 100644 --- a/lib/util/lbuf.c +++ b/lib/util/lbuf.c @@ -79,7 +79,7 @@ sudo_lbuf_expand(struct sudo_lbuf *lbuf, unsigned int extra) } if (lbuf->len + extra + 1 > lbuf->size) { - unsigned int new_size = sudo_pow2_roundup(lbuf->len + extra + 1); + size_t new_size = sudo_pow2_roundup(lbuf->len + extra + 1); char *new_buf; if (new_size < lbuf->size) { diff --git a/lib/util/roundup.c b/lib/util/roundup.c index 0e7e8fdcd..7caf8b454 100644 --- a/lib/util/roundup.c +++ b/lib/util/roundup.c @@ -30,10 +30,20 @@ /* * Round 32-bit unsigned length to the next highest power of two. * Always returns at least 64. - * Algorithm from bit twiddling hacks. */ unsigned int sudo_pow2_roundup_v1(unsigned int len) +{ + return (unsigned int)sudo_pow2_roundup_v2((size_t)len); +} + +/* + * Round a size_t length to the next highest power of two. + * Always returns at least 64. + * Algorithm from bit twiddling hacks. + */ +size_t +sudo_pow2_roundup_v2(size_t len) { if (len < 64) return 64; @@ -43,6 +53,8 @@ sudo_pow2_roundup_v1(unsigned int len) len |= len >> 4; len |= len >> 8; len |= len >> 16; - len++; - return len; +#ifdef __LP64__ + len |= len >> 32; +#endif + return ++len; } diff --git a/lib/util/util.exp.in b/lib/util/util.exp.in index 54bf365f5..d8ceeb51a 100644 --- a/lib/util/util.exp.in +++ b/lib/util/util.exp.in @@ -126,6 +126,7 @@ sudo_parse_gids_v1 sudo_parseln_v1 sudo_parseln_v2 sudo_pow2_roundup_v1 +sudo_pow2_roundup_v2 sudo_rcstr_addref sudo_rcstr_alloc sudo_rcstr_delref diff --git a/logsrvd/logsrv_util.c b/logsrvd/logsrv_util.c index 118b81bb4..95ec86e64 100644 --- a/logsrvd/logsrv_util.c +++ b/logsrvd/logsrv_util.c @@ -62,9 +62,9 @@ expand_buf(struct connection_buffer *buf, unsigned int needed) if (buf->size < needed) { /* Expand buffer. */ - const unsigned int newsize = sudo_pow2_roundup(needed); + const size_t newsize = sudo_pow2_roundup(needed); sudo_debug_printf(SUDO_DEBUG_INFO|SUDO_DEBUG_LINENO, - "expanding buffer from %u to %u", buf->size, newsize); + "expanding buffer from %u to %zu", buf->size, newsize); if (newsize < needed) { /* overflow */ errno = ENOMEM; diff --git a/logsrvd/logsrvd.c b/logsrvd/logsrvd.c index 9fc93c777..77e9252f8 100644 --- a/logsrvd/logsrvd.c +++ b/logsrvd/logsrvd.c @@ -302,7 +302,7 @@ get_free_buf(size_t len, struct connection_closure *closure) } if (len > buf->size) { - const unsigned int new_size = sudo_pow2_roundup(len); + const size_t new_size = sudo_pow2_roundup(len); if (new_size < len) { /* overflow */ errno = ENOMEM; diff --git a/plugins/sudoers/log_client.c b/plugins/sudoers/log_client.c index 5017f581d..d30353fbb 100644 --- a/plugins/sudoers/log_client.c +++ b/plugins/sudoers/log_client.c @@ -732,7 +732,7 @@ fmt_client_message(struct client_closure *closure, ClientMessage *msg) /* Resize buffer as needed. */ if (len > buf->size) { - const unsigned int new_size = sudo_pow2_roundup(len); + const size_t new_size = sudo_pow2_roundup(len); if (new_size < len) { /* overflow */ errno = ENOMEM; @@ -1639,7 +1639,7 @@ expand_buf(struct connection_buffer *buf, unsigned int needed) if (buf->size < needed) { /* Expand buffer. */ - const unsigned int newsize = sudo_pow2_roundup(needed); + const size_t newsize = sudo_pow2_roundup(needed); if (newsize < needed) { /* overflow */ errno = ENOMEM;