[php-src] master: Use typed properties in ext/zip

From: Date: Mon, 26 Apr 2021 11:22:18 +0000
Subject: [php-src] master: Use typed properties in ext/zip
Groups: php.cvs 
Request: Send a blank email to php-cvs+get-115819@lists.php.net to get a copy of this message
Author: Máté Kocsis (kocsismate) Date: 2021-04-26T13:21:17+02:00 Commit: https://github.com/php/php-src/commit/b567a9d173514f9d18253817148a57ee72234106 Raw diff: https://github.com/php/php-src/commit/b567a9d173514f9d18253817148a57ee72234106.diff Use typed properties in ext/zip Closes GH-6881 Changed paths: A ext/zip/tests/oo_readonly_properties.phpt M ext/zip/php_zip.c M ext/zip/php_zip.stub.php M ext/zip/php_zip_arginfo.h Diff: diff --git a/ext/zip/php_zip.c b/ext/zip/php_zip.c index e62f9fc8f9f4..ddbb7312b584 100644 --- a/ext/zip/php_zip.c +++ b/ext/zip/php_zip.c @@ -859,10 +859,6 @@ static zval *php_zip_property_reader(ze_zip_object *obj, zip_prop_handler *hnd, ZVAL_EMPTY_STRING(rv); } break; - /* case IS_TRUE */ - case IS_FALSE: - ZVAL_BOOL(rv, retint); - break; case IS_LONG: ZVAL_LONG(rv, retint); break; @@ -894,6 +890,26 @@ static zval *php_zip_get_property_ptr_ptr(zend_object *object, zend_string *name } /* }}} */ + +static zval *php_zip_write_property(zend_object *object, zend_string *name, zval *value, void **cache_slot) +{ + ze_zip_object *obj; + zip_prop_handler *hnd = NULL; + + obj = php_zip_fetch_object(object); + + if (obj->prop_handler != NULL) { + hnd = zend_hash_find_ptr(obj->prop_handler, name); + } + + if (hnd != NULL) { + zend_throw_error(NULL, "Cannot write read-only property %s::$%s", ZSTR_VAL(object->ce->name), ZSTR_VAL(name)); + return &EG(error_zval); + } + + return zend_std_write_property(object, name, value, cache_slot); +} + static zval *php_zip_read_property(zend_object *object, zend_string *name, int type, void **cache_slot, zval *rv) /* {{{ */ { ze_zip_object *obj; @@ -3029,6 +3045,7 @@ static PHP_MINIT_FUNCTION(zip) zip_object_handlers.get_properties = php_zip_get_properties; zip_object_handlers.read_property = php_zip_read_property; zip_object_handlers.has_property = php_zip_has_property; + zip_object_handlers.write_property = php_zip_write_property; zip_class_entry = register_class_ZipArchive(zend_ce_countable); zip_class_entry->create_object = php_zip_object_new; diff --git a/ext/zip/php_zip.stub.php b/ext/zip/php_zip.stub.php index 2c23b0385119..e2d473cb239d 100644 --- a/ext/zip/php_zip.stub.php +++ b/ext/zip/php_zip.stub.php @@ -66,23 +66,18 @@ function zip_entry_compressionmethod($zip_entry): string|false {} class ZipArchive implements Countable { - /** @var int|null */ - public $lastId; - - /** @var int|null */ - public $status; - - /** @var int|null */ - public $statusSys; - - /** @var int|null */ - public $numFiles; - - /** @var string|null */ - public $filename; - - /** @var string|null */ - public $comment; + /** @readonly */ + public int $lastId; + /** @readonly */ + public int $status; + /** @readonly */ + public int $statusSys; + /** @readonly */ + public int $numFiles; + /** @readonly */ + public string $filename; + /** @readonly */ + public string $comment; /** @return bool|int */ public function open(string $filename, int $flags = 0) {} diff --git a/ext/zip/php_zip_arginfo.h b/ext/zip/php_zip_arginfo.h index 900c334bf0d2..675e8d064afa 100644 --- a/ext/zip/php_zip_arginfo.h +++ b/ext/zip/php_zip_arginfo.h @@ -1,5 +1,5 @@ /* This is a generated file, edit the .stub.php file instead. - * Stub hash: a50da348df01027594efd7c8ab7427c05ffea39e */ + * Stub hash: d0f7d294c92a3d056e38abc92162c19e10329310 */ ZEND_BEGIN_ARG_INFO_EX(arginfo_zip_open, 0, 0, 1) ZEND_ARG_TYPE_INFO(0, filename, IS_STRING, 0) @@ -463,39 +463,39 @@ static zend_class_entry *register_class_ZipArchive(zend_class_entry *class_entry zend_class_implements(class_entry, 1, class_entry_Countable); zval property_lastId_default_value; - ZVAL_NULL(&property_lastId_default_value); + ZVAL_UNDEF(&property_lastId_default_value); zend_string *property_lastId_name = zend_string_init("lastId", sizeof("lastId") - 1, 1); - zend_declare_property_ex(class_entry, property_lastId_name, &property_lastId_default_value, ZEND_ACC_PUBLIC, NULL); + zend_declare_typed_property(class_entry, property_lastId_name, &property_lastId_default_value, ZEND_ACC_PUBLIC, NULL, (zend_type) ZEND_TYPE_INIT_MASK(MAY_BE_LONG)); zend_string_release(property_lastId_name); zval property_status_default_value; - ZVAL_NULL(&property_status_default_value); + ZVAL_UNDEF(&property_status_default_value); zend_string *property_status_name = zend_string_init("status", sizeof("status") - 1, 1); - zend_declare_property_ex(class_entry, property_status_name, &property_status_default_value, ZEND_ACC_PUBLIC, NULL); + zend_declare_typed_property(class_entry, property_status_name, &property_status_default_value, ZEND_ACC_PUBLIC, NULL, (zend_type) ZEND_TYPE_INIT_MASK(MAY_BE_LONG)); zend_string_release(property_status_name); zval property_statusSys_default_value; - ZVAL_NULL(&property_statusSys_default_value); + ZVAL_UNDEF(&property_statusSys_default_value); zend_string *property_statusSys_name = zend_string_init("statusSys", sizeof("statusSys") - 1, 1); - zend_declare_property_ex(class_entry, property_statusSys_name, &property_statusSys_default_value, ZEND_ACC_PUBLIC, NULL); + zend_declare_typed_property(class_entry, property_statusSys_name, &property_statusSys_default_value, ZEND_ACC_PUBLIC, NULL, (zend_type) ZEND_TYPE_INIT_MASK(MAY_BE_LONG)); zend_string_release(property_statusSys_name); zval property_numFiles_default_value; - ZVAL_NULL(&property_numFiles_default_value); + ZVAL_UNDEF(&property_numFiles_default_value); zend_string *property_numFiles_name = zend_string_init("numFiles", sizeof("numFiles") - 1, 1); - zend_declare_property_ex(class_entry, property_numFiles_name, &property_numFiles_default_value, ZEND_ACC_PUBLIC, NULL); + zend_declare_typed_property(class_entry, property_numFiles_name, &property_numFiles_default_value, ZEND_ACC_PUBLIC, NULL, (zend_type) ZEND_TYPE_INIT_MASK(MAY_BE_LONG)); zend_string_release(property_numFiles_name); zval property_filename_default_value; - ZVAL_NULL(&property_filename_default_value); + ZVAL_UNDEF(&property_filename_default_value); zend_string *property_filename_name = zend_string_init("filename", sizeof("filename") - 1, 1); - zend_declare_property_ex(class_entry, property_filename_name, &property_filename_default_value, ZEND_ACC_PUBLIC, NULL); + zend_declare_typed_property(class_entry, property_filename_name, &property_filename_default_value, ZEND_ACC_PUBLIC, NULL, (zend_type) ZEND_TYPE_INIT_MASK(MAY_BE_STRING)); zend_string_release(property_filename_name); zval property_comment_default_value; - ZVAL_NULL(&property_comment_default_value); + ZVAL_UNDEF(&property_comment_default_value); zend_string *property_comment_name = zend_string_init("comment", sizeof("comment") - 1, 1); - zend_declare_property_ex(class_entry, property_comment_name, &property_comment_default_value, ZEND_ACC_PUBLIC, NULL); + zend_declare_typed_property(class_entry, property_comment_name, &property_comment_default_value, ZEND_ACC_PUBLIC, NULL, (zend_type) ZEND_TYPE_INIT_MASK(MAY_BE_STRING)); zend_string_release(property_comment_name); return class_entry; diff --git a/ext/zip/tests/oo_readonly_properties.phpt b/ext/zip/tests/oo_readonly_properties.phpt new file mode 100644 index 000000000000..40eb29f68a7c --- /dev/null +++ b/ext/zip/tests/oo_readonly_properties.phpt @@ -0,0 +1,78 @@ +--TEST-- +Test that ZipArchive properties are read-only +--EXTENSIONS-- +zip +--FILE-- +<?php + +$zip = new ZipArchive(); + +try { + $zip->lastId = 1; +} catch (Error $exception) { + echo $exception->getMessage() . "\n"; +} + +try { + $zip->lastId += 1; +} catch (Error $exception) { + echo $exception->getMessage() . "\n"; +} + +var_dump($zip->lastId); + +try { + $zip->status = 1; +} catch (Error $exception) { + echo $exception->getMessage() . "\n"; +} + +var_dump($zip->status); + +try { + $zip->statusSys = 1; +} catch (Error $exception) { + echo $exception->getMessage() . "\n"; +} + +var_dump($zip->statusSys); + +try { + $zip->numFiles = 1; +} catch (Error $exception) { + echo $exception->getMessage() . "\n"; +} + +var_dump($zip->numFiles); + +try { + $zip->filename = "a"; +} catch (Error $exception) { + echo $exception->getMessage() . "\n"; +} + +var_dump($zip->filename); + +try { + $zip->comment = "a"; +} catch (Error $exception) { + echo $exception->getMessage() . "\n"; +} + +var_dump($zip->comment); + +?> +--EXPECT-- +Cannot write read-only property ZipArchive::$lastId +Cannot write read-only property ZipArchive::$lastId +int(-1) +Cannot write read-only property ZipArchive::$status +int(0) +Cannot write read-only property ZipArchive::$statusSys +int(0) +Cannot write read-only property ZipArchive::$numFiles +int(0) +Cannot write read-only property ZipArchive::$filename +string(0) "" +Cannot write read-only property ZipArchive::$comment +string(0) ""

« previous php.cvs (#115819) next »