Skip to content

Commit 6846ade

Browse files
committed
Binary: account for potential empty string in read(Signed)Byte
1 parent 64430cc commit 6846ade

File tree

5 files changed

+60
-1
lines changed

5 files changed

+60
-1
lines changed

.github/workflows/ci.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,5 @@ jobs:
2727
run: composer install --prefer-dist --no-interaction
2828
- name: Run PHPStan
2929
run: ./vendor/bin/phpstan analyze --no-progress
30+
- name: Run PHPUnit
31+
run: ./vendor/bin/phpunit tests/phpunit --fail-on-warning

composer.json

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,18 @@
1010
"require-dev": {
1111
"phpstan/phpstan": "1.2.0",
1212
"phpstan/extension-installer": "^1.0",
13-
"phpstan/phpstan-strict-rules": "^1.0.0"
13+
"phpstan/phpstan-strict-rules": "^1.0.0",
14+
"phpunit/phpunit": "^9.5",
15+
"phpstan/phpstan-phpunit": "^1.0"
1416
},
1517
"autoload": {
1618
"psr-4": {
1719
"pocketmine\\utils\\": "src/"
1820
}
21+
},
22+
"autoload-dev": {
23+
"psr-4": {
24+
"pocketmine\\utils\\": "tests/phpunit/"
25+
}
1926
}
2027
}

phpstan.neon.dist

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ parameters:
22
level: 8
33
paths:
44
- src
5+
- tests/phpunit
56
ignoreErrors:
67
-
78
#this would only happen if the regex was broken

src/Binary.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,15 +103,25 @@ public static function writeBool(bool $b) : string{
103103

104104
/**
105105
* Reads an unsigned byte (0 - 255)
106+
*
107+
* @throws BinaryDataException
106108
*/
107109
public static function readByte(string $c) : int{
110+
if($c === ""){
111+
throw new BinaryDataException("Expected a string of length 1");
112+
}
108113
return ord($c[0]);
109114
}
110115

111116
/**
112117
* Reads a signed byte (-128 - 127)
118+
*
119+
* @throws BinaryDataException
113120
*/
114121
public static function readSignedByte(string $c) : int{
122+
if($c === ""){
123+
throw new BinaryDataException("Expected a string of length 1");
124+
}
115125
return self::signByte(ord($c[0]));
116126
}
117127

tests/phpunit/BinaryTest.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
/*
4+
*
5+
* ____ _ _ __ __ _ __ __ ____
6+
* | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
7+
* | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
8+
* | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
9+
* |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
10+
*
11+
* This program is free software: you can redistribute it and/or modify
12+
* it under the terms of the GNU Lesser General Public License as published by
13+
* the Free Software Foundation, either version 3 of the License, or
14+
* (at your option) any later version.
15+
*
16+
* @author PocketMine Team
17+
* @link http://www.pocketmine.net/
18+
*
19+
*
20+
*/
21+
22+
declare(strict_types=1);
23+
24+
namespace pocketmine\utils;
25+
26+
use PHPUnit\Framework\TestCase;
27+
28+
final class BinaryTest extends TestCase{
29+
30+
public function testReadByteWithEmptyString() : void{
31+
$this->expectException(BinaryDataException::class);
32+
Binary::readByte("");
33+
}
34+
35+
public function testReadSignedByteWithEmptyString() : void{
36+
$this->expectException(BinaryDataException::class);
37+
Binary::readSignedByte("");
38+
}
39+
}

0 commit comments

Comments
 (0)