[Asterisk-code-review] core/conversions: Added a string to unsigned integer conversion (asterisk[master])

Kevin Harwell asteriskteam at digium.com
Mon May 15 13:33:06 CDT 2017


Kevin Harwell has uploaded a new change for review. ( https://gerrit.asterisk.org/5628 )

Change subject: core/conversions: Added a string to unsigned integer conversion
......................................................................

core/conversions: Added a string to unsigned integer conversion

Added a function that converts a string to an unsigned integer. Also added a
unit test to test it.

Change-Id: If7eaca4a48f8c7b89cc8b5a1f4bed2852fca82bb
---
A include/asterisk/conversions.h
A main/conversions.c
A tests/test_conversions.c
3 files changed, 179 insertions(+), 0 deletions(-)


  git pull ssh://gerrit.asterisk.org:29418/asterisk refs/changes/28/5628/1

diff --git a/include/asterisk/conversions.h b/include/asterisk/conversions.h
new file mode 100644
index 0000000..c1d9c5e
--- /dev/null
+++ b/include/asterisk/conversions.h
@@ -0,0 +1,36 @@
+/*
+ * Asterisk -- An open source telephony toolkit.
+ *
+ * Copyright (C) 2017, Digium, Inc.
+ *
+ * Kevin Harwell <kharwell at digium.com>
+ *
+ * See http://www.asterisk.org for more information about
+ * the Asterisk project. Please do not directly contact
+ * any of the maintainers of this project for assistance;
+ * the project provides a web site, mailing lists and IRC
+ * channels for your use.
+ *
+ * This program is free software, distributed under the terms of
+ * the GNU General Public License Version 2. See the LICENSE file
+ * at the top of the source tree.
+ */
+
+/*! \file
+ * \brief Conversion utility functions
+ */
+
+#ifndef _ASTERISK_CONVERSIONS_H
+#define _ASTERISK_CONVERSIONS_H
+
+/*!
+ * \brief Convert the given string to an unsigned integer
+ *
+ * \param str The string to convert
+ * \param res [out] The converted value
+ *
+ * \returns -1 if it fails to convert, 0 on success
+ */
+int ast_str_to_uint(const char *str, unsigned int *res);
+
+#endif /* _ASTERISK_CONVERSIONS_H */
diff --git a/main/conversions.c b/main/conversions.c
new file mode 100644
index 0000000..f884278
--- /dev/null
+++ b/main/conversions.c
@@ -0,0 +1,57 @@
+/*
+ * Asterisk -- An open source telephony toolkit.
+ *
+ * Copyright (C) 2017, Digium, Inc.
+ *
+ * See http://www.asterisk.org for more information about
+ * the Asterisk project. Please do not directly contact
+ * any of the maintainers of this project for assistance;
+ * the project provides a web site, mailing lists and IRC
+ * channels for your use.
+ *
+ * This program is free software, distributed under the terms of
+ * the GNU General Public License Version 2. See the LICENSE file
+ * at the top of the source tree.
+ */
+
+/*! \file
+ *
+ * \brief Conversion utility functions
+ */
+
+/*** MODULEINFO
+	<support_level>core</support_level>
+ ***/
+
+#include <errno.h>
+#include <limits.h>
+#include <stdlib.h>
+
+#include "asterisk/conversions.h"
+
+int ast_str_to_uint(const char *str, unsigned int *res)
+{
+	char *end;
+	unsigned long val;
+
+	if (!str || *str == '-') {
+		return -1;
+	}
+
+	errno = 0;
+	val = strtoul(str, &end, 0);
+
+	/*
+	 * If str equals end then no digits were found. If end is not pointing to
+	 * a null character then the string contained some numbers that could be
+	 * converted, but some characters that could not (which we'll consider
+	 * invalid).
+	 */
+	if (str == end || *end != '\0' || ((errno == ERANGE &&
+		(val == LONG_MIN || val == LONG_MAX))) || val > UINT_MAX) {
+		return -1;
+	}
+
+	*res = val;
+	return 0;
+}
diff --git a/tests/test_conversions.c b/tests/test_conversions.c
new file mode 100644
index 0000000..77f2db5
--- /dev/null
+++ b/tests/test_conversions.c
@@ -0,0 +1,86 @@
+/*
+ * Asterisk -- An open source telephony toolkit.
+ *
+ * Copyright (C) 2017, Digium, Inc.
+ *
+ * Kevin Harwell <kharwell at digium.com>
+ *
+ * See http://www.asterisk.org for more information about
+ * the Asterisk project. Please do not directly contact
+ * any of the maintainers of this project for assistance;
+ * the project provides a web site, mailing lists and IRC
+ * channels for your use.
+ *
+ * This program is free software, distributed under the terms of
+ * the GNU General Public License Version 2. See the LICENSE file
+ * at the top of the source tree.
+ */
+
+/*!
+ * \file
+ * \brief Conversions Unit Tests
+ *
+ * \author Kevin Harwell <kharwell at digium.com>
+ *
+ */
+
+/*** MODULEINFO
+	<depend>TEST_FRAMEWORK</depend>
+	<support_level>core</support_level>
+ ***/
+
+#include "asterisk.h"
+
+#include "asterisk/test.h"
+#include "asterisk/module.h"
+#include "asterisk/conversions.h"
+
+#define CATEGORY "/main/conversions/"
+
+AST_TEST_DEFINE(str_to_uint)
+{
+	const char *invalid = "abc";
+	const char *invalid_partial = "7abc";
+	const char *negative = "-7";
+	const char *out_of_range = "9999999999";
+	const char *spaces = "  ";
+	const char *valid = "7";
+	const char *spaces_valid = "  7";
+	unsigned int val;
+
+	switch (cmd) {
+	case TEST_INIT:
+		info->name = __func__;
+		info->category = CATEGORY;
+		info->summary = "convert a string to an unsigned integer";
+		info->description = info->summary;
+		return AST_TEST_NOT_RUN;
+	case TEST_EXECUTE:
+		break;
+	}
+
+	ast_test_validate(test, ast_str_to_uint(NULL, &val));
+	ast_test_validate(test, ast_str_to_uint(invalid, &val));
+	ast_test_validate(test, ast_str_to_uint(invalid_partial, &val));
+	ast_test_validate(test, ast_str_to_uint(negative, &val));
+	ast_test_validate(test, ast_str_to_uint(out_of_range, &val));
+	ast_test_validate(test, ast_str_to_uint(spaces, &val));
+	ast_test_validate(test, !ast_str_to_uint(valid, &val));
+	ast_test_validate(test, !ast_str_to_uint(spaces_valid, &val));
+
+	return AST_TEST_PASS;
+}
+
+static int load_module(void)
+{
+	AST_TEST_REGISTER(str_to_uint);
+	return AST_MODULE_LOAD_SUCCESS;
+}
+
+static int unload_module(void)
+{
+	AST_TEST_UNREGISTER(str_to_uint);
+	return 0;
+}
+
+AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "URI test module");

-- 
To view, visit https://gerrit.asterisk.org/5628
To unsubscribe, visit https://gerrit.asterisk.org/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: If7eaca4a48f8c7b89cc8b5a1f4bed2852fca82bb
Gerrit-PatchSet: 1
Gerrit-Project: asterisk
Gerrit-Branch: master
Gerrit-Owner: Kevin Harwell <kharwell at digium.com>



More information about the asterisk-code-review mailing list