Add macros to determine the length of an integer type in string form.

Adapted from answer #6 in:
https://stackoverflow.com/questions/10536207/ansi-c-maximum-number-of-characters-printing-a-decimal-int
This commit is contained in:
Todd C. Miller
2023-09-19 15:15:02 -06:00
parent 221a10340c
commit d53bbb54b2
12 changed files with 16 additions and 12 deletions

View File

@@ -136,6 +136,10 @@
#define sudo_isset(_a, _i) ((_a)[(_i) / NBBY] & (1<<((_i) % NBBY))) #define sudo_isset(_a, _i) ((_a)[(_i) / NBBY] & (1<<((_i) % NBBY)))
#define sudo_isclr(_a, _i) (((_a)[(_i) / NBBY] & (1<<((_i) % NBBY))) == 0) #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 */ /* sudo_parseln() flags */
#define PARSELN_COMM_BOL 0x01 /* comments only at beginning of line */ #define PARSELN_COMM_BOL 0x01 /* comments only at beginning of line */
#define PARSELN_CONT_IGN 0x02 /* ignore line continuation char */ #define PARSELN_CONT_IGN 0x02 /* ignore line continuation char */

View File

@@ -84,7 +84,7 @@ new_logline(int event_type, int flags, struct eventlog_args *args,
const struct eventlog_config *evl_conf = eventlog_getconf(); const struct eventlog_config *evl_conf = eventlog_getconf();
const char *iolog_file; const char *iolog_file;
const char *tty, *tsid = NULL; 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] = ""; char sessid[7], offsetstr[64] = "";
size_t i; size_t i;
debug_decl(new_logline, SUDO_DEBUG_UTIL); debug_decl(new_logline, SUDO_DEBUG_UTIL);

View File

@@ -88,7 +88,7 @@ char *
iolog_parse_delay(const char *cp, struct timespec *delay, iolog_parse_delay(const char *cp, struct timespec *delay,
const char *decimal_point) 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; const char *errstr, *ep;
long long llval; long long llval;
size_t len; size_t len;

View File

@@ -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_value *value, bool as_object)
{ {
struct json_container saved_container = *jsonc; 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); debug_decl(sudo_json_add_value, SUDO_DEBUG_UTIL);
/* Add comma if we are continuing an object/array. */ /* Add comma if we are continuing an object/array. */

View File

@@ -302,7 +302,7 @@ sudo_lbuf_append_v1(struct sudo_lbuf *lbuf, const char * restrict fmt, ...)
num_end++; num_end++;
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[STRLEN_MAX_SIGNED(int) + 1];
len = (unsigned int)(num_end - num_start); len = (unsigned int)(num_end - num_start);
if (len >= sizeof(numbuf)) { if (len >= sizeof(numbuf)) {
errno = EINVAL; errno = EINVAL;

View File

@@ -112,7 +112,7 @@ struct sudo_debug_instance {
static struct sudo_debug_instance *sudo_debug_instances[SUDO_DEBUG_INSTANCE_MAX]; static struct sudo_debug_instance *sudo_debug_instances[SUDO_DEBUG_INSTANCE_MAX];
static int sudo_debug_last_instance = -1; 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; static size_t sudo_debug_pidlen;
#define round_nfds(_n) (((_n) + (4 * NBBY) - 1) & ~((4 * NBBY) - 1)) #define round_nfds(_n) (((_n) + (4 * NBBY) - 1) & ~((4 * NBBY) - 1))

View File

@@ -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); print_cmndspec_ldif(fp, parse_tree, cs, &next, &priv->defaults);
if (conf->sudo_order != 0) { 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) { if (conf->order_max != 0 && conf->sudo_order > conf->order_max) {
sudo_fatalx(U_("too many sudoers entries, maximum %u"), sudo_fatalx(U_("too many sudoers entries, maximum %u"),
conf->order_padding); conf->order_padding);

View File

@@ -228,7 +228,7 @@ display_cmndspec_long(const struct sudoers_parse_tree *parse_tree,
if (cs->runcwd != NULL) if (cs->runcwd != NULL)
sudo_lbuf_append(lbuf, " Cwd: %s\n", cs->runcwd); sudo_lbuf_append(lbuf, " Cwd: %s\n", cs->runcwd);
if (cs->timeout > 0) { 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); (void)snprintf(numbuf, sizeof(numbuf), "%d", cs->timeout);
sudo_lbuf_append(lbuf, " Timeout: %s\n", numbuf); sudo_lbuf_append(lbuf, " Timeout: %s\n", numbuf);
} }

View File

@@ -255,7 +255,7 @@ sudoers_format_cmndspec(struct sudo_lbuf *lbuf,
if (cs->runcwd != NULL && FIELD_CHANGED(prev_cs, cs, runcwd)) if (cs->runcwd != NULL && FIELD_CHANGED(prev_cs, cs, runcwd))
sudo_lbuf_append(lbuf, "CWD=%s ", cs->runcwd); sudo_lbuf_append(lbuf, "CWD=%s ", cs->runcwd);
if (cs->timeout > 0 && FIELD_CHANGED(prev_cs, cs, timeout)) { 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); (void)snprintf(numbuf, sizeof(numbuf), "%d", cs->timeout);
sudo_lbuf_append(lbuf, "TIMEOUT=%s ", numbuf); sudo_lbuf_append(lbuf, "TIMEOUT=%s ", numbuf);
} }

View File

@@ -399,7 +399,7 @@ log_failure(const struct sudoers_context *ctx, unsigned int status,
static char * static char *
fmt_authfail_message(unsigned int tries) 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; char *dst, *dst_end, *ret = NULL;
const char *src; const char *src;
size_t len; size_t len;

View File

@@ -76,7 +76,7 @@ fmtstr(sudo_alloc_fn_t alloc_fn, sudo_free_fn_t free_fn, const char * restrict o
fmt += 2; fmt += 2;
continue; continue;
case 'd': { 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", len = (size_t)snprintf(numbuf, sizeof(numbuf), "%d",
va_arg(ap, int)); va_arg(ap, int));
if (len >= sizeof(numbuf)) { if (len >= sizeof(numbuf)) {

View File

@@ -681,8 +681,8 @@ serialize_rlimits(char **info, size_t info_max)
for (idx = 0; idx < nitems(saved_limits); idx++) { for (idx = 0; idx < nitems(saved_limits); idx++) {
const struct saved_limit *lim = &saved_limits[idx]; const struct saved_limit *lim = &saved_limits[idx];
const struct rlimit *rl = &lim->oldlimit; const struct rlimit *rl = &lim->oldlimit;
char curlim[(((sizeof(long long) * 8) + 2) / 3) + 2]; char curlim[STRLEN_MAX_UNSIGNED(unsigned long long) + 1];
char maxlim[(((sizeof(long long) * 8) + 2) / 3) + 2]; char maxlim[STRLEN_MAX_UNSIGNED(unsigned long long) + 1];
if (!lim->saved) if (!lim->saved)
continue; continue;