Skip to content

Commit 2375187

Browse files
committed
Fix phpGH-14774 time_sleep_until overflow.
1 parent 0b6289d commit 2375187

File tree

3 files changed

+30
-0
lines changed

3 files changed

+30
-0
lines changed

NEWS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ PHP NEWS
4242

4343
- Standard:
4444
. Fix 32-bit wordwrap test failures. (orlitzky)
45+
. Fixed bug GH-14774 (time_sleep_until overflow). (David Carlier)
4546

4647
- Treewide:
4748
. Fix compatibility with libxml2 2.13.2. (nielsdos)

ext/standard/basic_functions.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1228,6 +1228,7 @@ PHP_FUNCTION(time_sleep_until)
12281228
struct timespec php_req, php_rem;
12291229
uint64_t current_ns, target_ns, diff_ns;
12301230
const uint64_t ns_per_sec = 1000000000;
1231+
const double top_target_sec = (double)(UINT64_MAX / ns_per_sec);
12311232

12321233
ZEND_PARSE_PARAMETERS_START(1, 1)
12331234
Z_PARAM_DOUBLE(target_secs)
@@ -1237,6 +1238,11 @@ PHP_FUNCTION(time_sleep_until)
12371238
RETURN_FALSE;
12381239
}
12391240

1241+
if (UNEXPECTED(!(target_secs >= 0 && target_secs <= top_target_sec))) {
1242+
zend_argument_value_error(1, "must be between 0 and %" PRIu64, (uint64_t)top_target_sec);
1243+
RETURN_THROWS();
1244+
}
1245+
12401246
target_ns = (uint64_t) (target_secs * ns_per_sec);
12411247
current_ns = ((uint64_t) tm.tv_sec) * ns_per_sec + ((uint64_t) tm.tv_usec) * 1000;
12421248
if (target_ns < current_ns) {

ext/standard/tests/misc/gh14774.phpt

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
--TEST--
2+
GH-14774 time_sleep_until overflow
3+
--SKIPIF--
4+
<?php
5+
if (PHP_INT_SIZE != 8) die("skip this test is for 64bit platform only");
6+
?>
7+
--FILE--
8+
<?php
9+
foreach([INF, -INF, 10e300, -10e300, NAN, -NAN] as $var) {
10+
try {
11+
time_sleep_until($var);
12+
} catch (\ValueError $e) {
13+
echo $e->getMessage() . PHP_EOL;
14+
}
15+
}
16+
?>
17+
--EXPECTF--
18+
time_sleep_until(): Argument #1 ($timestamp) must be between 0 and %d
19+
time_sleep_until(): Argument #1 ($timestamp) must be between 0 and %d
20+
time_sleep_until(): Argument #1 ($timestamp) must be between 0 and %d
21+
time_sleep_until(): Argument #1 ($timestamp) must be between 0 and %d
22+
time_sleep_until(): Argument #1 ($timestamp) must be between 0 and %d
23+
time_sleep_until(): Argument #1 ($timestamp) must be between 0 and %d

0 commit comments

Comments
 (0)