Skip to content

Commit 48203c4

Browse files
committed
Actually make declared properties readonly
1 parent d41a03b commit 48203c4

File tree

4 files changed

+105
-13
lines changed

4 files changed

+105
-13
lines changed

ext/zip/php_zip.c

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -890,6 +890,26 @@ static zval *php_zip_get_property_ptr_ptr(zend_object *object, zend_string *name
890890
}
891891
/* }}} */
892892

893+
894+
static zval *php_zip_write_property(zend_object *object, zend_string *name, zval *value, void **cache_slot)
895+
{
896+
ze_zip_object *obj;
897+
zip_prop_handler *hnd = NULL;
898+
899+
obj = php_zip_fetch_object(object);
900+
901+
if (obj->prop_handler != NULL) {
902+
hnd = zend_hash_find_ptr(obj->prop_handler, name);
903+
}
904+
905+
if (hnd != NULL) {
906+
zend_throw_error(NULL, "Cannot write read-only property %s::$%s", ZSTR_VAL(object->ce->name), ZSTR_VAL(name));
907+
return &EG(error_zval);
908+
}
909+
910+
return zend_std_write_property(object, name, value, cache_slot);
911+
}
912+
893913
static zval *php_zip_read_property(zend_object *object, zend_string *name, int type, void **cache_slot, zval *rv) /* {{{ */
894914
{
895915
ze_zip_object *obj;
@@ -3025,6 +3045,7 @@ static PHP_MINIT_FUNCTION(zip)
30253045
zip_object_handlers.get_properties = php_zip_get_properties;
30263046
zip_object_handlers.read_property = php_zip_read_property;
30273047
zip_object_handlers.has_property = php_zip_has_property;
3048+
zip_object_handlers.write_property = php_zip_write_property;
30283049

30293050
zip_class_entry = register_class_ZipArchive(zend_ce_countable);
30303051
zip_class_entry->create_object = php_zip_object_new;

ext/zip/php_zip.stub.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -67,17 +67,17 @@ function zip_entry_compressionmethod($zip_entry): string|false {}
6767
class ZipArchive implements Countable
6868
{
6969
/** @readonly */
70-
public int $lastId;
70+
public int $lastId = -1;
7171
/** @readonly */
72-
public int $status;
72+
public int $status = 0;
7373
/** @readonly */
74-
public int $statusSys;
74+
public int $statusSys = 0;
7575
/** @readonly */
76-
public int $numFiles;
76+
public int $numFiles = 0;
7777
/** @readonly */
78-
public string $filename;
78+
public string $filename = "";
7979
/** @readonly */
80-
public string $comment;
80+
public string $comment = "";
8181

8282
/** @return bool|int */
8383
public function open(string $filename, int $flags = 0) {}

ext/zip/php_zip_arginfo.h

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/* This is a generated file, edit the .stub.php file instead.
2-
* Stub hash: d0f7d294c92a3d056e38abc92162c19e10329310 */
2+
* Stub hash: 9dafeea8ff97034912f8c8e65fa19554d8bdbb2c */
33

44
ZEND_BEGIN_ARG_INFO_EX(arginfo_zip_open, 0, 0, 1)
55
ZEND_ARG_TYPE_INFO(0, filename, IS_STRING, 0)
@@ -463,37 +463,37 @@ static zend_class_entry *register_class_ZipArchive(zend_class_entry *class_entry
463463
zend_class_implements(class_entry, 1, class_entry_Countable);
464464

465465
zval property_lastId_default_value;
466-
ZVAL_UNDEF(&property_lastId_default_value);
466+
ZVAL_LONG(&property_lastId_default_value, -1);
467467
zend_string *property_lastId_name = zend_string_init("lastId", sizeof("lastId") - 1, 1);
468468
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));
469469
zend_string_release(property_lastId_name);
470470

471471
zval property_status_default_value;
472-
ZVAL_UNDEF(&property_status_default_value);
472+
ZVAL_LONG(&property_status_default_value, 0);
473473
zend_string *property_status_name = zend_string_init("status", sizeof("status") - 1, 1);
474474
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));
475475
zend_string_release(property_status_name);
476476

477477
zval property_statusSys_default_value;
478-
ZVAL_UNDEF(&property_statusSys_default_value);
478+
ZVAL_LONG(&property_statusSys_default_value, 0);
479479
zend_string *property_statusSys_name = zend_string_init("statusSys", sizeof("statusSys") - 1, 1);
480480
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));
481481
zend_string_release(property_statusSys_name);
482482

483483
zval property_numFiles_default_value;
484-
ZVAL_UNDEF(&property_numFiles_default_value);
484+
ZVAL_LONG(&property_numFiles_default_value, 0);
485485
zend_string *property_numFiles_name = zend_string_init("numFiles", sizeof("numFiles") - 1, 1);
486486
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));
487487
zend_string_release(property_numFiles_name);
488488

489489
zval property_filename_default_value;
490-
ZVAL_UNDEF(&property_filename_default_value);
490+
ZVAL_EMPTY_STRING(&property_filename_default_value);
491491
zend_string *property_filename_name = zend_string_init("filename", sizeof("filename") - 1, 1);
492492
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));
493493
zend_string_release(property_filename_name);
494494

495495
zval property_comment_default_value;
496-
ZVAL_UNDEF(&property_comment_default_value);
496+
ZVAL_EMPTY_STRING(&property_comment_default_value);
497497
zend_string *property_comment_name = zend_string_init("comment", sizeof("comment") - 1, 1);
498498
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));
499499
zend_string_release(property_comment_name);
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
--TEST--
2+
Test that ZipArchive properties are read-only
3+
--EXTENSIONS--
4+
zip
5+
--FILE--
6+
<?php
7+
8+
$zip = new ZipArchive();
9+
10+
try {
11+
$zip->lastId = 1;
12+
} catch (Error $exception) {
13+
echo $exception->getMessage() . "\n";
14+
}
15+
16+
var_dump($zip->lastId);
17+
18+
try {
19+
$zip->status = 1;
20+
} catch (Error $exception) {
21+
echo $exception->getMessage() . "\n";
22+
}
23+
24+
var_dump($zip->status);
25+
26+
try {
27+
$zip->statusSys = 1;
28+
} catch (Error $exception) {
29+
echo $exception->getMessage() . "\n";
30+
}
31+
32+
var_dump($zip->statusSys);
33+
34+
try {
35+
$zip->numFiles = 1;
36+
} catch (Error $exception) {
37+
echo $exception->getMessage() . "\n";
38+
}
39+
40+
var_dump($zip->numFiles);
41+
42+
try {
43+
$zip->filename = "a";
44+
} catch (Error $exception) {
45+
echo $exception->getMessage() . "\n";
46+
}
47+
48+
var_dump($zip->filename);
49+
50+
try {
51+
$zip->comment = "a";
52+
} catch (Error $exception) {
53+
echo $exception->getMessage() . "\n";
54+
}
55+
56+
var_dump($zip->comment);
57+
58+
?>
59+
--EXPECT--
60+
Cannot write read-only property ZipArchive::$lastId
61+
int(-1)
62+
Cannot write read-only property ZipArchive::$status
63+
int(0)
64+
Cannot write read-only property ZipArchive::$statusSys
65+
int(0)
66+
Cannot write read-only property ZipArchive::$numFiles
67+
int(0)
68+
Cannot write read-only property ZipArchive::$filename
69+
string(0) ""
70+
Cannot write read-only property ZipArchive::$comment
71+
string(0) ""

0 commit comments

Comments
 (0)