diff options
author | Neels Hofmeyr <nhofmeyr@sysmocom.de> | 2021-09-05 19:50:34 +0200 |
---|---|---|
committer | neels <nhofmeyr@sysmocom.de> | 2021-10-04 11:24:59 +0000 |
commit | 34907fe6e14965fccd9f2eec3e857eac78e2bb17 (patch) | |
tree | 8a7ce6ebd60f50562ba977c216aa96ebfab2f7d3 /src | |
parent | 6b60d52abf0a12a21f6f11150a5e3a0dd3eceda6 (diff) |
revisit some calls of strtol(), stroul(), strtoull()
Replace some with atoi(), where the VTY has already validated correct
range of the argument.
Replace others with the new osmo_str_to_int() or osmo_str_to_int64()
functions, possibly covering more detection of invalid number strings.
Leave those strtol() callers that depend on endptr to provide the next
string token.
Related: SYS#5542
Change-Id: I0ebb06e751c28f7d1cdf328de29cd227a2449391
Diffstat (limited to 'src')
-rw-r--r-- | src/ctrl/control_if.c | 12 | ||||
-rw-r--r-- | src/gsm/gsm23003.c | 19 | ||||
-rw-r--r-- | src/gsm/gsm23236.c | 22 | ||||
-rw-r--r-- | src/vty/command.c | 29 | ||||
-rw-r--r-- | src/vty/cpu_sched_vty.c | 11 | ||||
-rw-r--r-- | src/vty/tdef_vty.c | 7 |
6 files changed, 28 insertions, 72 deletions
diff --git a/src/ctrl/control_if.c b/src/ctrl/control_if.c index 5fda28fa..bea8496f 100644 --- a/src/ctrl/control_if.c +++ b/src/ctrl/control_if.c @@ -84,20 +84,16 @@ static LLIST_HEAD(ctrl_lookup_helpers); * \returns 1 on success; 0 in case of error */ int ctrl_parse_get_num(vector vline, int i, long *num) { - char *token, *tmp; + char *token; + int64_t val; if (i >= vector_active(vline)) return 0; token = vector_slot(vline, i); - errno = 0; - if (token[0] == '\0') + if (osmo_str_to_int64(&val, token, 10, LONG_MIN, LONG_MAX)) return 0; - - *num = strtol(token, &tmp, 10); - if (tmp[0] != '\0' || errno != 0) - return 0; - + *num = (long)val; return 1; } diff --git a/src/gsm/gsm23003.c b/src/gsm/gsm23003.c index c2b3de86..f3c0123b 100644 --- a/src/gsm/gsm23003.c +++ b/src/gsm/gsm23003.c @@ -487,22 +487,23 @@ void osmo_plmn_from_bcd(const uint8_t *bcd_src, struct osmo_plmn_id *plmn) */ int osmo_mnc_from_str(const char *mnc_str, uint16_t *mnc, bool *mnc_3_digits) { - long int _mnc = 0; + int _mnc = 0; bool _mnc_3_digits = false; - char *endptr; int rc = 0; if (!mnc_str || !isdigit((unsigned char)mnc_str[0]) || strlen(mnc_str) > 3) return -EINVAL; - errno = 0; - _mnc = strtol(mnc_str, &endptr, 10); - if (errno) - rc = -errno; - else if (*endptr) + rc = osmo_str_to_int(&_mnc, mnc_str, 10, 0, 999); + /* Heed the API definition to return -EINVAL in case of surplus chars */ + if (rc == -E2BIG) return -EINVAL; - if (_mnc < 0 || _mnc > 999) - return -ERANGE; + /* Heed the API definition to always return negative errno */ + if (rc > 0) + return -rc; + if (rc < 0) + return rc; + _mnc_3_digits = strlen(mnc_str) > 2; if (mnc) diff --git a/src/gsm/gsm23236.c b/src/gsm/gsm23236.c index 01d0eb36..d4a14a55 100644 --- a/src/gsm/gsm23236.c +++ b/src/gsm/gsm23236.c @@ -436,27 +436,13 @@ char *osmo_nri_ranges_to_str_c(void *ctx, const struct osmo_nri_ranges *nri_rang */ static int osmo_nri_parse(int16_t *dst, const char *str) { - char *endp; - int64_t val; + int val; int base = 10; - - if (osmo_str_startswith(str, "0x")) { - str += 2; + if (osmo_str_startswith(str, "0x")) base = 16; - } - - if (!str || !str[0]) - return -1; - - errno = 0; - val = strtoull(str, &endp, base); - if (errno || *endp != '\0') - return -1; - - if (val < 0 || val > INT16_MAX) + if (osmo_str_to_int(&val, str, base, 0, INT16_MAX)) return -1; - - *dst = val; + *dst = (int16_t)val; return 0; } diff --git a/src/vty/command.c b/src/vty/command.c index e5e7d152..2a8942db 100644 --- a/src/vty/command.c +++ b/src/vty/command.c @@ -1349,7 +1349,6 @@ static enum match_type cmd_ipv6_prefix_match(const char *str) int colons = 0, nums = 0, double_colon = 0; int mask; const char *sp = NULL; - char *endptr = NULL; if (str == NULL) return PARTLY_MATCH; @@ -1447,11 +1446,7 @@ static enum match_type cmd_ipv6_prefix_match(const char *str) if (state < STATE_MASK) return PARTLY_MATCH; - mask = strtol(str, &endptr, 10); - if (*endptr != '\0') - return NO_MATCH; - - if (mask < 0 || mask > 128) + if (osmo_str_to_int(&mask, str, 10, 0, 128)) return NO_MATCH; /* I don't know why mask < 13 makes command match partly. @@ -3794,16 +3789,7 @@ DEFUN(config_terminal_length, config_terminal_length_cmd, "Set number of lines on a screen\n" "Number of lines on screen (0 for no pausing)\n") { - int lines; - char *endptr = NULL; - - lines = strtol(argv[0], &endptr, 10); - if (lines < 0 || lines > 512 || *endptr != '\0') { - vty_out(vty, "length is malformed%s", VTY_NEWLINE); - return CMD_WARNING; - } - vty->lines = lines; - + vty->lines = atoi(argv[0]); return CMD_SUCCESS; } @@ -3822,16 +3808,7 @@ DEFUN(service_terminal_length, service_terminal_length_cmd, "System wide terminal length configuration\n" "Number of lines of VTY (0 means no line control)\n") { - int lines; - char *endptr = NULL; - - lines = strtol(argv[0], &endptr, 10); - if (lines < 0 || lines > 512 || *endptr != '\0') { - vty_out(vty, "length is malformed%s", VTY_NEWLINE); - return CMD_WARNING; - } - host.lines = lines; - + host.lines = atoi(argv[0]); return CMD_SUCCESS; } diff --git a/src/vty/cpu_sched_vty.c b/src/vty/cpu_sched_vty.c index 4ccc6274..0b4b2492 100644 --- a/src/vty/cpu_sched_vty.c +++ b/src/vty/cpu_sched_vty.c @@ -276,7 +276,6 @@ static bool proc_name_exists(const char *name, pid_t *res_pid) static enum sched_vty_thread_id procname2pid(pid_t *res_pid, const char *str, bool applynow) { size_t i, len; - char *end; bool is_pid = true; if (strcmp(str, "all") == 0) { @@ -297,12 +296,12 @@ static enum sched_vty_thread_id procname2pid(pid_t *res_pid, const char *str, bo } } if (is_pid) { - errno = 0; - *res_pid = strtoul(str, &end, 0); - if ((errno == ERANGE && *res_pid == ULONG_MAX) || (errno && !*res_pid) || - str == end) { + int64_t val; + if (osmo_str_to_int64(&val, str, 0, 0, INT64_MAX)) + return SCHED_VTY_THREAD_UNKNOWN; + *res_pid = (pid_t)val; + if (*res_pid != val) return SCHED_VTY_THREAD_UNKNOWN; - } if (!applynow || proc_tid_exists(*res_pid)) return SCHED_VTY_THREAD_ID; else diff --git a/src/vty/tdef_vty.c b/src/vty/tdef_vty.c index 0556d8c9..09459f1d 100644 --- a/src/vty/tdef_vty.c +++ b/src/vty/tdef_vty.c @@ -50,10 +50,9 @@ */ struct osmo_tdef *osmo_tdef_vty_parse_T_arg(struct vty *vty, struct osmo_tdef *tdefs, const char *T_str) { - long l; + int l; int T; struct osmo_tdef *t; - char *endptr; const char *T_nr_str; int sign = 1; @@ -77,9 +76,7 @@ struct osmo_tdef *osmo_tdef_vty_parse_T_arg(struct vty *vty, struct osmo_tdef *t return NULL; } - errno = 0; - l = strtol(T_nr_str, &endptr, 10); - if (errno || *endptr || l > INT_MAX || l < 0) { + if (osmo_str_to_int(&l, T_nr_str, 10, 0, INT_MAX)) { vty_out(vty, "%% Invalid T timer argument (should be 'T1234' or 'X1234'): '%s'%s", T_str, VTY_NEWLINE); return NULL; } |