Skip to content

Commit 71cfe45

Browse files
committed
Make properties typed
1 parent 4b83480 commit 71cfe45

File tree

4 files changed

+126
-49
lines changed

4 files changed

+126
-49
lines changed

ext/snmp/snmp.c

Lines changed: 26 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1677,19 +1677,30 @@ zval *php_snmp_write_property(zend_object *object, zend_string *name, zval *valu
16771677
obj = php_snmp_fetch_object(object);
16781678
hnd = zend_hash_find_ptr(&php_snmp_properties, name);
16791679

1680-
if (hnd && hnd->write_func) {
1681-
hnd->write_func(obj, value);
1682-
/*
1683-
if (!PZVAL_IS_REF(value) && Z_REFCOUNT_P(value) == 0) {
1684-
Z_ADDREF_P(value);
1685-
zval_ptr_dtor(&value);
1680+
if (hnd) {
1681+
if (!hnd->write_func) {
1682+
zend_throw_error(NULL, "Cannot write read-only property %s::$%s", ZSTR_VAL(object->ce->name), ZSTR_VAL(name));
1683+
return &EG(error_zval);
16861684
}
1687-
*/
1688-
} else {
1689-
value = zend_std_write_property(object, name, value, cache_slot);
1685+
1686+
zend_property_info *prop = zend_get_property_info(object->ce, name, /* silent */ true);
1687+
if (prop && ZEND_TYPE_IS_SET(prop->type)) {
1688+
zval tmp;
1689+
ZVAL_COPY(&tmp, value);
1690+
if (!zend_verify_property_type(prop, &tmp,
1691+
ZEND_CALL_USES_STRICT_TYPES(EG(current_execute_data)))) {
1692+
zval_ptr_dtor(&tmp);
1693+
return &EG(error_zval);
1694+
}
1695+
hnd->write_func(obj, &tmp);
1696+
zval_ptr_dtor(&tmp);
1697+
} else {
1698+
hnd->write_func(obj, value);
1699+
}
1700+
return value;
16901701
}
16911702

1692-
return value;
1703+
return zend_std_write_property(object, name, value, cache_slot);
16931704
}
16941705
/* }}} */
16951706

@@ -1820,14 +1831,6 @@ PHP_SNMP_LONG_PROPERTY_READER_FUNCTION(valueretrieval)
18201831
PHP_SNMP_LONG_PROPERTY_READER_FUNCTION(oid_output_format)
18211832
PHP_SNMP_LONG_PROPERTY_READER_FUNCTION(exceptions_enabled)
18221833

1823-
/* {{{ */
1824-
static int php_snmp_write_info(php_snmp_object *snmp_object, zval *newval)
1825-
{
1826-
zend_throw_error(NULL, "SNMP::$info property is read-only");
1827-
return FAILURE;
1828-
}
1829-
/* }}} */
1830-
18311834
/* {{{ */
18321835
static int php_snmp_write_max_oids(php_snmp_object *snmp_object, zval *newval)
18331836
{
@@ -1841,7 +1844,7 @@ static int php_snmp_write_max_oids(php_snmp_object *snmp_object, zval *newval)
18411844
lval = zval_get_long(newval);
18421845

18431846
if (lval <= 0) {
1844-
zend_value_error("max_oids must be greater than 0 or null");
1847+
zend_value_error("SNMP::$max_oids must be greater than 0 or null");
18451848
return FAILURE;
18461849
}
18471850
snmp_object->max_oids = lval;
@@ -1924,8 +1927,11 @@ static void free_php_snmp_properties(zval *el) /* {{{ */
19241927
#define PHP_SNMP_PROPERTY_ENTRY_RECORD(name) \
19251928
{ "" #name "", sizeof("" #name "") - 1, php_snmp_read_##name, php_snmp_write_##name }
19261929

1930+
#define PHP_SNMP_READONLY_PROPERTY_ENTRY_RECORD(name) \
1931+
{ "" #name "", sizeof("" #name "") - 1, php_snmp_read_##name, NULL }
1932+
19271933
const php_snmp_prop_handler php_snmp_property_entries[] = {
1928-
PHP_SNMP_PROPERTY_ENTRY_RECORD(info),
1934+
PHP_SNMP_READONLY_PROPERTY_ENTRY_RECORD(info),
19291935
PHP_SNMP_PROPERTY_ENTRY_RECORD(max_oids),
19301936
PHP_SNMP_PROPERTY_ENTRY_RECORD(valueretrieval),
19311937
PHP_SNMP_PROPERTY_ENTRY_RECORD(quick_print),

ext/snmp/snmp.stub.php

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -75,21 +75,15 @@ function snmp_read_mib(string $filename): bool {}
7575

7676
class SNMP
7777
{
78+
/** @readonly */
7879
public array $info;
79-
/** @var int|null */
80-
public $max_oids;
81-
/** @var int */
82-
public $valueretrieval;
83-
/** @var bool */
84-
public $quick_print;
85-
/** @var bool */
86-
public $enum_print;
87-
/** @var int */
88-
public $oid_output_format;
89-
/** @var bool */
90-
public $oid_increasing_check = true;
91-
/** @var int */
92-
public $exceptions_enabled = 0;
80+
public ?int $max_oids;
81+
public int $valueretrieval;
82+
public bool $quick_print;
83+
public bool $enum_print;
84+
public int $oid_output_format;
85+
public bool $oid_increasing_check;
86+
public int $exceptions_enabled;
9387

9488
public function __construct(int $version, string $hostname, string $community, int $timeout = -1, int $retries = -1) {}
9589

ext/snmp/snmp_arginfo.h

Lines changed: 15 additions & 15 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: e413f4e60b0f04dc3f3cfd1bb90b8f896c3fd7f0 */
2+
* Stub hash: 5258c5796aca15e369dd72c0a8ed4dc1df31ce9d */
33

44
ZEND_BEGIN_ARG_WITH_RETURN_OBJ_TYPE_MASK_EX(arginfo_snmpget, 0, 3, stdClass, MAY_BE_ARRAY|MAY_BE_STRING|MAY_BE_BOOL)
55
ZEND_ARG_TYPE_INFO(0, hostname, IS_STRING, 0)
@@ -256,45 +256,45 @@ static zend_class_entry *register_class_SNMP(void)
256256
zend_string_release(property_info_name);
257257

258258
zval property_max_oids_default_value;
259-
ZVAL_NULL(&property_max_oids_default_value);
259+
ZVAL_UNDEF(&property_max_oids_default_value);
260260
zend_string *property_max_oids_name = zend_string_init("max_oids", sizeof("max_oids") - 1, 1);
261-
zend_declare_property_ex(class_entry, property_max_oids_name, &property_max_oids_default_value, ZEND_ACC_PUBLIC, NULL);
261+
zend_declare_typed_property(class_entry, property_max_oids_name, &property_max_oids_default_value, ZEND_ACC_PUBLIC, NULL, (zend_type) ZEND_TYPE_INIT_MASK(MAY_BE_LONG|MAY_BE_NULL));
262262
zend_string_release(property_max_oids_name);
263263

264264
zval property_valueretrieval_default_value;
265-
ZVAL_NULL(&property_valueretrieval_default_value);
265+
ZVAL_UNDEF(&property_valueretrieval_default_value);
266266
zend_string *property_valueretrieval_name = zend_string_init("valueretrieval", sizeof("valueretrieval") - 1, 1);
267-
zend_declare_property_ex(class_entry, property_valueretrieval_name, &property_valueretrieval_default_value, ZEND_ACC_PUBLIC, NULL);
267+
zend_declare_typed_property(class_entry, property_valueretrieval_name, &property_valueretrieval_default_value, ZEND_ACC_PUBLIC, NULL, (zend_type) ZEND_TYPE_INIT_MASK(MAY_BE_LONG));
268268
zend_string_release(property_valueretrieval_name);
269269

270270
zval property_quick_print_default_value;
271-
ZVAL_NULL(&property_quick_print_default_value);
271+
ZVAL_UNDEF(&property_quick_print_default_value);
272272
zend_string *property_quick_print_name = zend_string_init("quick_print", sizeof("quick_print") - 1, 1);
273-
zend_declare_property_ex(class_entry, property_quick_print_name, &property_quick_print_default_value, ZEND_ACC_PUBLIC, NULL);
273+
zend_declare_typed_property(class_entry, property_quick_print_name, &property_quick_print_default_value, ZEND_ACC_PUBLIC, NULL, (zend_type) ZEND_TYPE_INIT_MASK(MAY_BE_BOOL));
274274
zend_string_release(property_quick_print_name);
275275

276276
zval property_enum_print_default_value;
277-
ZVAL_NULL(&property_enum_print_default_value);
277+
ZVAL_UNDEF(&property_enum_print_default_value);
278278
zend_string *property_enum_print_name = zend_string_init("enum_print", sizeof("enum_print") - 1, 1);
279-
zend_declare_property_ex(class_entry, property_enum_print_name, &property_enum_print_default_value, ZEND_ACC_PUBLIC, NULL);
279+
zend_declare_typed_property(class_entry, property_enum_print_name, &property_enum_print_default_value, ZEND_ACC_PUBLIC, NULL, (zend_type) ZEND_TYPE_INIT_MASK(MAY_BE_BOOL));
280280
zend_string_release(property_enum_print_name);
281281

282282
zval property_oid_output_format_default_value;
283-
ZVAL_NULL(&property_oid_output_format_default_value);
283+
ZVAL_UNDEF(&property_oid_output_format_default_value);
284284
zend_string *property_oid_output_format_name = zend_string_init("oid_output_format", sizeof("oid_output_format") - 1, 1);
285-
zend_declare_property_ex(class_entry, property_oid_output_format_name, &property_oid_output_format_default_value, ZEND_ACC_PUBLIC, NULL);
285+
zend_declare_typed_property(class_entry, property_oid_output_format_name, &property_oid_output_format_default_value, ZEND_ACC_PUBLIC, NULL, (zend_type) ZEND_TYPE_INIT_MASK(MAY_BE_LONG));
286286
zend_string_release(property_oid_output_format_name);
287287

288288
zval property_oid_increasing_check_default_value;
289-
ZVAL_BOOL(&property_oid_increasing_check_default_value, 1);
289+
ZVAL_UNDEF(&property_oid_increasing_check_default_value);
290290
zend_string *property_oid_increasing_check_name = zend_string_init("oid_increasing_check", sizeof("oid_increasing_check") - 1, 1);
291-
zend_declare_property_ex(class_entry, property_oid_increasing_check_name, &property_oid_increasing_check_default_value, ZEND_ACC_PUBLIC, NULL);
291+
zend_declare_typed_property(class_entry, property_oid_increasing_check_name, &property_oid_increasing_check_default_value, ZEND_ACC_PUBLIC, NULL, (zend_type) ZEND_TYPE_INIT_MASK(MAY_BE_BOOL));
292292
zend_string_release(property_oid_increasing_check_name);
293293

294294
zval property_exceptions_enabled_default_value;
295-
ZVAL_LONG(&property_exceptions_enabled_default_value, 0);
295+
ZVAL_UNDEF(&property_exceptions_enabled_default_value);
296296
zend_string *property_exceptions_enabled_name = zend_string_init("exceptions_enabled", sizeof("exceptions_enabled") - 1, 1);
297-
zend_declare_property_ex(class_entry, property_exceptions_enabled_name, &property_exceptions_enabled_default_value, ZEND_ACC_PUBLIC, NULL);
297+
zend_declare_typed_property(class_entry, property_exceptions_enabled_name, &property_exceptions_enabled_default_value, ZEND_ACC_PUBLIC, NULL, (zend_type) ZEND_TYPE_INIT_MASK(MAY_BE_LONG));
298298
zend_string_release(property_exceptions_enabled_name);
299299

300300
return class_entry;
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
--TEST--
2+
Test SNMP object property errors
3+
--SKIPIF--
4+
<?php
5+
require_once(__DIR__.'/skipif.inc');
6+
?>
7+
--FILE--
8+
<?php
9+
require_once(__DIR__.'/snmp_include.inc');
10+
11+
$session = new SNMP(SNMP::VERSION_1, $hostname, $community, $timeout, $retries);
12+
13+
try {
14+
$session->info = [];
15+
} catch (Error $exception) {
16+
echo $exception->getMessage() . "\n";
17+
}
18+
19+
try {
20+
$session->max_oids = [];
21+
} catch (TypeError $exception) {
22+
echo $exception->getMessage() . "\n";
23+
}
24+
25+
try {
26+
$session->max_oids = -1;
27+
} catch (ValueError $exception) {
28+
echo $exception->getMessage() . "\n";
29+
}
30+
31+
try {
32+
$session->valueretrieval = [];
33+
} catch (TypeError $exception) {
34+
echo $exception->getMessage() . "\n";
35+
}
36+
37+
try {
38+
$session->quick_print = [];
39+
} catch (TypeError $exception) {
40+
echo $exception->getMessage() . "\n";
41+
}
42+
43+
try {
44+
$session->enum_print = [];
45+
} catch (TypeError $exception) {
46+
echo $exception->getMessage() . "\n";
47+
}
48+
49+
try {
50+
$session->oid_output_format = [];
51+
} catch (TypeError $exception) {
52+
echo $exception->getMessage() . "\n";
53+
}
54+
55+
try {
56+
$session->oid_increasing_check = [];
57+
} catch (TypeError $exception) {
58+
echo $exception->getMessage() . "\n";
59+
}
60+
61+
try {
62+
$session->exceptions_enabled = [];
63+
} catch (TypeError $exception) {
64+
echo $exception->getMessage() . "\n";
65+
}
66+
67+
?>
68+
--EXPECT--
69+
Cannot write read-only property SNMP::$info
70+
Cannot assign array to property SNMP::$max_oids of type ?int
71+
SNMP::$max_oids must be greater than 0 or null
72+
Cannot assign array to property SNMP::$valueretrieval of type ?int
73+
Cannot assign array to property SNMP::$quick_print of type bool
74+
Cannot assign array to property SNMP::$enum_print of type bool
75+
Cannot assign array to property SNMP::$oid_output_format of type int
76+
Cannot assign array to property SNMP::$oid_increasing_check of type bool
77+
Cannot assign array to property SNMP::$exceptions_enabled of type int

0 commit comments

Comments
 (0)