Replace unreliable atoi() with more reliable strtol_i()/strtoul_ui().
Signed-off-by: Vitezslav Samel <vitezslav(a)samel.cz>
---
src/ipfilter.c | 16 ++++------------
src/options.c | 9 +++++++--
src/serv.c | 34 +++++++++++++++++++++++++++++-----
3 files changed, 40 insertions(+), 19 deletions(-)
diff --git a/src/ipfilter.c b/src/ipfilter.c
index d5b7dd6..cffc81d 100644
--- a/src/ipfilter.c
+++ b/src/ipfilter.c
@@ -191,16 +191,12 @@ void gethostparams(struct hostparams *data, char *init_saddr, char
*init_smask,
* Process Source Port fields
*/
fieldptr = fieldptr->nextfield;
- if (fieldptr->buf[0] == '\0')
+ if (strtoul_ui(fieldptr->buf, 10, &data->sport1) == -1)
data->sport1 = 0;
- else
- data->sport1 = atoi(fieldptr->buf);
fieldptr = fieldptr->nextfield;
- if (fieldptr->buf[0] == '\0')
+ if (strtoul_ui(fieldptr->buf, 10, &data->sport2) == -1)
data->sport2 = 0;
- else
- data->sport2 = atoi(fieldptr->buf);
/*
* Process Destination Address field
@@ -236,16 +232,12 @@ void gethostparams(struct hostparams *data, char *init_saddr, char
*init_smask,
* Process Dedination Port fields
*/
fieldptr = fieldptr->nextfield;
- if (fieldptr->buf[0] == '\0')
+ if (strtoul_ui(fieldptr->buf, 10, &data->dport1) == -1)
data->dport1 = 0;
- else
- data->dport1 = atoi(fieldptr->buf);
fieldptr = fieldptr->nextfield;
- if (fieldptr->buf[0] == '\0')
+ if (strtoul_ui(fieldptr->buf, 10, &data->dport2) == -1)
data->dport2 = 0;
- else
- data->dport2 = atoi(fieldptr->buf);
/*
* Process IP protocol filter fields
diff --git a/src/options.c b/src/options.c
index e44aeca..7c9a858 100644
--- a/src/options.c
+++ b/src/options.c
@@ -229,9 +229,14 @@ static void settimeout(time_t *value, const char *units, int
allow_zero,
tx_fillfields(&field, aborted);
if (!(*aborted)) {
- tmval = atoi(field.list->buf);
- if ((!allow_zero) && (tmval == 0))
+ unsigned int tm;
+
+ tmval = 0;
+ int ret = strtoul_ui(field.list->buf, 10, &tm);
+ if ((ret == -1) || (!allow_zero && (tm == 0)))
tui_error(ANYKEY_MSG, "Invalid timeout value");
+ else
+ tmval = tm;
}
} while (((!allow_zero) && (tmval == 0)) && (!(*aborted)));
diff --git a/src/serv.c b/src/serv.c
index 92fb09e..c7f38d5 100644
--- a/src/serv.c
+++ b/src/serv.c
@@ -1180,12 +1180,36 @@ static void portdlg(unsigned int *port_min, unsigned int
*port_max,
tx_addfield(&list, 5, 0, 0, "");
tx_addfield(&list, 5, 0, 9, "");
- tx_fillfields(&list, aborted);
+ int ok;
+ do {
+ unsigned int val;
+ int ret;
- if (!(*aborted)) {
- *port_min = atoi(list.list->buf);
- *port_max = atoi(list.list->nextfield->buf);
- }
+ ok = 1;
+ tx_fillfields(&list, aborted);
+
+ if (*aborted)
+ break;
+
+ ret = strtoul_ui(list.list->buf, 10, &val);
+ if (ret == -1 || val > 65535) {
+ tui_error(ANYKEY_MSG, "Invalid port");
+ ok = 0;
+ continue;
+ }
+ *port_min = val;
+
+ if (list.list->nextfield->buf[0] != '\0') {
+ ret = strtoul_ui(list.list->nextfield->buf, 10, &val);
+ if (ret == -1 || val > 65535 || *port_min > val) {
+ tui_error(ANYKEY_MSG, "Invalid port");
+ ok = 0;
+ continue;
+ }
+ *port_max = val;
+ } else
+ *port_max = 0;
+ } while (!ok);
del_panel(bp);
delwin(bw);
del_panel(panel);
--
1.7.8.4