From 5c280bcc5a726e53d7108ad2bbed9cd47df6a7b6 Mon Sep 17 00:00:00 2001 From: Martin Pitt Date: Wed, 11 Nov 2015 10:05:00 +0100 Subject: 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*(). --- src/test/test-parse-util.c | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) (limited to 'src') 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); } -- cgit v1.2.3-54-g00ecf