diff --git a/include/sudo_util.h b/include/sudo_util.h index 285228188..e6043cb7e 100644 --- a/include/sudo_util.h +++ b/include/sudo_util.h @@ -136,6 +136,10 @@ #define sudo_isset(_a, _i) ((_a)[(_i) / NBBY] & (1<<((_i) % NBBY))) #define sudo_isclr(_a, _i) (((_a)[(_i) / NBBY] & (1<<((_i) % NBBY))) == 0) +/* Macros to determine the length of a type in string form. */ +#define STRLEN_MAX_UNSIGNED(t) (((sizeof(t) * 8 * 1233) >> 12) + 1) +#define STRLEN_MAX_SIGNED(t) (STRLEN_MAX_UNSIGNED(t) + ((sizeof(t) == 8) ? 0 : 1)) + /* sudo_parseln() flags */ #define PARSELN_COMM_BOL 0x01 /* comments only at beginning of line */ #define PARSELN_CONT_IGN 0x02 /* ignore line continuation char */ diff --git a/lib/eventlog/eventlog.c b/lib/eventlog/eventlog.c index 1c2472cb9..478007ec4 100644 --- a/lib/eventlog/eventlog.c +++ b/lib/eventlog/eventlog.c @@ -84,7 +84,7 @@ new_logline(int event_type, int flags, struct eventlog_args *args, const struct eventlog_config *evl_conf = eventlog_getconf(); const char *iolog_file; const char *tty, *tsid = NULL; - char exit_str[(((sizeof(int) * 8) + 2) / 3) + 2]; + char exit_str[STRLEN_MAX_SIGNED(int) + 1]; char sessid[7], offsetstr[64] = ""; size_t i; debug_decl(new_logline, SUDO_DEBUG_UTIL); diff --git a/lib/iolog/iolog_timing.c b/lib/iolog/iolog_timing.c index b401f7b7f..2e8c33931 100644 --- a/lib/iolog/iolog_timing.c +++ b/lib/iolog/iolog_timing.c @@ -88,7 +88,7 @@ char * iolog_parse_delay(const char *cp, struct timespec *delay, const char *decimal_point) { - char numbuf[(((sizeof(long long) * 8) + 2) / 3) + 2]; + char numbuf[STRLEN_MAX_SIGNED(long long) + 1]; const char *errstr, *ep; long long llval; size_t len; diff --git a/lib/util/json.c b/lib/util/json.c index 8edab73c4..58e3305ed 100644 --- a/lib/util/json.c +++ b/lib/util/json.c @@ -318,7 +318,7 @@ sudo_json_add_value_int(struct json_container *jsonc, const char *name, struct json_value *value, bool as_object) { struct json_container saved_container = *jsonc; - char numbuf[(((sizeof(long long) * 8) + 2) / 3) + 2]; + char numbuf[STRLEN_MAX_SIGNED(long long) + 1]; debug_decl(sudo_json_add_value, SUDO_DEBUG_UTIL); /* Add comma if we are continuing an object/array. */ diff --git a/lib/util/lbuf.c b/lib/util/lbuf.c index c2e695302..6aee5609b 100644 --- a/lib/util/lbuf.c +++ b/lib/util/lbuf.c @@ -302,7 +302,7 @@ sudo_lbuf_append_v1(struct sudo_lbuf *lbuf, const char * restrict fmt, ...) num_end++; 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]; + char numbuf[STRLEN_MAX_SIGNED(int) + 1]; len = (unsigned int)(num_end - num_start); if (len >= sizeof(numbuf)) { errno = EINVAL; diff --git a/lib/util/sudo_debug.c b/lib/util/sudo_debug.c index d5bf4aa01..fe9ed458b 100644 --- a/lib/util/sudo_debug.c +++ b/lib/util/sudo_debug.c @@ -112,7 +112,7 @@ struct sudo_debug_instance { static struct sudo_debug_instance *sudo_debug_instances[SUDO_DEBUG_INSTANCE_MAX]; static int sudo_debug_last_instance = -1; -static char sudo_debug_pidstr[(((sizeof(int) * 8) + 2) / 3) + 3]; +static char sudo_debug_pidstr[STRLEN_MAX_SIGNED(int) + 3]; static size_t sudo_debug_pidlen; #define round_nfds(_n) (((_n) + (4 * NBBY) - 1) & ~((4 * NBBY) - 1)) diff --git a/plugins/sudoers/cvtsudoers_ldif.c b/plugins/sudoers/cvtsudoers_ldif.c index 0724094e7..3bc742d75 100644 --- a/plugins/sudoers/cvtsudoers_ldif.c +++ b/plugins/sudoers/cvtsudoers_ldif.c @@ -658,7 +658,7 @@ print_userspec_ldif(FILE *fp, const struct sudoers_parse_tree *parse_tree, print_cmndspec_ldif(fp, parse_tree, cs, &next, &priv->defaults); if (conf->sudo_order != 0) { - char numbuf[(((sizeof(conf->sudo_order) * 8) + 2) / 3) + 2]; + char numbuf[STRLEN_MAX_UNSIGNED(conf->sudo_order) + 1]; if (conf->order_max != 0 && conf->sudo_order > conf->order_max) { sudo_fatalx(U_("too many sudoers entries, maximum %u"), conf->order_padding); diff --git a/plugins/sudoers/display.c b/plugins/sudoers/display.c index de476d3f3..403598ee4 100644 --- a/plugins/sudoers/display.c +++ b/plugins/sudoers/display.c @@ -228,7 +228,7 @@ display_cmndspec_long(const struct sudoers_parse_tree *parse_tree, if (cs->runcwd != NULL) sudo_lbuf_append(lbuf, " Cwd: %s\n", cs->runcwd); if (cs->timeout > 0) { - char numbuf[(((sizeof(int) * 8) + 2) / 3) + 2]; + char numbuf[STRLEN_MAX_SIGNED(int) + 1]; (void)snprintf(numbuf, sizeof(numbuf), "%d", cs->timeout); sudo_lbuf_append(lbuf, " Timeout: %s\n", numbuf); } diff --git a/plugins/sudoers/fmtsudoers.c b/plugins/sudoers/fmtsudoers.c index 5c182b087..1b4b7c5ef 100644 --- a/plugins/sudoers/fmtsudoers.c +++ b/plugins/sudoers/fmtsudoers.c @@ -255,7 +255,7 @@ sudoers_format_cmndspec(struct sudo_lbuf *lbuf, if (cs->runcwd != NULL && FIELD_CHANGED(prev_cs, cs, runcwd)) sudo_lbuf_append(lbuf, "CWD=%s ", cs->runcwd); if (cs->timeout > 0 && FIELD_CHANGED(prev_cs, cs, timeout)) { - char numbuf[(((sizeof(int) * 8) + 2) / 3) + 2]; + char numbuf[STRLEN_MAX_SIGNED(int) + 1]; (void)snprintf(numbuf, sizeof(numbuf), "%d", cs->timeout); sudo_lbuf_append(lbuf, "TIMEOUT=%s ", numbuf); } diff --git a/plugins/sudoers/logging.c b/plugins/sudoers/logging.c index 02108f3e4..ac072d31c 100644 --- a/plugins/sudoers/logging.c +++ b/plugins/sudoers/logging.c @@ -399,7 +399,7 @@ log_failure(const struct sudoers_context *ctx, unsigned int status, static char * fmt_authfail_message(unsigned int tries) { - char numbuf[(((sizeof(int) * 8) + 2) / 3) + 2]; + char numbuf[STRLEN_MAX_UNSIGNED(unsigned int) + 1]; char *dst, *dst_end, *ret = NULL; const char *src; size_t len; diff --git a/src/exec_preload.c b/src/exec_preload.c index 2a88e5bb1..af5b12944 100644 --- a/src/exec_preload.c +++ b/src/exec_preload.c @@ -76,7 +76,7 @@ fmtstr(sudo_alloc_fn_t alloc_fn, sudo_free_fn_t free_fn, const char * restrict o fmt += 2; continue; case 'd': { - char numbuf[(((sizeof(int) * 8) + 2) / 3) + 2]; + char numbuf[STRLEN_MAX_SIGNED(int) + 1]; len = (size_t)snprintf(numbuf, sizeof(numbuf), "%d", va_arg(ap, int)); if (len >= sizeof(numbuf)) { diff --git a/src/limits.c b/src/limits.c index 58224141e..d82ac0041 100644 --- a/src/limits.c +++ b/src/limits.c @@ -681,8 +681,8 @@ serialize_rlimits(char **info, size_t info_max) for (idx = 0; idx < nitems(saved_limits); idx++) { const struct saved_limit *lim = &saved_limits[idx]; const struct rlimit *rl = &lim->oldlimit; - char curlim[(((sizeof(long long) * 8) + 2) / 3) + 2]; - char maxlim[(((sizeof(long long) * 8) + 2) / 3) + 2]; + char curlim[STRLEN_MAX_UNSIGNED(unsigned long long) + 1]; + char maxlim[STRLEN_MAX_UNSIGNED(unsigned long long) + 1]; if (!lim->saved) continue;