summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authordtucker <dtucker@openbsd.org>2021-01-15 02:58:11 +0000
committerdtucker <dtucker@openbsd.org>2021-01-15 02:58:11 +0000
commit6684a37183d70311324effbd0688f5b22ce17e16 (patch)
treea04e3a56186524e6c25541b27211e331670f840f
parentIn waitfd(), when poll returns early we are subtracting the elapsed time (diff)
downloadwireguard-openbsd-6684a37183d70311324effbd0688f5b22ce17e16.tar.xz
wireguard-openbsd-6684a37183d70311324effbd0688f5b22ce17e16.zip
Change types in convtime() unit test to int to match change its new type.
Add tests for boundary conditions and fix convtime to work up to INT_MAX. ok djm@
-rw-r--r--regress/usr.bin/ssh/unittests/misc/tests.c43
-rw-r--r--usr.bin/ssh/misc.c6
2 files changed, 30 insertions, 19 deletions
diff --git a/regress/usr.bin/ssh/unittests/misc/tests.c b/regress/usr.bin/ssh/unittests/misc/tests.c
index 0bd0c84f961..d873dc24154 100644
--- a/regress/usr.bin/ssh/unittests/misc/tests.c
+++ b/regress/usr.bin/ssh/unittests/misc/tests.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: tests.c,v 1.3 2020/05/29 04:32:26 dtucker Exp $ */
+/* $OpenBSD: tests.c,v 1.4 2021/01/15 02:58:11 dtucker Exp $ */
/*
* Regress test for misc helper functions.
*
@@ -22,6 +22,7 @@ tests(void)
{
int port, parseerr;
char *user, *host, *path, *ret;
+ char buf[1024];
TEST_START("misc_parse_user_host_path");
ASSERT_INT_EQ(parse_user_host_path("someuser@some.host:some/path",
@@ -79,22 +80,32 @@ tests(void)
TEST_DONE();
TEST_START("misc_convtime");
- ASSERT_LONG_EQ(convtime("1"), 1);
- ASSERT_LONG_EQ(convtime("2s"), 2);
- ASSERT_LONG_EQ(convtime("3m"), 180);
- ASSERT_LONG_EQ(convtime("1m30"), 90);
- ASSERT_LONG_EQ(convtime("1m30s"), 90);
- ASSERT_LONG_EQ(convtime("1h1s"), 3601);
- ASSERT_LONG_EQ(convtime("1h30m"), 90 * 60);
- ASSERT_LONG_EQ(convtime("1d"), 24 * 60 * 60);
- ASSERT_LONG_EQ(convtime("1w"), 7 * 24 * 60 * 60);
- ASSERT_LONG_EQ(convtime("1w2d3h4m5"), 788645);
- ASSERT_LONG_EQ(convtime("1w2d3h4m5s"), 788645);
+ ASSERT_INT_EQ(convtime("0"), 0);
+ ASSERT_INT_EQ(convtime("1"), 1);
+ ASSERT_INT_EQ(convtime("2s"), 2);
+ ASSERT_INT_EQ(convtime("3m"), 180);
+ ASSERT_INT_EQ(convtime("1m30"), 90);
+ ASSERT_INT_EQ(convtime("1m30s"), 90);
+ ASSERT_INT_EQ(convtime("1h1s"), 3601);
+ ASSERT_INT_EQ(convtime("1h30m"), 90 * 60);
+ ASSERT_INT_EQ(convtime("1d"), 24 * 60 * 60);
+ ASSERT_INT_EQ(convtime("1w"), 7 * 24 * 60 * 60);
+ ASSERT_INT_EQ(convtime("1w2d3h4m5"), 788645);
+ ASSERT_INT_EQ(convtime("1w2d3h4m5s"), 788645);
/* any negative number or error returns -1 */
- ASSERT_LONG_EQ(convtime("-1"), -1);
- ASSERT_LONG_EQ(convtime(""), -1);
- ASSERT_LONG_EQ(convtime("trout"), -1);
- ASSERT_LONG_EQ(convtime("-77"), -1);
+ ASSERT_INT_EQ(convtime("-1"), -1);
+ ASSERT_INT_EQ(convtime(""), -1);
+ ASSERT_INT_EQ(convtime("trout"), -1);
+ ASSERT_INT_EQ(convtime("-77"), -1);
+ /* boundary conditions */
+ snprintf(buf, sizeof buf, "%llu", (long long unsigned)INT_MAX);
+ ASSERT_INT_EQ(convtime(buf), INT_MAX);
+ snprintf(buf, sizeof buf, "%llu", (long long unsigned)INT_MAX + 1);
+ ASSERT_INT_EQ(convtime(buf), -1);
+ ASSERT_INT_EQ(convtime("3550w5d3h14m7s"), 2147483647);
+#if INT_MAX == 2147483647
+ ASSERT_INT_EQ(convtime("3550w5d3h14m8s"), -1);
+#endif
TEST_DONE();
TEST_START("dollar_expand");
diff --git a/usr.bin/ssh/misc.c b/usr.bin/ssh/misc.c
index f793ec9f33e..9dfa7977787 100644
--- a/usr.bin/ssh/misc.c
+++ b/usr.bin/ssh/misc.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: misc.c,v 1.159 2021/01/15 02:32:41 dtucker Exp $ */
+/* $OpenBSD: misc.c,v 1.160 2021/01/15 02:58:11 dtucker Exp $ */
/*
* Copyright (c) 2000 Markus Friedl. All rights reserved.
* Copyright (c) 2005-2020 Damien Miller. All rights reserved.
@@ -545,10 +545,10 @@ convtime(const char *s)
default:
return -1;
}
- if (secs >= INT_MAX / multiplier)
+ if (secs > INT_MAX / multiplier)
return -1;
secs *= multiplier;
- if (total >= INT_MAX - secs)
+ if (total > INT_MAX - secs)
return -1;
total += secs;
if (total < 0)