Skip to content

Commit b32c375

Browse files
committed
better error handling for invalid NBT data, added ext-json to composer.json
1 parent dd2e256 commit b32c375

6 files changed

+41
-23
lines changed

composer.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
"require": {
1818
"pocketmine/binaryutils": "^0.2.1",
1919
"php-64bit": "*",
20-
"ext-zlib": "*"
20+
"ext-zlib": "*",
21+
"ext-json": "*"
2122
}
2223
}

src/IO/Reader/GZipCompressedStringReader.php

+9-1
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,18 @@
22

33
namespace Aternos\Nbt\IO\Reader;
44

5+
use Exception;
6+
57
class GZipCompressedStringReader extends StringReader
68
{
9+
/**
10+
* @throws Exception
11+
*/
712
public function __construct(string $data, int $format)
813
{
9-
parent::__construct(gzdecode($data), $format);
14+
if(($uncompressed = @gzdecode($data)) === false) {
15+
throw new Exception("Invalid GZip data");
16+
}
17+
parent::__construct($uncompressed, $format);
1018
}
1119
}

src/IO/Reader/ZLibCompressedStringReader.php

+9-1
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,18 @@
22

33
namespace Aternos\Nbt\IO\Reader;
44

5+
use Exception;
6+
57
class ZLibCompressedStringReader extends StringReader
68
{
9+
/**
10+
* @throws Exception
11+
*/
712
public function __construct(string $data, int $format)
813
{
9-
parent::__construct(zlib_decode($data), $format);
14+
if(($uncompressed = @zlib_decode($data)) === false) {
15+
throw new Exception("Invalid ZLib data");
16+
}
17+
parent::__construct($uncompressed, $format);
1018
}
1119
}

src/IO/Writer/GZipCompressedStringWriter.php

+9-1
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,18 @@
22

33
namespace Aternos\Nbt\IO\Writer;
44

5+
use Exception;
6+
57
class GZipCompressedStringWriter extends StringWriter
68
{
9+
/**
10+
* @throws Exception
11+
*/
712
public function getStringData(): string
813
{
9-
return gzencode(parent::getStringData());
14+
if(($compressed = @gzencode(parent::getStringData())) === false) {
15+
throw new Exception("Failed to apply GZip compression");
16+
}
17+
return $compressed;
1018
}
1119
}

src/IO/Writer/ZLibCompressedStringWriter.php

+9-1
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,18 @@
22

33
namespace Aternos\Nbt\IO\Writer;
44

5+
use Exception;
6+
57
class ZLibCompressedStringWriter extends StringWriter
68
{
9+
/**
10+
* @throws Exception
11+
*/
712
public function getStringData(): string
813
{
9-
return zlib_encode(parent::getStringData(), ZLIB_ENCODING_DEFLATE);
14+
if(($compressed = @zlib_encode(parent::getStringData(), ZLIB_ENCODING_DEFLATE)) === false) {
15+
throw new Exception("Failed to apply ZLib compression");
16+
}
17+
return $compressed;
1018
}
1119
}

src/Tag/Tag.php

+3-18
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,9 @@
77
use Aternos\Nbt\NbtFormat;
88
use Aternos\Nbt\Serializer\NbtSerializer;
99
use Exception;
10+
use JsonSerializable;
1011

11-
abstract class Tag implements \JsonSerializable
12+
abstract class Tag implements JsonSerializable
1213
{
1314
public const TYPE = -1;
1415

@@ -143,22 +144,6 @@ public function write(Writer $writer): Tag
143144
return $this;
144145
}
145146

146-
/**
147-
* @param string $type
148-
* @param string $value
149-
* @return mixed
150-
* @throws Exception
151-
* @deprecated
152-
*/
153-
protected function unpack(string $type, string $value)
154-
{
155-
$unpacked = @unpack($type, $value);
156-
if($unpacked === false) {
157-
throw new Exception("Failed to unpack " . $type . " from " . bin2hex($value));
158-
}
159-
return $unpacked[1];
160-
}
161-
162147
/**
163148
* @param string $str
164149
* @return string
@@ -195,7 +180,7 @@ abstract protected function getValueString(): string;
195180
*/
196181
public static function getTagClass(int $type): ?string
197182
{
198-
return static::TAGS[$type] ?: null;
183+
return static::TAGS[$type] ?? null;
199184
}
200185

201186
/**

0 commit comments

Comments
 (0)