1
0
mirror of https://github.com/tmux/tmux.git synced 2025-04-10 11:08:49 +00:00

Use strtonum instead of atoi.

This commit is contained in:
nicm 2024-08-26 13:02:15 +00:00
parent 31b6c9356c
commit 2d1e93447e
3 changed files with 32 additions and 24 deletions

View File

@ -942,13 +942,17 @@ colour_byname(const char *name)
{ "yellow3", 0xcdcd00 }, { "yellow3", 0xcdcd00 },
{ "yellow4", 0x8b8b00 } { "yellow4", 0x8b8b00 }
}; };
u_int i; u_int i;
int c; int c;
const char *errstr;
if (strncmp(name, "grey", 4) == 0 || strncmp(name, "gray", 4) == 0) { if (strncmp(name, "grey", 4) == 0 || strncmp(name, "gray", 4) == 0) {
if (!isdigit((u_char)name[4])) if (name[4] == '\0')
return (0xbebebe|COLOUR_FLAG_RGB); return (-1);
c = round(2.55 * atoi(name + 4)); c = strtonum(name + 4, 0, 100, &errstr);
if (errstr != NULL)
return (-1);
c = round(2.55 * c);
if (c < 0 || c > 255) if (c < 0 || c > 255)
return (-1); return (-1);
return (colour_join_rgb(c, c, c)); return (colour_join_rgb(c, c, c));

30
style.c
View File

@ -58,10 +58,11 @@ int
style_parse(struct style *sy, const struct grid_cell *base, const char *in) style_parse(struct style *sy, const struct grid_cell *base, const char *in)
{ {
struct style saved; struct style saved;
const char delimiters[] = " ,\n", *cp; const char delimiters[] = " ,\n", *errstr;
char tmp[256], *found; char tmp[256], *found;
int value; int value;
size_t end; size_t end;
u_int n;
if (*in == '\0') if (*in == '\0')
return (0); return (0);
@ -137,34 +138,31 @@ style_parse(struct style *sy, const struct grid_cell *base, const char *in)
goto error; goto error;
if (*found != '%' || found[1] == '\0') if (*found != '%' || found[1] == '\0')
goto error; goto error;
for (cp = found + 1; *cp != '\0'; cp++) { n = strtonum(found + 1, 0, UINT_MAX, &errstr);
if (!isdigit((u_char)*cp)) if (errstr != NULL)
goto error; goto error;
}
sy->range_type = STYLE_RANGE_PANE; sy->range_type = STYLE_RANGE_PANE;
sy->range_argument = atoi(found + 1); sy->range_argument = n;
style_set_range_string(sy, ""); style_set_range_string(sy, "");
} else if (strcasecmp(tmp + 6, "window") == 0) { } else if (strcasecmp(tmp + 6, "window") == 0) {
if (found == NULL) if (found == NULL)
goto error; goto error;
for (cp = found; *cp != '\0'; cp++) { n = strtonum(found, 0, UINT_MAX, &errstr);
if (!isdigit((u_char)*cp)) if (errstr != NULL)
goto error; goto error;
}
sy->range_type = STYLE_RANGE_WINDOW; sy->range_type = STYLE_RANGE_WINDOW;
sy->range_argument = atoi(found); sy->range_argument = n;
style_set_range_string(sy, ""); style_set_range_string(sy, "");
} else if (strcasecmp(tmp + 6, "session") == 0) { } else if (strcasecmp(tmp + 6, "session") == 0) {
if (found == NULL) if (found == NULL)
goto error; goto error;
if (*found != '$' || found[1] == '\0') if (*found != '$' || found[1] == '\0')
goto error; goto error;
for (cp = found + 1; *cp != '\0'; cp++) { n = strtonum(found + 1, 0, UINT_MAX, &errstr);
if (!isdigit((u_char)*cp)) if (errstr != NULL)
goto error; goto error;
}
sy->range_type = STYLE_RANGE_SESSION; sy->range_type = STYLE_RANGE_SESSION;
sy->range_argument = atoi(found + 1); sy->range_argument = n;
style_set_range_string(sy, ""); style_set_range_string(sy, "");
} else if (strcasecmp(tmp + 6, "user") == 0) { } else if (strcasecmp(tmp + 6, "user") == 0) {
if (found == NULL) if (found == NULL)

View File

@ -528,9 +528,10 @@ tty_term_create(struct tty *tty, char *name, char **caps, u_int ncaps,
struct options_array_item *a; struct options_array_item *a;
union options_value *ov; union options_value *ov;
u_int i, j; u_int i, j;
const char *s, *value; const char *s, *value, *errstr;
size_t offset, namelen; size_t offset, namelen;
char *first; char *first;
int n;
log_debug("adding term %s", name); log_debug("adding term %s", name);
@ -564,8 +565,13 @@ tty_term_create(struct tty *tty, char *name, char **caps, u_int ncaps,
code->value.string = tty_term_strip(value); code->value.string = tty_term_strip(value);
break; break;
case TTYCODE_NUMBER: case TTYCODE_NUMBER:
code->type = TTYCODE_NUMBER; n = strtonum(value, 0, INT_MAX, &errstr);
code->value.number = atoi(value); if (errstr != NULL)
log_debug("%s: %s", ent->name, errstr);
else {
code->type = TTYCODE_NUMBER;
code->value.number = n;
}
break; break;
case TTYCODE_FLAG: case TTYCODE_FLAG:
code->type = TTYCODE_FLAG; code->type = TTYCODE_FLAG;