Use typed properties in ext/zip

Closes GH-6881
This commit is contained in:
Máté Kocsis 2021-04-19 23:56:47 +02:00
parent 28a66b3f02
commit b567a9d173
No known key found for this signature in database
GPG Key ID: FD055E41728BF310
4 changed files with 124 additions and 34 deletions

View File

@ -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;

View File

@ -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) {}

View File

@ -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;

View File

@ -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) ""