summaryrefslogtreecommitdiff
path: root/src/test/test-parse-util.c
diff options
context:
space:
mode:
authorMartin Pitt <martin.pitt@ubuntu.com>2015-11-11 10:05:00 +0100
committerMartin Pitt <martin.pitt@ubuntu.com>2015-11-11 10:09:18 +0100
commit5c280bcc5a726e53d7108ad2bbed9cd47df6a7b6 (patch)
tree8c521d56df8e5635c6f4281b141b6ac4b18c063e /src/test/test-parse-util.c
parent7042fc14ff2bd30648aea6602c95d3cf6946e7be (diff)
test-parse-util: cover negative numbers, leading spaces, and more range errors
Motivated by https://github.com/systemd/systemd/issues/1829 where negative numbers were mis-handled on 32 bit platforms by safe_atou*().
Diffstat (limited to 'src/test/test-parse-util.c')
-rw-r--r--src/test/test-parse-util.c43
1 files changed, 43 insertions, 0 deletions
diff --git a/src/test/test-parse-util.c b/src/test/test-parse-util.c
index d3ae0599ab..f0d5d71083 100644
--- a/src/test/test-parse-util.c
+++ b/src/test/test-parse-util.c
@@ -352,6 +352,24 @@ static void test_safe_atolli(void) {
assert_se(r == 0);
assert_se(l == 12345);
+ r = safe_atolli(" 12345", &l);
+ assert_se(r == 0);
+ assert_se(l == 12345);
+
+ r = safe_atolli("-12345", &l);
+ assert_se(r == 0);
+ assert_se(l == -12345);
+
+ r = safe_atolli(" -12345", &l);
+ assert_se(r == 0);
+ assert_se(l == -12345);
+
+ r = safe_atolli("12345678901234567890", &l);
+ assert_se(r == -ERANGE);
+
+ r = safe_atolli("-12345678901234567890", &l);
+ assert_se(r == -ERANGE);
+
r = safe_atolli("junk", &l);
assert_se(r == -EINVAL);
}
@@ -364,9 +382,19 @@ static void test_safe_atou16(void) {
assert_se(r == 0);
assert_se(l == 12345);
+ r = safe_atou16(" 12345", &l);
+ assert_se(r == 0);
+ assert_se(l == 12345);
+
r = safe_atou16("123456", &l);
assert_se(r == -ERANGE);
+ r = safe_atou16("-1", &l);
+ assert_se(r == -ERANGE);
+
+ r = safe_atou16(" -1", &l);
+ assert_se(r == -ERANGE);
+
r = safe_atou16("junk", &l);
assert_se(r == -EINVAL);
}
@@ -379,9 +407,24 @@ static void test_safe_atoi16(void) {
assert_se(r == 0);
assert_se(l == -12345);
+ r = safe_atoi16(" -12345", &l);
+ assert_se(r == 0);
+ assert_se(l == -12345);
+
+ r = safe_atoi16("32767", &l);
+ assert_se(r == 0);
+ assert_se(l == 32767);
+
+ r = safe_atoi16(" 32767", &l);
+ assert_se(r == 0);
+ assert_se(l == 32767);
+
r = safe_atoi16("36536", &l);
assert_se(r == -ERANGE);
+ r = safe_atoi16("-32769", &l);
+ assert_se(r == -ERANGE);
+
r = safe_atoi16("junk", &l);
assert_se(r == -EINVAL);
}