Skip to content

Declare SNMP properties #6742

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 26 additions & 20 deletions ext/snmp/snmp.c
Original file line number Diff line number Diff line change
Expand Up @@ -1677,19 +1677,30 @@ zval *php_snmp_write_property(zend_object *object, zend_string *name, zval *valu
obj = php_snmp_fetch_object(object);
hnd = zend_hash_find_ptr(&php_snmp_properties, name);

if (hnd && hnd->write_func) {
hnd->write_func(obj, value);
/*
if (!PZVAL_IS_REF(value) && Z_REFCOUNT_P(value) == 0) {
Z_ADDREF_P(value);
zval_ptr_dtor(&value);
if (hnd) {
if (!hnd->write_func) {
zend_throw_error(NULL, "Cannot write read-only property %s::$%s", ZSTR_VAL(object->ce->name), ZSTR_VAL(name));
return &EG(error_zval);
}
*/
} else {
value = zend_std_write_property(object, name, value, cache_slot);

zend_property_info *prop = zend_get_property_info(object->ce, name, /* silent */ true);
if (prop && ZEND_TYPE_IS_SET(prop->type)) {
zval tmp;
ZVAL_COPY(&tmp, value);
if (!zend_verify_property_type(prop, &tmp,
ZEND_CALL_USES_STRICT_TYPES(EG(current_execute_data)))) {
zval_ptr_dtor(&tmp);
return &EG(error_zval);
}
hnd->write_func(obj, &tmp);
zval_ptr_dtor(&tmp);
} else {
hnd->write_func(obj, value);
}
return value;
}

return value;
return zend_std_write_property(object, name, value, cache_slot);
}
/* }}} */

Expand Down Expand Up @@ -1820,14 +1831,6 @@ PHP_SNMP_LONG_PROPERTY_READER_FUNCTION(valueretrieval)
PHP_SNMP_LONG_PROPERTY_READER_FUNCTION(oid_output_format)
PHP_SNMP_LONG_PROPERTY_READER_FUNCTION(exceptions_enabled)

/* {{{ */
static int php_snmp_write_info(php_snmp_object *snmp_object, zval *newval)
{
zend_throw_error(NULL, "SNMP::$info property is read-only");
return FAILURE;
}
/* }}} */

/* {{{ */
static int php_snmp_write_max_oids(php_snmp_object *snmp_object, zval *newval)
{
Expand All @@ -1841,7 +1844,7 @@ static int php_snmp_write_max_oids(php_snmp_object *snmp_object, zval *newval)
lval = zval_get_long(newval);

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

#define PHP_SNMP_READONLY_PROPERTY_ENTRY_RECORD(name) \
{ "" #name "", sizeof("" #name "") - 1, php_snmp_read_##name, NULL }

const php_snmp_prop_handler php_snmp_property_entries[] = {
PHP_SNMP_PROPERTY_ENTRY_RECORD(info),
PHP_SNMP_READONLY_PROPERTY_ENTRY_RECORD(info),
PHP_SNMP_PROPERTY_ENTRY_RECORD(max_oids),
PHP_SNMP_PROPERTY_ENTRY_RECORD(valueretrieval),
PHP_SNMP_PROPERTY_ENTRY_RECORD(quick_print),
Expand Down
10 changes: 10 additions & 0 deletions ext/snmp/snmp.stub.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,16 @@ function snmp_read_mib(string $filename): bool {}

class SNMP
{
/** @readonly */
public array $info;
public ?int $max_oids;
public int $valueretrieval;
public bool $quick_print;
public bool $enum_print;
public int $oid_output_format;
public bool $oid_increasing_check;
public int $exceptions_enabled;

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

/** @return bool */
Expand Down
50 changes: 49 additions & 1 deletion ext/snmp/snmp_arginfo.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* This is a generated file, edit the .stub.php file instead.
* Stub hash: 08192d87d2ac5d35092cfcf4a2cdcc50f7ec4ada */
* Stub hash: 5258c5796aca15e369dd72c0a8ed4dc1df31ce9d */

ZEND_BEGIN_ARG_WITH_RETURN_OBJ_TYPE_MASK_EX(arginfo_snmpget, 0, 3, stdClass, MAY_BE_ARRAY|MAY_BE_STRING|MAY_BE_BOOL)
ZEND_ARG_TYPE_INFO(0, hostname, IS_STRING, 0)
Expand Down Expand Up @@ -249,6 +249,54 @@ static zend_class_entry *register_class_SNMP(void)
INIT_CLASS_ENTRY(ce, "SNMP", class_SNMP_methods);
class_entry = zend_register_internal_class_ex(&ce, NULL);

zval property_info_default_value;
ZVAL_UNDEF(&property_info_default_value);
zend_string *property_info_name = zend_string_init("info", sizeof("info") - 1, 1);
zend_declare_typed_property(class_entry, property_info_name, &property_info_default_value, ZEND_ACC_PUBLIC, NULL, (zend_type) ZEND_TYPE_INIT_MASK(MAY_BE_ARRAY));
zend_string_release(property_info_name);

zval property_max_oids_default_value;
ZVAL_UNDEF(&property_max_oids_default_value);
zend_string *property_max_oids_name = zend_string_init("max_oids", sizeof("max_oids") - 1, 1);
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));
zend_string_release(property_max_oids_name);

zval property_valueretrieval_default_value;
ZVAL_UNDEF(&property_valueretrieval_default_value);
zend_string *property_valueretrieval_name = zend_string_init("valueretrieval", sizeof("valueretrieval") - 1, 1);
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));
zend_string_release(property_valueretrieval_name);

zval property_quick_print_default_value;
ZVAL_UNDEF(&property_quick_print_default_value);
zend_string *property_quick_print_name = zend_string_init("quick_print", sizeof("quick_print") - 1, 1);
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));
zend_string_release(property_quick_print_name);

zval property_enum_print_default_value;
ZVAL_UNDEF(&property_enum_print_default_value);
zend_string *property_enum_print_name = zend_string_init("enum_print", sizeof("enum_print") - 1, 1);
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));
zend_string_release(property_enum_print_name);

zval property_oid_output_format_default_value;
ZVAL_UNDEF(&property_oid_output_format_default_value);
zend_string *property_oid_output_format_name = zend_string_init("oid_output_format", sizeof("oid_output_format") - 1, 1);
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));
zend_string_release(property_oid_output_format_name);

zval property_oid_increasing_check_default_value;
ZVAL_UNDEF(&property_oid_increasing_check_default_value);
zend_string *property_oid_increasing_check_name = zend_string_init("oid_increasing_check", sizeof("oid_increasing_check") - 1, 1);
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));
zend_string_release(property_oid_increasing_check_name);

zval property_exceptions_enabled_default_value;
ZVAL_UNDEF(&property_exceptions_enabled_default_value);
zend_string *property_exceptions_enabled_name = zend_string_init("exceptions_enabled", sizeof("exceptions_enabled") - 1, 1);
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));
zend_string_release(property_exceptions_enabled_name);

return class_entry;
}

Expand Down
6 changes: 3 additions & 3 deletions ext/snmp/tests/snmp-object-error.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ $session = new SNMP(SNMP::VERSION_2c, $hostname, $community, $timeout, $retries)
var_dump($session->max_oids);
try {
$session->max_oids = "ttt";
} catch (\ValueError $e) {
} catch (TypeError $e) {
echo $e->getMessage() . \PHP_EOL;
}
try {
Expand Down Expand Up @@ -103,6 +103,6 @@ Closing session
bool(true)
Invalid or uninitialized SNMP object
NULL
max_oids must be greater than 0 or null
max_oids must be greater than 0 or null
Cannot assign string to property SNMP::$max_oids of type ?int
SNMP::$max_oids must be greater than 0 or null
NULL
77 changes: 77 additions & 0 deletions ext/snmp/tests/snmp-object-properties-error.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
--TEST--
Test SNMP object property errors
--SKIPIF--
<?php
require_once(__DIR__.'/skipif.inc');
?>
--FILE--
<?php
require_once(__DIR__.'/snmp_include.inc');

$session = new SNMP(SNMP::VERSION_1, $hostname, $community, $timeout, $retries);

try {
$session->info = [];
} catch (Error $exception) {
echo $exception->getMessage() . "\n";
}

try {
$session->max_oids = [];
} catch (TypeError $exception) {
echo $exception->getMessage() . "\n";
}

try {
$session->max_oids = -1;
} catch (ValueError $exception) {
echo $exception->getMessage() . "\n";
}

try {
$session->valueretrieval = [];
} catch (TypeError $exception) {
echo $exception->getMessage() . "\n";
}

try {
$session->quick_print = [];
} catch (TypeError $exception) {
echo $exception->getMessage() . "\n";
}

try {
$session->enum_print = [];
} catch (TypeError $exception) {
echo $exception->getMessage() . "\n";
}

try {
$session->oid_output_format = [];
} catch (TypeError $exception) {
echo $exception->getMessage() . "\n";
}

try {
$session->oid_increasing_check = [];
} catch (TypeError $exception) {
echo $exception->getMessage() . "\n";
}

try {
$session->exceptions_enabled = [];
} catch (TypeError $exception) {
echo $exception->getMessage() . "\n";
}

?>
--EXPECT--
Cannot write read-only property SNMP::$info
Cannot assign array to property SNMP::$max_oids of type ?int
SNMP::$max_oids must be greater than 0 or null
Cannot assign array to property SNMP::$valueretrieval of type int
Cannot assign array to property SNMP::$quick_print of type bool
Cannot assign array to property SNMP::$enum_print of type bool
Cannot assign array to property SNMP::$oid_output_format of type int
Cannot assign array to property SNMP::$oid_increasing_check of type bool
Cannot assign array to property SNMP::$exceptions_enabled of type int
2 changes: 1 addition & 1 deletion ext/snmp/tests/snmp-object-properties.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -192,5 +192,5 @@ NULL
bool(false)
SNMP retrieval method must be a bitmask of SNMP_VALUE_LIBRARY, SNMP_VALUE_PLAIN, and SNMP_VALUE_OBJECT
SNMP output print format must be an SNMP_OID_OUTPUT_* constant
SNMP::$info property is read-only
Cannot write read-only property SNMP::$info
NULL