diff options
author | Pau Espin Pedrol <pespin@sysmocom.de> | 2018-07-12 18:34:11 +0200 |
---|---|---|
committer | Pau Espin Pedrol <pespin@sysmocom.de> | 2018-07-12 20:46:37 +0200 |
commit | 746ebf31e1290e85b632369885647bb8ea074c8c (patch) | |
tree | 9c6547411c1c63b9d7b154e0290372c8d3a312ee /openbsc | |
parent | c1a6a967c3b47db74d0f501841932fe1a7d75fef (diff) |
nat: ctrl: use strtol instead of atoi as it has explicit error documentation
In some cases id can be non-digit such as "err" for ERROR cmds generated
from parsing failures.
Change-Id: Ief0b203efbcf2be04253b5056840be94d58a9994
Diffstat (limited to 'openbsc')
-rw-r--r-- | openbsc/src/osmo-bsc_nat/bsc_nat_ctrl.c | 15 |
1 files changed, 13 insertions, 2 deletions
diff --git a/openbsc/src/osmo-bsc_nat/bsc_nat_ctrl.c b/openbsc/src/osmo-bsc_nat/bsc_nat_ctrl.c index af110a204..7a5e9d0ca 100644 --- a/openbsc/src/osmo-bsc_nat/bsc_nat_ctrl.c +++ b/openbsc/src/osmo-bsc_nat/bsc_nat_ctrl.c @@ -36,6 +36,7 @@ #include <unistd.h> #include <string.h> #include <errno.h> +#include <limits.h> #define NAT_MAX_CTRL_ID 65535 @@ -85,10 +86,20 @@ void bsc_nat_ctrl_del_pending(struct bsc_cmd_list *pending) static struct bsc_cmd_list *bsc_get_pending(struct bsc_connection *bsc, char *id_str) { struct bsc_cmd_list *cmd_entry; - int id = atoi(id_str); - if (id == 0) + char * endptr = NULL; + long int long_id; + int id; + + errno = 0; + long_id = (int) strtol(id_str, &endptr, 10); + /* check parse errors */ + if (id_str[0] == '\0' || endptr[0] != '\0') + return NULL; + /* check value store errors */ + if (errno == ERANGE || long_id > INT_MAX || long_id < 0) return NULL; + id = (int) long_id; llist_for_each_entry(cmd_entry, &bsc->cmd_pending, list_entry) { if (cmd_entry->nat_id == id) { return cmd_entry; |