66
77#include " PhpPalettedBlockArrayObj.h"
88#include " stubs/pocketmine/world/format/PalettedBlockArray_arginfo.h"
9+ #include " stubs/pocketmine/world/format/PalettedBlockArrayLoadException_arginfo.h"
910
1011extern " C" {
1112#include " php.h"
@@ -16,21 +17,23 @@ extern "C" {
1617}
1718
1819zend_class_entry *paletted_block_array_entry;
20+ zend_class_entry* paletted_block_array_load_exception_entry;
21+
1922static zend_object_handlers paletted_block_array_handlers;
2023
2124/* internal object methods */
2225
23- static inline bool checkPaletteEntrySize (zend_long v) {
26+ static inline bool checkPaletteEntrySize (zend_long v, zend_class_entry* exception_ce ) {
2427 Block casted = (Block)v;
2528 zend_long castedBack = (zend_long)casted;
2629 if (castedBack != v) {
27- zend_throw_exception_ex (spl_ce_InvalidArgumentException , 0 , " value %zd is too large to be used as a palette entry" , v);
30+ zend_throw_exception_ex (exception_ce , 0 , " value %zd is too large to be used as a palette entry" , v);
2831 return false ;
2932 }
3033 return true ;
3134}
3235
33- static bool palette_data_from_array (HashTable* paletteHt, std::vector<Block>& palette) {
36+ static bool palette_data_from_array (HashTable* paletteHt, std::vector<Block>& palette, zend_class_entry* exception_ce ) {
3437 HashPosition pos;
3538 zval *current;
3639 Block b;
@@ -42,7 +45,7 @@ static bool palette_data_from_array(HashTable* paletteHt, std::vector<Block>& pa
4245 zend_hash_move_forward_ex (paletteHt, &pos)
4346 ) {
4447
45- if (!checkPaletteEntrySize (Z_LVAL_P (current))) {
48+ if (!checkPaletteEntrySize (Z_LVAL_P (current), exception_ce )) {
4649 return false ;
4750 }
4851 b = (Block)Z_LVAL_P (current);
@@ -52,9 +55,9 @@ static bool palette_data_from_array(HashTable* paletteHt, std::vector<Block>& pa
5255 return true ;
5356}
5457
55- static bool palette_data_from_string (zend_string* paletteZstr, std::vector<Block>& palette) {
58+ static bool palette_data_from_string (zend_string* paletteZstr, std::vector<Block>& palette, zend_class_entry* exception_ce ) {
5659 if ((ZSTR_LEN (paletteZstr) % sizeof (Block)) != 0 ) {
57- zend_throw_exception_ex (spl_ce_InvalidArgumentException , 0 , " palette length in bytes must be a multiple of %zu, but have %zu bytes" , sizeof (Block), ZSTR_LEN (paletteZstr));
60+ zend_throw_exception_ex (exception_ce , 0 , " palette length in bytes must be a multiple of %zu, but have %zu bytes" , sizeof (Block), ZSTR_LEN (paletteZstr));
5861 return false ;
5962 }
6063
@@ -74,9 +77,9 @@ static bool paletted_block_array_from_data(zval *return_value, zend_long bitsPer
7477
7578 assert (paletteHt != nullptr || paletteZstr != nullptr );
7679
77- if (paletteHt != nullptr && !palette_data_from_array (paletteHt, palette)) {
80+ if (paletteHt != nullptr && !palette_data_from_array (paletteHt, palette, paletted_block_array_load_exception_entry )) {
7881 return false ;
79- } else if (paletteZstr != nullptr && !palette_data_from_string (paletteZstr, palette)) {
82+ } else if (paletteZstr != nullptr && !palette_data_from_string (paletteZstr, palette, paletted_block_array_load_exception_entry )) {
8083 return false ;
8184 }
8285
@@ -85,7 +88,7 @@ static bool paletted_block_array_from_data(zval *return_value, zend_long bitsPer
8588 return true ;
8689 }
8790 catch (std::exception& e) {
88- zend_throw_exception_ex (spl_ce_RuntimeException , 0 , " %s" , e.what ());
91+ zend_throw_exception_ex (paletted_block_array_load_exception_entry , 0 , " %s" , e.what ());
8992 return false ;
9093 }
9194}
@@ -123,11 +126,12 @@ static void paletted_block_array_get_palette_bytes(zval* object, zval* return_va
123126 ZVAL_STRINGL (return_value, reinterpret_cast <const char *>(paletteValues), palette.size_bytes ());
124127}
125128
129+
126130static bool paletted_block_array_set_palette (zval* object, HashTable* paletteHt) {
127131 paletted_block_array_obj* intern = fetch_from_zend_object<paletted_block_array_obj>(Z_OBJ_P (object));
128132
129133 std::vector<Block> palette;
130- if (!palette_data_from_array (paletteHt, palette)) {
134+ if (!palette_data_from_array (paletteHt, palette, spl_ce_InvalidArgumentException )) {
131135 return false ;
132136 }
133137
@@ -262,7 +266,7 @@ PALETTED_BLOCK_ARRAY_METHOD(__construct) {
262266 Z_PARAM_LONG (fillEntry)
263267 ZEND_PARSE_PARAMETERS_END ();
264268
265- if (!checkPaletteEntrySize (fillEntry)) {
269+ if (!checkPaletteEntrySize (fillEntry, spl_ce_InvalidArgumentException )) {
266270 return ;
267271 }
268272
@@ -271,7 +275,7 @@ PALETTED_BLOCK_ARRAY_METHOD(__construct) {
271275 new (&intern->container ) NormalBlockArrayContainer ((Block)fillEntry, 0 );
272276 }
273277 catch (std::exception& e) {
274- zend_throw_exception_ex (spl_ce_RuntimeException , 0 , " %s" , e.what ());
278+ zend_throw_exception_ex (spl_ce_InvalidArgumentException , 0 , " %s" , e.what ());
275279 }
276280}
277281
@@ -389,6 +393,11 @@ PALETTED_BLOCK_ARRAY_METHOD(getExpectedWordArraySize) {
389393 Z_PARAM_LONG (bitsPerBlock)
390394 ZEND_PARSE_PARAMETERS_END ();
391395
396+ // TODO: this probably shouldn't be throwing InvalidArgumentException
397+ // the parameters given to this function will typically be coming from serialized
398+ // data, so it doesn't really make sense to declare them logically invalid
399+ // however using a different exception type will break BC
400+
392401 uint8_t casted = (uint8_t )bitsPerBlock;
393402 zend_long castedBack = (zend_long)casted;
394403 if (bitsPerBlock != castedBack) {
@@ -405,6 +414,8 @@ PALETTED_BLOCK_ARRAY_METHOD(getExpectedWordArraySize) {
405414}
406415
407416void register_paletted_block_array_class () {
417+ paletted_block_array_load_exception_entry = register_class_pocketmine_world_format_PalettedBlockArrayLoadException (spl_ce_RuntimeException);
418+
408419 memcpy (&paletted_block_array_handlers, zend_get_std_object_handlers (), sizeof (zend_object_handlers));
409420 paletted_block_array_handlers.offset = XtOffsetOf (paletted_block_array_obj, std);
410421 paletted_block_array_handlers.free_obj = paletted_block_array_free;
0 commit comments