Skip to content

Commit d572b1b

Browse files
committed
Fix GH-16959: snmpget modifies the object_id (as array).
Instead of modifying the zval, we use the zend_try_get_string.
1 parent 97b0318 commit d572b1b

File tree

2 files changed

+50
-2
lines changed

2 files changed

+50
-2
lines changed

ext/snmp/snmp.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -676,8 +676,12 @@ static bool php_snmp_parse_oid(
676676
objid_query->vars = (snmpobjarg *)safe_emalloc(sizeof(snmpobjarg), zend_hash_num_elements(oid_ht), 0);
677677
objid_query->array_output = (st & SNMP_CMD_SET) == 0;
678678
ZEND_HASH_FOREACH_VAL(oid_ht, tmp_oid) {
679-
convert_to_string(tmp_oid);
680-
objid_query->vars[objid_query->count].oid = Z_STRVAL_P(tmp_oid);
679+
zend_string *tmp = zval_try_get_string(tmp_oid);
680+
if (!tmp) {
681+
continue;
682+
}
683+
objid_query->vars[objid_query->count].oid = ZSTR_VAL(tmp);
684+
zend_string_release(tmp);
681685
if (st & SNMP_CMD_SET) {
682686
if (type_str) {
683687
pptr = ZSTR_VAL(type_str);

ext/snmp/tests/gh16959.phpt

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
--TEST--
2+
snmpget() modifies object_id array source
3+
--EXTENSIONS--
4+
snmp
5+
--SKIPIF--
6+
<?php
7+
require_once(__DIR__.'/skipif.inc');
8+
?>
9+
--FILE--
10+
<?php
11+
require_once(__DIR__.'/snmp_include.inc');
12+
13+
$bad_object_ids = array (
14+
077 => 077, -066 => -066, -0345 => -0345, 0 => 0
15+
);
16+
var_dump($bad_object_ids);
17+
var_dump(snmpget($hostname, "", $bad_object_ids) === false);
18+
// The array should remain unmodified
19+
var_dump($bad_object_ids);
20+
?>
21+
--EXPECTF--
22+
array(4) {
23+
[63]=>
24+
int(63)
25+
[-54]=>
26+
int(-54)
27+
[-229]=>
28+
int(-229)
29+
[0]=>
30+
int(0)
31+
}
32+
33+
Warning: snmpget(): Invalid object identifier: -229 in %s on line %d
34+
bool(true)
35+
array(4) {
36+
[63]=>
37+
int(63)
38+
[-54]=>
39+
int(-54)
40+
[-229]=>
41+
int(-229)
42+
[0]=>
43+
int(0)
44+
}

0 commit comments

Comments
 (0)