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
This commit is contained in:
Neels Hofmeyr 2021-09-05 19:50:34 +02:00 committed by neels
parent 6b60d52abf
commit 34907fe6e1
7 changed files with 31 additions and 73 deletions

View File

@ -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;
}

View File

@ -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)

View File

@ -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])
if (osmo_str_to_int(&val, str, base, 0, INT16_MAX))
return -1;
errno = 0;
val = strtoull(str, &endp, base);
if (errno || *endp != '\0')
return -1;
if (val < 0 || val > INT16_MAX)
return -1;
*dst = val;
*dst = (int16_t)val;
return 0;
}

View File

@ -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;
}

View File

@ -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

View File

@ -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;
}

View File

@ -123,6 +123,7 @@ static int handle_options(int argc, char **argv)
bool opc_is_set = false;
bool amf_is_set = false;
bool opc_is_op = false;
int64_t val64;
while (1) {
int c;
@ -169,7 +170,8 @@ static int handle_options(int argc, char **argv)
amf_is_set = true;
break;
case 's':
g_sqn = strtoull(optarg, 0, 10);
rc = osmo_str_to_int64(&val64, optarg, 10, 0, INT64_MAX);
g_sqn = (unsigned long long)val64;
sqn_is_set = true;
break;
case 'r':