From beb5d72299b93088f62a36e9d9cc01fb48465e14 Mon Sep 17 00:00:00 2001 From: "Dylan K. Taylor" Date: Sat, 29 Dec 2018 00:02:54 +0000 Subject: [PATCH] RegionLoader: fix off-by-one bug with large chunks, closes #2615 --- .../level/format/io/region/RegionLoader.php | 2 +- .../format/io/region/RegionLoaderTest.php | 52 +++++++++++++++++++ 2 files changed, 53 insertions(+), 1 deletion(-) create mode 100644 tests/phpunit/level/format/io/region/RegionLoaderTest.php diff --git a/src/pocketmine/level/format/io/region/RegionLoader.php b/src/pocketmine/level/format/io/region/RegionLoader.php index a8400f527..15b868bf1 100644 --- a/src/pocketmine/level/format/io/region/RegionLoader.php +++ b/src/pocketmine/level/format/io/region/RegionLoader.php @@ -33,7 +33,7 @@ class RegionLoader{ public const COMPRESSION_GZIP = 1; public const COMPRESSION_ZLIB = 2; - public const MAX_SECTOR_LENGTH = 256 << 12; //256 sectors, (1 MiB) + public const MAX_SECTOR_LENGTH = 255 << 12; //255 sectors (~0.996 MiB) public const REGION_HEADER_LENGTH = 8192; //4096 location table + 4096 timestamps public static $COMPRESSION_LEVEL = 7; diff --git a/tests/phpunit/level/format/io/region/RegionLoaderTest.php b/tests/phpunit/level/format/io/region/RegionLoaderTest.php new file mode 100644 index 000000000..279439c0b --- /dev/null +++ b/tests/phpunit/level/format/io/region/RegionLoaderTest.php @@ -0,0 +1,52 @@ +open(); + + $this->expectException(ChunkException::class); + $r->writeChunk(0, 0, str_repeat("a", 1044476)); + } + + public function testChunkMaxSize() : void{ + $data = str_repeat("a", 1044475); + $path = sys_get_temp_dir() . '/chunk_just_fits.testregion_' . bin2hex(random_bytes(4)); + $r = new RegionLoader($path, 0, 0); + $r->open(); + + $r->writeChunk(0, 0, $data); + $r->close(); + + $r = new RegionLoader($path, 0, 0); + $r->open(); + self::assertSame($data, $r->readChunk(0, 0)); + } +}