@@ -626,6 +626,31 @@ static void php_snmp_internal(INTERNAL_FUNCTION_PARAMETERS, int st,
626
626
}
627
627
/* }}} */
628
628
629
+ static void php_snmp_zend_string_release_from_char_pointer (char * ptr ) {
630
+ if (ptr ) {
631
+ zend_string * pptr = (zend_string * )(ptr - XtOffsetOf (zend_string , val ));
632
+ zend_string_release (pptr );
633
+ }
634
+ }
635
+
636
+ static void php_free_objid_query (struct objid_query * objid_query , HashTable * oid_ht , const zend_string * value_str , const HashTable * value_ht , int st ) {
637
+ if (oid_ht ) {
638
+ uint32_t count = zend_hash_num_elements (oid_ht );
639
+
640
+ for (uint32_t i = 0 ; i < count ; i ++ ) {
641
+ snmpobjarg * arg = & objid_query -> vars [i ];
642
+ if (!arg -> oid ) {
643
+ break ;
644
+ }
645
+ if (value_ht && !value_str ) {
646
+ php_snmp_zend_string_release_from_char_pointer (arg -> value );
647
+ }
648
+ php_snmp_zend_string_release_from_char_pointer (arg -> oid );
649
+ }
650
+ }
651
+ efree (objid_query -> vars );
652
+ }
653
+
629
654
/* {{{ php_snmp_parse_oid
630
655
*
631
656
* OID parser (and type, value for SNMP_SET command)
@@ -674,15 +699,15 @@ static bool php_snmp_parse_oid(
674
699
return false;
675
700
}
676
701
objid_query -> vars = (snmpobjarg * )safe_emalloc (sizeof (snmpobjarg ), zend_hash_num_elements (oid_ht ), 0 );
702
+ memset (objid_query -> vars , 0 , sizeof (snmpobjarg ) * zend_hash_num_elements (oid_ht ));
677
703
objid_query -> array_output = (st & SNMP_CMD_SET ) == 0 ;
678
704
ZEND_HASH_FOREACH_VAL (oid_ht , tmp_oid ) {
679
705
zend_string * tmp = zval_try_get_string (tmp_oid );
680
706
if (!tmp ) {
681
- efree (objid_query -> vars );
707
+ php_free_objid_query (objid_query , oid_ht , value_str , value_ht , st );
682
708
return false;
683
709
}
684
710
objid_query -> vars [objid_query -> count ].oid = ZSTR_VAL (tmp );
685
- zend_string_release (tmp );
686
711
if (st & SNMP_CMD_SET ) {
687
712
if (type_str ) {
688
713
pptr = ZSTR_VAL (type_str );
@@ -706,18 +731,24 @@ static bool php_snmp_parse_oid(
706
731
}
707
732
}
708
733
if (idx_type < type_ht -> nNumUsed ) {
709
- convert_to_string (tmp_type );
710
- if (Z_STRLEN_P (tmp_type ) != 1 ) {
734
+ zend_string * type = zval_try_get_string (tmp_type );
735
+ if (!type ) {
736
+ php_free_objid_query (objid_query , oid_ht , value_str , value_ht , st );
737
+ return false;
738
+ }
739
+ size_t len = ZSTR_LEN (type );
740
+ char ptype = * ZSTR_VAL (type );
741
+ zend_string_release (type );
742
+ if (len != 1 ) {
711
743
zend_value_error ("Type must be a single character" );
712
- efree (objid_query -> vars );
744
+ php_free_objid_query (objid_query , oid_ht , value_str , value_ht , st );
713
745
return false;
714
746
}
715
- pptr = Z_STRVAL_P (tmp_type );
716
- objid_query -> vars [objid_query -> count ].type = * pptr ;
747
+ objid_query -> vars [objid_query -> count ].type = ptype ;
717
748
idx_type ++ ;
718
749
} else {
719
- php_error_docref (NULL , E_WARNING , "'%s': no type set" , Z_STRVAL_P ( tmp_oid ));
720
- efree (objid_query -> vars );
750
+ php_error_docref (NULL , E_WARNING , "'%s': no type set" , ZSTR_VAL ( tmp ));
751
+ php_free_objid_query (objid_query , oid_ht , value_str , value_ht , st );
721
752
return false;
722
753
}
723
754
}
@@ -743,12 +774,16 @@ static bool php_snmp_parse_oid(
743
774
}
744
775
}
745
776
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 );
777
+ zend_string * tmp = zval_try_get_string (tmp_value );
778
+ if (!tmp ) {
779
+ php_free_objid_query (objid_query , oid_ht , value_str , value_ht , st );
780
+ return false;
781
+ }
782
+ objid_query -> vars [objid_query -> count ].value = ZSTR_VAL (tmp );
748
783
idx_value ++ ;
749
784
} else {
750
- php_error_docref (NULL , E_WARNING , "'%s': no value set" , Z_STRVAL_P ( tmp_oid ));
751
- efree (objid_query -> vars );
785
+ php_error_docref (NULL , E_WARNING , "'%s': no value set" , ZSTR_VAL ( tmp ));
786
+ php_free_objid_query (objid_query , oid_ht , value_str , value_ht , st );
752
787
return false;
753
788
}
754
789
}
@@ -761,14 +796,14 @@ static bool php_snmp_parse_oid(
761
796
if (st & SNMP_CMD_WALK ) {
762
797
if (objid_query -> count > 1 ) {
763
798
php_snmp_error (object , PHP_SNMP_ERRNO_OID_PARSING_ERROR , "Multi OID walks are not supported!" );
764
- efree (objid_query -> vars );
799
+ php_free_objid_query (objid_query , oid_ht , value_str , value_ht , st );
765
800
return false;
766
801
}
767
802
objid_query -> vars [0 ].name_length = MAX_NAME_LEN ;
768
803
if (strlen (objid_query -> vars [0 ].oid )) { /* on a walk, an empty string means top of tree - no error */
769
804
if (!snmp_parse_oid (objid_query -> vars [0 ].oid , objid_query -> vars [0 ].name , & (objid_query -> vars [0 ].name_length ))) {
770
805
php_snmp_error (object , PHP_SNMP_ERRNO_OID_PARSING_ERROR , "Invalid object identifier: %s" , objid_query -> vars [0 ].oid );
771
- efree (objid_query -> vars );
806
+ php_free_objid_query (objid_query , oid_ht , value_str , value_ht , st );
772
807
return false;
773
808
}
774
809
} else {
@@ -780,7 +815,7 @@ static bool php_snmp_parse_oid(
780
815
objid_query -> vars [objid_query -> offset ].name_length = MAX_OID_LEN ;
781
816
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 ))) {
782
817
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 );
818
+ php_free_objid_query (objid_query , oid_ht , value_str , value_ht , st );
784
819
return false;
785
820
}
786
821
}
@@ -1257,12 +1292,12 @@ static void php_snmp(INTERNAL_FUNCTION_PARAMETERS, int st, int version)
1257
1292
1258
1293
if (session_less_mode ) {
1259
1294
if (!netsnmp_session_init (& session , version , a1 , a2 , timeout , retries )) {
1260
- efree ( objid_query . vars );
1295
+ php_free_objid_query ( & objid_query , oid_ht , value_str , value_ht , st );
1261
1296
netsnmp_session_free (& session );
1262
1297
RETURN_FALSE ;
1263
1298
}
1264
1299
if (version == SNMP_VERSION_3 && !netsnmp_session_set_security (session , a3 , a4 , a5 , a6 , a7 , NULL , NULL )) {
1265
- efree ( objid_query . vars );
1300
+ php_free_objid_query ( & objid_query , oid_ht , value_str , value_ht , st );
1266
1301
netsnmp_session_free (& session );
1267
1302
/* Warning message sent already, just bail out */
1268
1303
RETURN_FALSE ;
@@ -1273,7 +1308,7 @@ static void php_snmp(INTERNAL_FUNCTION_PARAMETERS, int st, int version)
1273
1308
session = snmp_object -> session ;
1274
1309
if (!session ) {
1275
1310
zend_throw_error (NULL , "Invalid or uninitialized SNMP object" );
1276
- efree ( objid_query . vars );
1311
+ php_free_objid_query ( & objid_query , oid_ht , value_str , value_ht , st );
1277
1312
RETURN_THROWS ();
1278
1313
}
1279
1314
@@ -1299,7 +1334,7 @@ static void php_snmp(INTERNAL_FUNCTION_PARAMETERS, int st, int version)
1299
1334
1300
1335
php_snmp_internal (INTERNAL_FUNCTION_PARAM_PASSTHRU , st , session , & objid_query );
1301
1336
1302
- efree ( objid_query . vars );
1337
+ php_free_objid_query ( & objid_query , oid_ht , value_str , value_ht , st );
1303
1338
1304
1339
if (session_less_mode ) {
1305
1340
netsnmp_session_free (& session );
0 commit comments