From 52c4c3d953008cc89af230651568f2081e132c9b Mon Sep 17 00:00:00 2001 From: Dmitry Podgorny Date: Wed, 14 Nov 2012 00:23:06 +0200 Subject: introduce _strtoi() and validation of typed numbers --- src/command.c | 44 +++++++++++++++++++++++++++++++++++++------- 1 file changed, 37 insertions(+), 7 deletions(-) (limited to 'src/command.c') diff --git a/src/command.c b/src/command.c index bf7ae5be..1ca20c47 100644 --- a/src/command.c +++ b/src/command.c @@ -22,6 +22,8 @@ #include #include +#include +#include #include @@ -79,6 +81,8 @@ static void _notify_autocomplete(char *input, int *size); static void _parameter_autocomplete(char *input, int *size, char *command, autocomplete_func func); +static int _strtoi(char *str, int *saveptr, int min, int max); + // command prototypes static gboolean _cmd_quit(const char * const inp, struct cmd_help_t help); static gboolean _cmd_help(const char * const inp, struct cmd_help_t help); @@ -1404,14 +1408,17 @@ _cmd_set_log(const char * const inp, struct cmd_help_t help) value = strtok(NULL, " "); if (subcmd == NULL || value == NULL) { cons_show("Usage: %s", help.usage); - } else { - if (strcmp(subcmd, "maxsize") == 0) { - intval = atoi(value); + return TRUE; + } + + if (strcmp(subcmd, "maxsize") == 0) { + if (_strtoi(value, &intval, PREFS_MIN_LOG_SIZE, INT_MAX) == 0) { prefs_set_max_log_size(intval); cons_show("Log maxinum size set to %d bytes", intval); } - /* TODO: make 'level' subcommand for debug level */ } + /* TODO: make 'level' subcommand for debug level */ + return TRUE; } @@ -1427,10 +1434,12 @@ _cmd_set_priority(const char * const inp, struct cmd_help_t help) value = strtok(NULL, " "); if (value == NULL) { cons_show("Usage: %s", help.usage); - } else { + return TRUE; + } + + if (_strtoi(value, &intval, -128, 127) == 0) { char *status = jabber_get_status(); - intval = atoi(value); - prefs_set_priority(intval); + prefs_set_priority((int)intval); // update presence with new priority jabber_update_presence(jabber_get_presence(), status); if (status != NULL) @@ -1702,3 +1711,24 @@ _notify_autocomplete(char *input, int *size) } } } + +static int +_strtoi(char *str, int *saveptr, int min, int max) +{ + char *ptr; + int val; + + errno = 0; + val = (int)strtol(str, &ptr, 0); + if (*str == '\0' || *ptr != '\0') { + cons_show("Illegal character. Must be a number."); + return -1; + } else if (errno == ERANGE || val < min || val > max) { + cons_show("Value out of range. Must be in %d..%d.", min, max); + return -1; + } + + *saveptr = val; + + return 0; +} -- cgit 1.4.1-2-gfad0