1
0
mirror of https://github.com/danog/parallel.git synced 2025-01-22 14:01:14 +01:00

Improve error message for invalid channel data

This commit is contained in:
Aaron Piotrowski 2018-01-22 16:12:55 -06:00
parent d16da4611e
commit abaa1609fd
No known key found for this signature in database
GPG Key ID: ADD1EF783EDE9EEB
2 changed files with 31 additions and 4 deletions

View File

@ -38,7 +38,7 @@ class ChannelParser extends Parser {
}
/**
* @param \SplQueue $queue
* @param callable $push
* @param callable $errorHandler
*
* @return \Generator
@ -46,13 +46,19 @@ class ChannelParser extends Parser {
* @throws \Amp\Parallel\Sync\ChannelException
* @throws \Amp\Parallel\Sync\SerializationException
*/
private static function parser(callable $callback, callable $errorHandler): \Generator {
private static function parser(callable $push, callable $errorHandler): \Generator {
while (true) {
$header = yield self::HEADER_LENGTH;
$data = \unpack("Cprefix/Llength", $header);
if ($data["prefix"] !== 0) {
throw new ChannelException("Invalid header received: " . \bin2hex($header . yield));
$data = $header . yield;
$data = \preg_replace_callback("/[^\x20-\x7e]/", function (array $matches) {
return "\\x" . \dechex(\ord($matches[0]));
}, $data);
throw new ChannelException("Invalid packet received: " . $data);
}
$data = yield $data["length"];
@ -68,7 +74,7 @@ class ChannelParser extends Parser {
\restore_error_handler();
}
$callback($data);
$push($data);
}
}

View File

@ -16,4 +16,25 @@ class ChannelParserTest extends TestCase {
$parser = new ChannelParser($this->createCallback(0));
$parser->push($data);
}
/**
* @expectedException \Amp\Parallel\Sync\ChannelException
* @expectedExceptionMessage Invalid packet received: Invalid packet
*/
public function testInvalidHeaderData() {
$data = "Invalid packet";
$parser = new ChannelParser($this->createCallback(0));
$parser->push($data);
}
/**
* @expectedException \Amp\Parallel\Sync\ChannelException
* @expectedExceptionMessage Invalid packet received: B \xf3\xf2\x0\x1
*/
public function testInvalidHeaderBinaryData() {
$data = "\x42\x20\xf3\xf2\x00\x01";
$parser = new ChannelParser($this->createCallback(0));
$parser->push($data);
}
}