From 527d0d53b5f7c2139be60f5e12a42baffaeea3ca Mon Sep 17 00:00:00 2001 From: nicm Date: Mon, 26 Aug 2024 13:02:15 +0000 Subject: [PATCH] Use strtonum instead of atoi. --- usr.bin/tmux/colour.c | 16 ++++++++++------ usr.bin/tmux/style.c | 32 +++++++++++++++----------------- usr.bin/tmux/tty-term.c | 14 ++++++++++---- 3 files changed, 35 insertions(+), 27 deletions(-) diff --git a/usr.bin/tmux/colour.c b/usr.bin/tmux/colour.c index 4f7a9a08d55..455781221cc 100644 --- a/usr.bin/tmux/colour.c +++ b/usr.bin/tmux/colour.c @@ -1,4 +1,4 @@ -/* $OpenBSD: colour.c,v 1.26 2023/01/03 11:43:24 nicm Exp $ */ +/* $OpenBSD: colour.c,v 1.27 2024/08/26 13:02:15 nicm Exp $ */ /* * Copyright (c) 2008 Nicholas Marriott @@ -942,13 +942,17 @@ colour_byname(const char *name) { "yellow3", 0xcdcd00 }, { "yellow4", 0x8b8b00 } }; - u_int i; - int c; + u_int i; + int c; + const char *errstr; if (strncmp(name, "grey", 4) == 0 || strncmp(name, "gray", 4) == 0) { - if (!isdigit((u_char)name[4])) - return (0xbebebe|COLOUR_FLAG_RGB); - c = round(2.55 * atoi(name + 4)); + if (name[4] == '\0') + return (-1); + c = strtonum(name + 4, 0, 100, &errstr); + if (errstr != NULL) + return (-1); + c = round(2.55 * c); if (c < 0 || c > 255) return (-1); return (colour_join_rgb(c, c, c)); diff --git a/usr.bin/tmux/style.c b/usr.bin/tmux/style.c index 8491e0329b9..049b40ed3cc 100644 --- a/usr.bin/tmux/style.c +++ b/usr.bin/tmux/style.c @@ -1,4 +1,4 @@ -/* $OpenBSD: style.c,v 1.34 2024/01/22 16:34:46 nicm Exp $ */ +/* $OpenBSD: style.c,v 1.35 2024/08/26 13:02:15 nicm Exp $ */ /* * Copyright (c) 2007 Nicholas Marriott @@ -58,10 +58,11 @@ int style_parse(struct style *sy, const struct grid_cell *base, const char *in) { struct style saved; - const char delimiters[] = " ,\n", *cp; + const char delimiters[] = " ,\n", *errstr; char tmp[256], *found; int value; size_t end; + u_int n; if (*in == '\0') return (0); @@ -137,34 +138,31 @@ style_parse(struct style *sy, const struct grid_cell *base, const char *in) goto error; if (*found != '%' || found[1] == '\0') goto error; - for (cp = found + 1; *cp != '\0'; cp++) { - if (!isdigit((u_char)*cp)) - goto error; - } + n = strtonum(found + 1, 0, UINT_MAX, &errstr); + if (errstr != NULL) + goto error; sy->range_type = STYLE_RANGE_PANE; - sy->range_argument = atoi(found + 1); + sy->range_argument = n; style_set_range_string(sy, ""); } else if (strcasecmp(tmp + 6, "window") == 0) { if (found == NULL) goto error; - for (cp = found; *cp != '\0'; cp++) { - if (!isdigit((u_char)*cp)) - goto error; - } + n = strtonum(found, 0, UINT_MAX, &errstr); + if (errstr != NULL) + goto error; sy->range_type = STYLE_RANGE_WINDOW; - sy->range_argument = atoi(found); + sy->range_argument = n; style_set_range_string(sy, ""); } else if (strcasecmp(tmp + 6, "session") == 0) { if (found == NULL) goto error; if (*found != '$' || found[1] == '\0') goto error; - for (cp = found + 1; *cp != '\0'; cp++) { - if (!isdigit((u_char)*cp)) - goto error; - } + n = strtonum(found + 1, 0, UINT_MAX, &errstr); + if (errstr != NULL) + goto error; sy->range_type = STYLE_RANGE_SESSION; - sy->range_argument = atoi(found + 1); + sy->range_argument = n; style_set_range_string(sy, ""); } else if (strcasecmp(tmp + 6, "user") == 0) { if (found == NULL) diff --git a/usr.bin/tmux/tty-term.c b/usr.bin/tmux/tty-term.c index 7e0dd475b6c..e6808830310 100644 --- a/usr.bin/tmux/tty-term.c +++ b/usr.bin/tmux/tty-term.c @@ -1,4 +1,4 @@ -/* $OpenBSD: tty-term.c,v 1.101 2023/10/17 09:55:32 nicm Exp $ */ +/* $OpenBSD: tty-term.c,v 1.102 2024/08/26 13:02:15 nicm Exp $ */ /* * Copyright (c) 2008 Nicholas Marriott @@ -528,9 +528,10 @@ tty_term_create(struct tty *tty, char *name, char **caps, u_int ncaps, struct options_array_item *a; union options_value *ov; u_int i, j; - const char *s, *value; + const char *s, *value, *errstr; size_t offset, namelen; char *first; + int n; 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); break; case TTYCODE_NUMBER: - code->type = TTYCODE_NUMBER; - code->value.number = atoi(value); + n = strtonum(value, 0, INT_MAX, &errstr); + if (errstr != NULL) + log_debug("%s: %s", ent->name, errstr); + else { + code->type = TTYCODE_NUMBER; + code->value.number = n; + } break; case TTYCODE_FLAG: code->type = TTYCODE_FLAG; -- 2.20.1