2
0
mirror of https://github.com/sudo-project/sudo.git synced 2025-08-22 01:49:11 +00:00

Make sudo_pow2_roundup() operate on size_t.

This commit is contained in:
Todd C. Miller 2023-07-03 16:51:05 -06:00
parent b926df1df2
commit 56a431f7ea
7 changed files with 24 additions and 10 deletions

View File

@ -262,7 +262,8 @@ sudo_dso_public bool sudo_regex_compile_v1(void *v, const char *pattern, const c
/* roundup.c */ /* roundup.c */
sudo_dso_public unsigned int sudo_pow2_roundup_v1(unsigned int len); 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 */ /* secure_path.c */
#define SUDO_PATH_SECURE 0 #define SUDO_PATH_SECURE 0

View File

@ -79,7 +79,7 @@ sudo_lbuf_expand(struct sudo_lbuf *lbuf, unsigned int extra)
} }
if (lbuf->len + extra + 1 > lbuf->size) { 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; char *new_buf;
if (new_size < lbuf->size) { if (new_size < lbuf->size) {

View File

@ -30,10 +30,20 @@
/* /*
* Round 32-bit unsigned length to the next highest power of two. * Round 32-bit unsigned length to the next highest power of two.
* Always returns at least 64. * Always returns at least 64.
* Algorithm from bit twiddling hacks.
*/ */
unsigned int unsigned int
sudo_pow2_roundup_v1(unsigned int len) 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) if (len < 64)
return 64; return 64;
@ -43,6 +53,8 @@ sudo_pow2_roundup_v1(unsigned int len)
len |= len >> 4; len |= len >> 4;
len |= len >> 8; len |= len >> 8;
len |= len >> 16; len |= len >> 16;
len++; #ifdef __LP64__
return len; len |= len >> 32;
#endif
return ++len;
} }

View File

@ -126,6 +126,7 @@ sudo_parse_gids_v1
sudo_parseln_v1 sudo_parseln_v1
sudo_parseln_v2 sudo_parseln_v2
sudo_pow2_roundup_v1 sudo_pow2_roundup_v1
sudo_pow2_roundup_v2
sudo_rcstr_addref sudo_rcstr_addref
sudo_rcstr_alloc sudo_rcstr_alloc
sudo_rcstr_delref sudo_rcstr_delref

View File

@ -62,9 +62,9 @@ expand_buf(struct connection_buffer *buf, unsigned int needed)
if (buf->size < needed) { if (buf->size < needed) {
/* Expand buffer. */ /* 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, 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) { if (newsize < needed) {
/* overflow */ /* overflow */
errno = ENOMEM; errno = ENOMEM;

View File

@ -302,7 +302,7 @@ get_free_buf(size_t len, struct connection_closure *closure)
} }
if (len > buf->size) { 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) { if (new_size < len) {
/* overflow */ /* overflow */
errno = ENOMEM; errno = ENOMEM;

View File

@ -732,7 +732,7 @@ fmt_client_message(struct client_closure *closure, ClientMessage *msg)
/* Resize buffer as needed. */ /* Resize buffer as needed. */
if (len > buf->size) { 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) { if (new_size < len) {
/* overflow */ /* overflow */
errno = ENOMEM; errno = ENOMEM;
@ -1639,7 +1639,7 @@ expand_buf(struct connection_buffer *buf, unsigned int needed)
if (buf->size < needed) { if (buf->size < needed) {
/* Expand buffer. */ /* Expand buffer. */
const unsigned int newsize = sudo_pow2_roundup(needed); const size_t newsize = sudo_pow2_roundup(needed);
if (newsize < needed) { if (newsize < needed) {
/* overflow */ /* overflow */
errno = ENOMEM; errno = ENOMEM;