[PATCH libinput 1/2] util: add a safe_atoi_base() function

Peter Hutterer peter.hutterer at who-t.net
Thu Aug 31 01:21:03 UTC 2017


For parsing hex numbers

Signed-off-by: Peter Hutterer <peter.hutterer at who-t.net>
---
 src/libinput-util.h | 12 +++++++--
 test/test-misc.c    | 73 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 83 insertions(+), 2 deletions(-)

diff --git a/src/libinput-util.h b/src/libinput-util.h
index bf632a5c..139d6932 100644
--- a/src/libinput-util.h
+++ b/src/libinput-util.h
@@ -473,13 +473,15 @@ tv2us(const struct timeval *tv)
 }
 
 static inline bool
-safe_atoi(const char *str, int *val)
+safe_atoi_base(const char *str, int *val, int base)
 {
 	char *endptr;
 	long v;
 
+	assert(base == 10 || base == 16 || base == 8);
+
 	errno = 0;
-	v = strtol(str, &endptr, 10);
+	v = strtol(str, &endptr, base);
 	if (errno > 0)
 		return false;
 	if (str == endptr)
@@ -495,6 +497,12 @@ safe_atoi(const char *str, int *val)
 }
 
 static inline bool
+safe_atoi(const char *str, int *val)
+{
+	return safe_atoi_base(str, val, 10);
+}
+
+static inline bool
 safe_atod(const char *str, double *val)
 {
 	char *endptr;
diff --git a/test/test-misc.c b/test/test-misc.c
index 2173910b..13d65144 100644
--- a/test/test-misc.c
+++ b/test/test-misc.c
@@ -1123,6 +1123,77 @@ START_TEST(safe_atoi_test)
 }
 END_TEST
 
+START_TEST(safe_atoi_base_16_test)
+{
+	struct atoi_test tests[] = {
+		{ "10", true, 0x10 },
+		{ "20", true, 0x20 },
+		{ "-1", true, -1 },
+		{ "0x10", true, 0x10 },
+		{ "0xff", true, 0xff },
+		{ "abc", true, 0xabc },
+		{ "-10", true, -0x10 },
+		{ "0x0", true, 0 },
+		{ "0", true, 0 },
+		{ "0x-99", false, 0 },
+		{ "0xak", false, 0 },
+		{ "0x", false, 0 },
+		{ "x10", false, 0 },
+		{ NULL, false, 0 }
+	};
+
+	int v;
+	bool success;
+
+	for (int i = 0; tests[i].str != NULL; i++) {
+		v = 0xad;
+		success = safe_atoi_base(tests[i].str, &v, 16);
+		ck_assert(success == tests[i].success);
+		if (success)
+			ck_assert_int_eq(v, tests[i].val);
+		else
+			ck_assert_int_eq(v, 0xad);
+	}
+}
+END_TEST
+
+START_TEST(safe_atoi_base_8_test)
+{
+	struct atoi_test tests[] = {
+		{ "7", true, 07 },
+		{ "10", true, 010 },
+		{ "20", true, 020 },
+		{ "-1", true, -1 },
+		{ "010", true, 010 },
+		{ "0ff", false, 0 },
+		{ "abc", false, 0},
+		{ "0xabc", false, 0},
+		{ "-10", true, -010 },
+		{ "0", true, 0 },
+		{ "00", true, 0 },
+		{ "0x0", false, 0 },
+		{ "0x-99", false, 0 },
+		{ "0xak", false, 0 },
+		{ "0x", false, 0 },
+		{ "x10", false, 0 },
+		{ NULL, false, 0 }
+	};
+
+	int v;
+	bool success;
+
+	for (int i = 0; tests[i].str != NULL; i++) {
+		v = 0xad;
+		success = safe_atoi_base(tests[i].str, &v, 8);
+		ck_assert(success == tests[i].success);
+		if (success)
+			ck_assert_int_eq(v, tests[i].val);
+		else
+			ck_assert_int_eq(v, 0xad);
+	}
+}
+END_TEST
+
 struct atod_test {
 	char *str;
 	bool success;
@@ -1376,6 +1447,8 @@ litest_setup_tests_misc(void)
 	litest_add_no_device("misc:parser", range_prop_parser);
 	litest_add_no_device("misc:parser", palm_pressure_parser);
 	litest_add_no_device("misc:parser", safe_atoi_test);
+	litest_add_no_device("misc:parser", safe_atoi_base_16_test);
+	litest_add_no_device("misc:parser", safe_atoi_base_8_test);
 	litest_add_no_device("misc:parser", safe_atod_test);
 	litest_add_no_device("misc:parser", strsplit_test);
 	litest_add_no_device("misc:time", time_conversion);
-- 
2.13.5



More information about the wayland-devel mailing list