Skip to content

Commit fb1f6dd

Browse files
committed
try out @nielsdos suggestion, delaying free object_id at the end of the call.
1 parent fc4ef43 commit fb1f6dd

File tree

2 files changed

+39
-15
lines changed

2 files changed

+39
-15
lines changed

ext/snmp/snmp.c

Lines changed: 38 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -626,6 +626,23 @@ static void php_snmp_internal(INTERNAL_FUNCTION_PARAMETERS, int st,
626626
}
627627
/* }}} */
628628

629+
static void php_snmp_zend_string_release_from_char_pointer(char *ptr) {
630+
zend_string_release((zend_string*) (ptr - XtOffsetOf(zend_string, val)));
631+
}
632+
633+
static void php_free_objid_query(struct objid_query *objid_query, zend_string* oid_str, zend_string *type_str, int st) {
634+
if (!oid_str) {
635+
for (int i = 0; i < objid_query->count; i ++) {
636+
snmpobjarg *arg = &objid_query->vars[i];
637+
if ((st & SNMP_CMD_SET) && !type_str) {
638+
php_snmp_zend_string_release_from_char_pointer(&arg->type);
639+
}
640+
php_snmp_zend_string_release_from_char_pointer(arg->oid);
641+
}
642+
}
643+
efree(objid_query->vars);
644+
}
645+
629646
/* {{{ php_snmp_parse_oid
630647
*
631648
* OID parser (and type, value for SNMP_SET command)
@@ -682,7 +699,6 @@ static bool php_snmp_parse_oid(
682699
return false;
683700
}
684701
objid_query->vars[objid_query->count].oid = ZSTR_VAL(tmp);
685-
zend_string_release(tmp);
686702
if (st & SNMP_CMD_SET) {
687703
if (type_str) {
688704
pptr = ZSTR_VAL(type_str);
@@ -706,13 +722,17 @@ static bool php_snmp_parse_oid(
706722
}
707723
}
708724
if (idx_type < type_ht->nNumUsed) {
709-
convert_to_string(tmp_type);
710-
if (Z_STRLEN_P(tmp_type) != 1) {
725+
zend_string *tmp = zval_try_get_string(tmp_type);
726+
if (!tmp) {
727+
efree(objid_query->vars);
728+
return false;
729+
}
730+
if (ZSTR_LEN(tmp) != 1) {
711731
zend_value_error("Type must be a single character");
712732
efree(objid_query->vars);
713733
return false;
714734
}
715-
pptr = Z_STRVAL_P(tmp_type);
735+
pptr = ZSTR_VAL(tmp);
716736
objid_query->vars[objid_query->count].type = *pptr;
717737
idx_type++;
718738
} else {
@@ -743,8 +763,12 @@ static bool php_snmp_parse_oid(
743763
}
744764
}
745765
if (idx_value < value_ht->nNumUsed) {
746-
convert_to_string(tmp_value);
747-
objid_query->vars[objid_query->count].value = Z_STRVAL_P(tmp_value);
766+
zend_string *tmp = zval_try_get_string(tmp_value);
767+
if (!tmp) {
768+
efree(objid_query->vars);
769+
return false;
770+
}
771+
objid_query->vars[objid_query->count].value = ZSTR_VAL(tmp);
748772
idx_value++;
749773
} else {
750774
php_error_docref(NULL, E_WARNING, "'%s': no value set", Z_STRVAL_P(tmp_oid));
@@ -761,14 +785,14 @@ static bool php_snmp_parse_oid(
761785
if (st & SNMP_CMD_WALK) {
762786
if (objid_query->count > 1) {
763787
php_snmp_error(object, PHP_SNMP_ERRNO_OID_PARSING_ERROR, "Multi OID walks are not supported!");
764-
efree(objid_query->vars);
788+
php_free_objid_query(objid_query, oid_str, type_str, st);
765789
return false;
766790
}
767791
objid_query->vars[0].name_length = MAX_NAME_LEN;
768792
if (strlen(objid_query->vars[0].oid)) { /* on a walk, an empty string means top of tree - no error */
769793
if (!snmp_parse_oid(objid_query->vars[0].oid, objid_query->vars[0].name, &(objid_query->vars[0].name_length))) {
770794
php_snmp_error(object, PHP_SNMP_ERRNO_OID_PARSING_ERROR, "Invalid object identifier: %s", objid_query->vars[0].oid);
771-
efree(objid_query->vars);
795+
php_free_objid_query(objid_query, oid_str, type_str, st);
772796
return false;
773797
}
774798
} else {
@@ -780,7 +804,7 @@ static bool php_snmp_parse_oid(
780804
objid_query->vars[objid_query->offset].name_length = MAX_OID_LEN;
781805
if (!snmp_parse_oid(objid_query->vars[objid_query->offset].oid, objid_query->vars[objid_query->offset].name, &(objid_query->vars[objid_query->offset].name_length))) {
782806
php_snmp_error(object, PHP_SNMP_ERRNO_OID_PARSING_ERROR, "Invalid object identifier: %s", objid_query->vars[objid_query->offset].oid);
783-
efree(objid_query->vars);
807+
php_free_objid_query(objid_query, oid_str, type_str, st);
784808
return false;
785809
}
786810
}
@@ -1257,12 +1281,12 @@ static void php_snmp(INTERNAL_FUNCTION_PARAMETERS, int st, int version)
12571281

12581282
if (session_less_mode) {
12591283
if (!netsnmp_session_init(&session, version, a1, a2, timeout, retries)) {
1260-
efree(objid_query.vars);
1284+
php_free_objid_query(&objid_query, oid_str, type_str, st);
12611285
netsnmp_session_free(&session);
12621286
RETURN_FALSE;
12631287
}
12641288
if (version == SNMP_VERSION_3 && !netsnmp_session_set_security(session, a3, a4, a5, a6, a7, NULL, NULL)) {
1265-
efree(objid_query.vars);
1289+
php_free_objid_query(&objid_query, oid_str, type_str, st);
12661290
netsnmp_session_free(&session);
12671291
/* Warning message sent already, just bail out */
12681292
RETURN_FALSE;
@@ -1273,7 +1297,7 @@ static void php_snmp(INTERNAL_FUNCTION_PARAMETERS, int st, int version)
12731297
session = snmp_object->session;
12741298
if (!session) {
12751299
zend_throw_error(NULL, "Invalid or uninitialized SNMP object");
1276-
efree(objid_query.vars);
1300+
php_free_objid_query(&objid_query, oid_str, type_str, st);
12771301
RETURN_THROWS();
12781302
}
12791303

@@ -1299,15 +1323,15 @@ static void php_snmp(INTERNAL_FUNCTION_PARAMETERS, int st, int version)
12991323

13001324
php_snmp_internal(INTERNAL_FUNCTION_PARAM_PASSTHRU, st, session, &objid_query);
13011325

1302-
efree(objid_query.vars);
1303-
13041326
if (session_less_mode) {
13051327
netsnmp_session_free(&session);
13061328
} else {
13071329
netsnmp_ds_set_boolean(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_PRINT_NUMERIC_ENUM, glob_snmp_object.enum_print);
13081330
netsnmp_ds_set_boolean(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_QUICK_PRINT, glob_snmp_object.quick_print);
13091331
netsnmp_ds_set_int(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_OID_OUTPUT_FORMAT, glob_snmp_object.oid_output_format);
13101332
}
1333+
1334+
php_free_objid_query(&objid_query, oid_str, type_str, st);
13111335
}
13121336
/* }}} */
13131337

ext/snmp/tests/gh16959.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ array(4) {
3535
int(0)
3636
}
3737

38-
Warning: snmpget(): Invalid object identifier: -229 in %s on line %d
38+
Warning: snmpget(): Invalid object identifier: -54 in %s on line %d
3939
bool(true)
4040
array(4) {
4141
[63]=>

0 commit comments

Comments
 (0)