Browse Source

Don't use atoi()

Serj Kalichev 3 years ago
parent
commit
43caeef009

+ 11 - 15
bin/clish.c

@@ -36,6 +36,7 @@
 #include "lub/list.h"
 #include "lub/system.h"
 #include "lub/log.h"
+#include "lub/conv.h"
 #include "clish/shell.h"
 
 #define QUOTE(t) #t
@@ -71,7 +72,7 @@ int main(int argc, char **argv)
 
 	FILE *outfd = stdout;
 	bool_t istimeout = BOOL_FALSE;
-	int timeout = 0;
+	unsigned int timeout = 0;
 	bool_t cmd = BOOL_FALSE; /* -c option */
 	lub_list_t *cmds; /* Commands defined by -c */
 	lub_list_node_t *iter;
@@ -189,14 +190,15 @@ int main(int argc, char **argv)
 			break;
 		case 't':
 			istimeout = BOOL_TRUE;
-			timeout = atoi(optarg);
+			timeout = 0;
+			lub_conv_atoui(optarg, &timeout, 0);
 			break;
 		case 'c': {
-				char *str;
-				cmd = BOOL_TRUE;
-				quiet = BOOL_TRUE;
-				str = strdup(optarg);
-				lub_list_add(cmds, str);
+			char *str;
+			cmd = BOOL_TRUE;
+			quiet = BOOL_TRUE;
+			str = strdup(optarg);
+			lub_list_add(cmds, str);
 			}
 			break;
 		case 'f':
@@ -206,14 +208,8 @@ int main(int argc, char **argv)
 				histfile = optarg;
 			break;
 		case 'z': {
-				int itmp = 0;
-				itmp = atoi(optarg);
-				if (itmp <= 0) {
-					fprintf(stderr, "Error: Illegal histsize option value.\n");
-					help(-1, argv[0]);
-					goto end;
-				}
-				histsize = itmp;
+			histsize = 0;
+			lub_conv_atoui(optarg, &histsize, 0);
 			}
 			break;
 		case 'p':

+ 9 - 6
clish/ptype/ptype.c

@@ -5,6 +5,7 @@
 #include "lub/string.h"
 #include "lub/ctype.h"
 #include "lub/argv.h"
+#include "lub/conv.h"
 
 #include <stdlib.h>
 #include <string.h>
@@ -298,9 +299,10 @@ static char *clish_ptype_validate_or_translate(const clish_ptype_t * this,
 		}
 		if (BOOL_TRUE == ok) {
 			/* convert and check the range */
-			int value = atoi(result);
-			if ((value < this->u.integer.min)
-				|| (value > this->u.integer.max)) {
+			int value = 0;
+			if ((lub_conv_atoi(result, &value, 0) < 0) ||
+				(value < this->u.integer.min) ||
+				(value > this->u.integer.max)) {
 				lub_string_free(result);
 				result = NULL;
 			}
@@ -324,9 +326,10 @@ static char *clish_ptype_validate_or_translate(const clish_ptype_t * this,
 		}
 		if (BOOL_TRUE == ok) {
 			/* convert and check the range */
-			unsigned int value = (unsigned int)atoi(result);
-			if ((value < (unsigned)this->u.integer.min)
-				|| (value > (unsigned)this->u.integer.max)) {
+			unsigned int value = 0;
+			if ((lub_conv_atoui(result, &value, 0) < 0) ||
+				(value < (unsigned)this->u.integer.min) ||
+				(value > (unsigned)this->u.integer.max)) {
 				lub_string_free(result);
 				result = NULL;
 			}

+ 1 - 1
clish/shell.h

@@ -176,7 +176,7 @@ void clish_shell__set_interactive(clish_shell_t * instance, bool_t interactive);
 bool_t clish_shell__get_interactive(const clish_shell_t * instance);
 bool_t clish_shell__get_utf8(const clish_shell_t * instance);
 void clish_shell__set_utf8(clish_shell_t * instance, bool_t utf8);
-void clish_shell__set_timeout(clish_shell_t *instance, int timeout);
+void clish_shell__set_timeout(clish_shell_t *instance, unsigned int timeout);
 char *clish_shell__get_line(clish_context_t *context);
 char *clish_shell__get_full_line(clish_context_t *context);
 char *clish_shell__get_params(clish_context_t *context);

+ 1 - 1
clish/shell/shell_tinyrl.c

@@ -587,7 +587,7 @@ void clish_shell__set_utf8(clish_shell_t * this, bool_t utf8)
 }
 
 /*-------------------------------------------------------- */
-void clish_shell__set_timeout(clish_shell_t *this, int timeout)
+void clish_shell__set_timeout(clish_shell_t *this, unsigned int timeout)
 {
 	assert(this);
 	this->idle_timeout = timeout;

+ 4 - 3
clish/shell/shell_var.c

@@ -10,6 +10,7 @@
 #include <unistd.h>
 
 #include "lub/string.h"
+#include "lub/conv.h"
 #include "private.h"
 
 /*----------------------------------------------------------- */
@@ -117,12 +118,12 @@ static char *find_context_var(const char *name, clish_context_t *this)
 		result = strdup(tmp);
 
 	} else if (lub_string_nocasestr(name, "_prefix") == name) {
-		int idx = 0;
-		int pnum = 0;
+		unsigned int idx = 0;
+		unsigned int pnum = 0;
 		pnum = lub_string_wordcount(clish_command__get_name(this->cmd)) -
 			lub_string_wordcount(clish_command__get_name(
 			clish_command__get_cmd(this->cmd)));
-		idx = atoi(name + strlen("_prefix"));
+		lub_conv_atoui(name + strlen("_prefix"), &idx, 0);
 		if (idx < pnum) {
 			lub_argv_t *argv = lub_argv_new(
 				clish_command__get_name(this->cmd), 0);

+ 10 - 16
clish/shell/shell_xml.c

@@ -11,6 +11,7 @@
 #include "lub/string.h"
 #include "lub/ctype.h"
 #include "lub/system.h"
+#include "lub/conv.h"
 #include <stdlib.h>
 #include <string.h>
 #include <assert.h>
@@ -334,7 +335,8 @@ static int process_view(clish_shell_t *shell, clish_xmlnode_t *element,
 	view = clish_shell_find_create_view(shell, name, prompt);
 
 	if (depth && (lub_ctype_isdigit(*depth))) {
-		unsigned res = atoi(depth);
+		unsigned int res = 0;
+		lub_conv_atoui(depth, &res, 0);
 		clish_view__set_depth(view, res);
 	}
 
@@ -613,8 +615,11 @@ static int process_startup(clish_shell_t *shell, clish_xmlnode_t *element,
 	if (default_shebang)
 		clish_shell__set_default_shebang(shell, default_shebang);
 
-	if (timeout)
-		clish_shell__set_timeout(shell, atoi(timeout));
+	if (timeout) {
+		unsigned int to = 0;
+		lub_conv_atoui(timeout, &to, 0);
+		clish_shell__set_timeout(shell, to);
+	}
 
 	/* lock field */
 	if (lock && lub_string_nocasecmp(lock, "false") == 0)
@@ -1008,19 +1013,8 @@ static int process_config(clish_shell_t *shell, clish_xmlnode_t *element,
 	}
 
 	if (priority) {
-		long val = 0;
-		char *endptr;
-		unsigned short pri;
-
-		val = strtol(priority, &endptr, 0);
-		if (endptr == priority)
-			pri = 0;
-		else if (val > 0xffff)
-			pri = 0xffff;
-		else if (val < 0)
-			pri = 0;
-		else
-			pri = (unsigned short)val;
+		unsigned short pri = 0;
+		lub_conv_atous(priority, &pri, 0);
 		clish_config__set_priority(config, pri);
 	}
 

+ 10 - 24
konf/query/query.c

@@ -21,6 +21,7 @@
 #include "lub/types.h"
 #include "lub/argv.h"
 #include "lub/string.h"
+#include "lub/conv.h"
 #include "private.h"
 
 /*-------------------------------------------------------- */
@@ -142,29 +143,19 @@ int konf_query_parse(konf_query_t *this, int argc, char **argv)
 			break;
 		case 'p':
 			{
-			long val = 0;
-			char *endptr;
-
-			val = strtol(optarg, &endptr, 0);
-			if (endptr == optarg)
-				break;
-			if ((val > 0xffff) || (val < 0))
+			unsigned short val = 0;
+			if (lub_conv_atous(optarg, &val, 0) < 0)
 				break;
-			this->priority = (unsigned short)val;
+			this->priority = val;
 			break;
 			}
 		case 'q':
 			{
-			long val = 0;
-			char *endptr;
-
+			unsigned short val = 0;
 			this->seq = BOOL_TRUE;
-			val = strtol(optarg, &endptr, 0);
-			if (endptr == optarg)
+			if (lub_conv_atous(optarg, &val, 0) < 0)
 				break;
-			if ((val > 0xffff) || (val < 0))
-				break;
-			this->seq_num = (unsigned short)val;
+			this->seq_num = val;
 			break;
 			}
 		case 'r':
@@ -184,15 +175,10 @@ int konf_query_parse(konf_query_t *this, int argc, char **argv)
 			break;
 		case 'h':
 			{
-			long val = 0;
-			char *endptr;
-
-			val = strtol(optarg, &endptr, 0);
-			if (endptr == optarg)
-				break;
-			if ((val > 0xffff) || (val < 0))
+			unsigned short val = 0;
+			if (lub_conv_atous(optarg, &val, 0) < 0)
 				break;
-			this->depth = (unsigned short)val;
+			this->depth = val;
 			break;
 			}
 		default:

+ 4 - 1
lub/module.am

@@ -15,7 +15,8 @@ nobase_include_HEADERS += \
     lub/system.h \
     lub/db.h \
     lub/ini.h \
-    lub/log.h
+    lub/log.h \
+    lub/conv.h
 
 EXTRA_DIST +=   \
     lub/argv/module.am \
@@ -28,6 +29,7 @@ EXTRA_DIST +=   \
     lub/db/module.am \
     lub/ini/module.am \
     lub/log/module.am \
+    lub/conv/module.am \
     lub/README
 
 include $(top_srcdir)/lub/argv/module.am
@@ -40,3 +42,4 @@ include $(top_srcdir)/lub/system/module.am
 include $(top_srcdir)/lub/db/module.am
 include $(top_srcdir)/lub/ini/module.am
 include $(top_srcdir)/lub/log/module.am
+include $(top_srcdir)/lub/conv/module.am

+ 2 - 16
plugins/clish/hook_config.c

@@ -18,6 +18,7 @@
 #include "konf/buf.h"
 #include "konf/query.h"
 #include "lub/string.h"
+#include "lub/conv.h"
 #include "clish/shell.h"
 
 static int send_request(konf_client_t * client, char *command);
@@ -26,22 +27,7 @@ static int send_request(konf_client_t * client, char *command);
 static unsigned short str2ushort(const char *str)
 {
 	unsigned short num = 0;
-
-	if (str && (*str != '\0')) {
-		long val = 0;
-		char *endptr;
-
-		val = strtol(str, &endptr, 0);
-		if (endptr == str)
-			num = 0;
-		else if (val > 0xffff)
-			num = 0xffff;
-		else if (val < 0)
-			num = 0;
-		else
-			num = (unsigned)val;
-	}
-
+	lub_conv_atous(str, &num, 0);
 	return num;
 }
 

+ 6 - 3
plugins/clish/sym_misc.c

@@ -4,6 +4,7 @@
 #include "private.h"
 #include "lub/string.h"
 #include "lub/argv.h"
+#include "lub/conv.h"
 
 #include <assert.h>
 #include <stdio.h>
@@ -117,11 +118,11 @@ CLISH_PLUGIN_SYM(clish_history)
 	tinyrl_history_t *history = tinyrl__get_history(tinyrl);
 	tinyrl_history_iterator_t iter;
 	const tinyrl_history_entry_t *entry;
-	unsigned limit = 0;
+	unsigned int limit = 0;
 	const char *arg = script;
 
 	if (arg && ('\0' != *arg)) {
-		limit = (unsigned)atoi(arg);
+		lub_conv_atoui(arg, &limit, 0);
 		if (0 == limit) {
 			/* unlimit the history list */
 			(void)tinyrl_history_unstifle(history);
@@ -190,6 +191,7 @@ CLISH_PLUGIN_SYM(clish_wdog)
 {
 	const char *arg = script;
 	clish_shell_t *this = clish_context__get_shell(clish_context);
+	unsigned int wdto = 0;
 
 	/* Turn off watchdog if no args */
 	if (!arg || ('\0' == *arg)) {
@@ -197,7 +199,8 @@ CLISH_PLUGIN_SYM(clish_wdog)
 		return 0;
 	}
 
-	clish_shell__set_wdog_timeout(this, (unsigned int)atoi(arg));
+	lub_conv_atoui(arg, &wdto, 0);
+	clish_shell__set_wdog_timeout(this, wdto);
 
 	out = out; /* Happy compiler */