Skip to content

Commit 85c4a0c

Browse files
committed
changes from early feedback, new test case
1 parent e4a8437 commit 85c4a0c

File tree

2 files changed

+34
-15
lines changed

2 files changed

+34
-15
lines changed

ext/snmp/snmp.c

Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -632,15 +632,29 @@ static void php_snmp_zend_string_release_from_char_pointer(char *ptr) {
632632
}
633633

634634
static void php_free_objid_query(struct objid_query *objid_query, HashTable* oid_ht, zend_string *value_str, HashTable *value_ht, int st) {
635+
#define PHP_FREE_OBJID_VAL(arg) \
636+
do { \
637+
if (st & SNMP_CMD_SET) { \
638+
if (value_str) { \
639+
php_snmp_zend_string_release_from_char_pointer(arg->value); \
640+
} \
641+
if (arg->type) { \
642+
php_snmp_zend_string_release_from_char_pointer(&arg->type); \
643+
} \
644+
} \
645+
if (arg->oid) { \
646+
php_snmp_zend_string_release_from_char_pointer(arg->oid); \
647+
} \
648+
} while (0)
649+
635650
if (oid_ht) {
636-
for (int i = 0; i < objid_query->count; i ++) {
637-
snmpobjarg *arg = &objid_query->vars[i];
638-
if (st & SNMP_CMD_SET) {
639-
if (!value_str && value_ht) {
640-
php_snmp_zend_string_release_from_char_pointer(arg->value);
641-
}
651+
snmpobjarg *arg = &objid_query->vars[0];
652+
PHP_FREE_OBJID_VAL(arg);
653+
if (objid_query->count > 0) {
654+
for (int i = 1; i < objid_query->count; i ++) {
655+
snmpobjarg *arg = &objid_query->vars[i];
656+
PHP_FREE_OBJID_VAL(arg);
642657
}
643-
php_snmp_zend_string_release_from_char_pointer(arg->oid);
644658
}
645659
}
646660
efree(objid_query->vars);
@@ -696,6 +710,7 @@ static bool php_snmp_parse_oid(
696710
objid_query->vars = (snmpobjarg *)safe_emalloc(sizeof(snmpobjarg), zend_hash_num_elements(oid_ht), 0);
697711
objid_query->array_output = (st & SNMP_CMD_SET) == 0;
698712
ZEND_HASH_FOREACH_VAL(oid_ht, tmp_oid) {
713+
objid_query->vars[objid_query->count].oid = NULL;
699714
zend_string *tmp = zval_try_get_string(tmp_oid);
700715
if (!tmp) {
701716
php_free_objid_query(objid_query, oid_ht, value_str, value_ht, st);
@@ -707,6 +722,7 @@ static bool php_snmp_parse_oid(
707722
pptr = ZSTR_VAL(type_str);
708723
objid_query->vars[objid_query->count].type = *pptr;
709724
} else if (type_ht) {
725+
objid_query->vars[objid_query->count].type = 0;
710726
if (HT_IS_PACKED(type_ht)) {
711727
while (idx_type < type_ht->nNumUsed) {
712728
tmp_type = &type_ht->arPacked[idx_type];
@@ -725,25 +741,22 @@ static bool php_snmp_parse_oid(
725741
}
726742
}
727743
if (idx_type < type_ht->nNumUsed) {
728-
zval new;
729-
ZVAL_COPY_VALUE(&new, tmp_type);
730-
if (!try_convert_to_string(&new)) {
731-
zend_string_release(tmp);
744+
zend_string *type = zval_try_get_string(tmp_type);
745+
if (!type) {
732746
php_free_objid_query(objid_query, oid_ht, value_str, value_ht, st);
733747
return false;
734748
}
735-
if (Z_STRLEN(new) != 1) {
749+
if (ZSTR_LEN(type) != 1) {
736750
zend_value_error("Type must be a single character");
737-
zend_string_release(tmp);
751+
zend_string_release(type);
738752
php_free_objid_query(objid_query, oid_ht, value_str, value_ht, st);
739753
return false;
740754
}
741-
pptr = Z_STRVAL(new);
755+
pptr = ZSTR_VAL(type);
742756
objid_query->vars[objid_query->count].type = *pptr;
743757
idx_type++;
744758
} else {
745759
php_error_docref(NULL, E_WARNING, "'%s': no type set", ZSTR_VAL(tmp));
746-
zend_string_release(tmp);
747760
php_free_objid_query(objid_query, oid_ht, value_str, value_ht, st);
748761
return false;
749762
}

ext/snmp/tests/gh16959.phpt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,11 @@ try {
3030
}
3131
try {
3232
snmp2_set($hostname, $communityWrite, $bad_object_ids, array("toolongtype"), array(null));
33+
} catch (Throwable $e) {
34+
echo $e->getMessage() . PHP_EOL;
35+
}
36+
try {
37+
snmp2_set($hostname, $communityWrite, $bad_object_ids, array(str_repeat("onetoomuch", random_int(1, 1))), array(null));
3338
} catch (Throwable $e) {
3439
echo $e->getMessage();
3540
}
@@ -61,3 +66,4 @@ array(4) {
6166
Object of class stdClass could not be converted to string
6267
Object of class stdClass could not be converted to string
6368
Type must be a single character
69+
Type must be a single character

0 commit comments

Comments
 (0)