aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSimon Warta <[email protected]>2015-08-11 10:02:32 +0200
committerSimon Warta <[email protected]>2015-08-11 10:02:51 +0200
commit7264369898fda03bfb3926d755bdb3370b6bf25d (patch)
treea54d164f4738a8808bdc43595dc01f4e8e7f806b
parent765f445f3cef73f5bd6647eab1b99c39a7ad24cb (diff)
Strict uint32 parsing
-rw-r--r--src/lib/utils/parsing.cpp10
-rw-r--r--src/tests/catchy/test_utils.cpp17
2 files changed, 20 insertions, 7 deletions
diff --git a/src/lib/utils/parsing.cpp b/src/lib/utils/parsing.cpp
index 605823082..4feea8d60 100644
--- a/src/lib/utils/parsing.cpp
+++ b/src/lib/utils/parsing.cpp
@@ -20,6 +20,16 @@ u32bit to_u32bit(const std::string& str)
{
try
{
+ // std::stoul is not strict enough. Ensure that str is digit only [0-9]*
+ for (const char chr : str)
+ {
+ if (chr < '0' || chr > '9')
+ {
+ auto chrAsString = std::string(1, chr);
+ throw Invalid_Argument("String contains non-digit char: " + chrAsString);
+ }
+ }
+
const auto integerValue = std::stoul(str);
// integerValue might be uint64
diff --git a/src/tests/catchy/test_utils.cpp b/src/tests/catchy/test_utils.cpp
index b63076c4f..c2b22c34a 100644
--- a/src/tests/catchy/test_utils.cpp
+++ b/src/tests/catchy/test_utils.cpp
@@ -190,13 +190,16 @@ TEST_CASE("uint32 parsing valid", "[utils]")
CHECK_THAT(to_u32bit("0000000000000000000000000010"), Equals(10));
// leading and trailing whitespace
- CHECK_THAT(to_u32bit(" 1"), Equals(1));
- CHECK_THAT(to_u32bit(" 1 "), Equals(1));
- CHECK_THAT(to_u32bit("\n1"), Equals(1));
- CHECK_THAT(to_u32bit("1\n"), Equals(1));
- CHECK_THAT(to_u32bit("1 5"), Equals(1));
- CHECK_THAT(to_u32bit("1\t5"), Equals(1));
- CHECK_THAT(to_u32bit("1\n5"), Equals(1));
+ CHECK_THROWS(to_u32bit(" 1"));
+ CHECK_THROWS(to_u32bit(" 1 "));
+ CHECK_THROWS(to_u32bit("\n1"));
+ CHECK_THROWS(to_u32bit("1\n"));
+ CHECK_THROWS(to_u32bit("1 5"));
+ CHECK_THROWS(to_u32bit("1\t5"));
+ CHECK_THROWS(to_u32bit("1\n5"));
+
+ // Other stuff that is no digit
+ CHECK_THROWS(to_u32bit("1Z"));
// invalid input
CHECK_THROWS(to_u32bit(""));