Skip to content

Prevent "interval" overflow in ExtUvLoop #196

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 12, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 26 additions & 3 deletions src/ExtUvLoop.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ public function addTimer($interval, $callback)
$this->timers->attach($timer, $event);
\uv_timer_start(
$event,
(int) ($interval * 1000) + 1,
$this->convertFloatSecondsToMilliseconds($interval),
0,
$callback
);
Expand All @@ -146,12 +146,13 @@ public function addPeriodicTimer($interval, $callback)
\call_user_func($timer->getCallback(), $timer);
};

$interval = $this->convertFloatSecondsToMilliseconds($interval);
$event = \uv_timer_init($this->uv);
$this->timers->attach($timer, $event);
\uv_timer_start(
$event,
(int) ($interval * 1000) + 1,
(int) ($interval * 1000) + 1,
$interval,
(int) $interval === 0 ? 1 : $interval,
$callback
);

Expand Down Expand Up @@ -313,4 +314,26 @@ private function createStreamListener()

return $callback;
}

/**
* @param float $interval
* @return int
*/
private function convertFloatSecondsToMilliseconds($interval)
{
if ($interval < 0) {
return 0;
}

$maxValue = (int) (\PHP_INT_MAX / 1000);
$intInterval = (int) $interval;

if (($intInterval <= 0 && $interval > 1) || $intInterval >= $maxValue) {
throw new \InvalidArgumentException(
"Interval overflow, value must be lower than '{$maxValue}', but '{$interval}' passed."
);
}

return (int) \floor($interval * 1000);
}
}
4 changes: 3 additions & 1 deletion tests/AbstractLoopTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -591,9 +591,11 @@ public function testSignalsKeepTheLoopRunningAndRemovingItStopsTheLoop()

public function testTimerIntervalCanBeFarInFuture()
{
// Maximum interval for ExtUvLoop implementation
$interval = ((int) (PHP_INT_MAX / 1000)) - 1;
$loop = $this->loop;
// start a timer very far in the future
$timer = $this->loop->addTimer(PHP_INT_MAX, function () { });
$timer = $this->loop->addTimer($interval, function () { });

$this->loop->futureTick(function () use ($timer, $loop) {
$loop->cancelTimer($timer);
Expand Down
78 changes: 78 additions & 0 deletions tests/ExtUvLoopTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,82 @@ public function createLoop()

return new ExtUvLoop();
}

/** @dataProvider intervalProvider */
public function testTimerInterval($interval, $expectedExceptionMessage)
{
$this->expectException('InvalidArgumentException');
$this->expectExceptionMessage($expectedExceptionMessage);

$this->loop
->addTimer(
$interval,
function () {
return 0;
}
);
}

public function intervalProvider()
{
$oversizeInterval = PHP_INT_MAX / 1000;
$maxValue = (int) (PHP_INT_MAX / 1000);
$oneMaxValue = $maxValue + 1;
$tenMaxValue = $maxValue + 10;
$tenMillionsMaxValue = $maxValue + 10000000;
$intMax = PHP_INT_MAX;
$oneIntMax = PHP_INT_MAX + 1;
$tenIntMax = PHP_INT_MAX + 10;
$oneHundredIntMax = PHP_INT_MAX + 100;
$oneThousandIntMax = PHP_INT_MAX + 1000;
$tenMillionsIntMax = PHP_INT_MAX + 10000000;
$tenThousandsTimesIntMax = PHP_INT_MAX * 1000;

return array(
array(
$oversizeInterval,
"Interval overflow, value must be lower than '{$maxValue}', but '{$oversizeInterval}' passed."
),
array(
$oneMaxValue,
"Interval overflow, value must be lower than '{$maxValue}', but '{$oneMaxValue}' passed.",
),
array(
$tenMaxValue,
"Interval overflow, value must be lower than '{$maxValue}', but '{$tenMaxValue}' passed.",
),
array(
$tenMillionsMaxValue,
"Interval overflow, value must be lower than '{$maxValue}', but '{$tenMillionsMaxValue}' passed.",
),
array(
$intMax,
"Interval overflow, value must be lower than '{$maxValue}', but '{$intMax}' passed.",
),
array(
$oneIntMax,
"Interval overflow, value must be lower than '{$maxValue}', but '{$oneIntMax}' passed.",
),
array(
$tenIntMax,
"Interval overflow, value must be lower than '{$maxValue}', but '{$tenIntMax}' passed.",
),
array(
$oneHundredIntMax,
"Interval overflow, value must be lower than '{$maxValue}', but '{$oneHundredIntMax}' passed.",
),
array(
$oneThousandIntMax,
"Interval overflow, value must be lower than '{$maxValue}', but '{$oneThousandIntMax}' passed.",
),
array(
$tenMillionsIntMax,
"Interval overflow, value must be lower than '{$maxValue}', but '{$tenMillionsIntMax}' passed.",
),
array(
$tenThousandsTimesIntMax,
"Interval overflow, value must be lower than '{$maxValue}', but '{$tenThousandsTimesIntMax}' passed.",
),
);
}
}
18 changes: 18 additions & 0 deletions tests/Timer/AbstractTimerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -137,4 +137,22 @@ public function testMinimumIntervalOneMicrosecond()

$this->assertEquals(0.000001, $timer->getInterval());
}

public function testTimerIntervalBelowZeroRunsImmediately()
{
$loop = $this->createLoop();
$start = 0;
$loop->addTimer(
-1,
function () use (&$start) {
$start = \microtime(true);
}
);

$loop->run();
$end = \microtime(true);

// 1ms should be enough even on slow machines
$this->assertLessThan(0.001, $end - $start);
}
}
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy