@@ -626,6 +626,40 @@ 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
+ if (GC_REFCOUNT (pptr )) {
633
+ zend_string_release (pptr );
634
+ }
635
+ }
636
+ }
637
+
638
+ static void php_free_objid_query (struct objid_query * objid_query , HashTable * oid_ht , zend_string * value_str , HashTable * value_ht , int st ) {
639
+ #define PHP_FREE_OBJID_VAL (arg ) \
640
+ do { \
641
+ if (value_ht && !value_str) { \
642
+ php_snmp_zend_string_release_from_char_pointer(arg->value); \
643
+ } \
644
+ php_snmp_zend_string_release_from_char_pointer(&arg->type); \
645
+ php_snmp_zend_string_release_from_char_pointer(arg->oid); \
646
+ } while (0)
647
+
648
+ if (oid_ht ) {
649
+ uint32_t i = 0 , count = zend_hash_num_elements (oid_ht );
650
+
651
+ while (i < count ) {
652
+ snmpobjarg * arg = & objid_query -> vars [i ];
653
+ if (!arg -> oid ) {
654
+ break ;
655
+ }
656
+ PHP_FREE_OBJID_VAL (arg );
657
+ i ++ ;
658
+ }
659
+ }
660
+ efree (objid_query -> vars );
661
+ }
662
+
629
663
/* {{{ php_snmp_parse_oid
630
664
*
631
665
* OID parser (and type, value for SNMP_SET command)
@@ -674,15 +708,15 @@ static bool php_snmp_parse_oid(
674
708
return false;
675
709
}
676
710
objid_query -> vars = (snmpobjarg * )safe_emalloc (sizeof (snmpobjarg ), zend_hash_num_elements (oid_ht ), 0 );
711
+ memset (objid_query -> vars , 0 , sizeof (snmpobjarg ) * zend_hash_num_elements (oid_ht ));
677
712
objid_query -> array_output = (st & SNMP_CMD_SET ) == 0 ;
678
713
ZEND_HASH_FOREACH_VAL (oid_ht , tmp_oid ) {
679
714
zend_string * tmp = zval_try_get_string (tmp_oid );
680
715
if (!tmp ) {
681
- efree (objid_query -> vars );
716
+ php_free_objid_query (objid_query , oid_ht , value_str , value_ht , st );
682
717
return false;
683
718
}
684
719
objid_query -> vars [objid_query -> count ].oid = ZSTR_VAL (tmp );
685
- zend_string_release (tmp );
686
720
if (st & SNMP_CMD_SET ) {
687
721
if (type_str ) {
688
722
pptr = ZSTR_VAL (type_str );
@@ -706,18 +740,23 @@ static bool php_snmp_parse_oid(
706
740
}
707
741
}
708
742
if (idx_type < type_ht -> nNumUsed ) {
709
- convert_to_string (tmp_type );
710
- if (Z_STRLEN_P (tmp_type ) != 1 ) {
743
+ zend_string * type = zval_try_get_string (tmp_type );
744
+ if (!type ) {
745
+ php_free_objid_query (objid_query , oid_ht , value_str , value_ht , st );
746
+ return false;
747
+ }
748
+ if (ZSTR_LEN (type ) != 1 ) {
711
749
zend_value_error ("Type must be a single character" );
712
- efree (objid_query -> vars );
750
+ zend_string_release (type );
751
+ php_free_objid_query (objid_query , oid_ht , value_str , value_ht , st );
713
752
return false;
714
753
}
715
- pptr = Z_STRVAL_P ( tmp_type );
754
+ pptr = ZSTR_VAL ( type );
716
755
objid_query -> vars [objid_query -> count ].type = * pptr ;
717
756
idx_type ++ ;
718
757
} else {
719
- php_error_docref (NULL , E_WARNING , "'%s': no type set" , Z_STRVAL_P ( tmp_oid ));
720
- efree (objid_query -> vars );
758
+ php_error_docref (NULL , E_WARNING , "'%s': no type set" , ZSTR_VAL ( tmp ));
759
+ php_free_objid_query (objid_query , oid_ht , value_str , value_ht , st );
721
760
return false;
722
761
}
723
762
}
@@ -743,12 +782,16 @@ static bool php_snmp_parse_oid(
743
782
}
744
783
}
745
784
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 );
785
+ zend_string * tmp = zval_try_get_string (tmp_value );
786
+ if (!tmp ) {
787
+ php_free_objid_query (objid_query , oid_ht , value_str , value_ht , st );
788
+ return false;
789
+ }
790
+ objid_query -> vars [objid_query -> count ].value = ZSTR_VAL (tmp );
748
791
idx_value ++ ;
749
792
} else {
750
- php_error_docref (NULL , E_WARNING , "'%s': no value set" , Z_STRVAL_P ( tmp_oid ));
751
- efree (objid_query -> vars );
793
+ php_error_docref (NULL , E_WARNING , "'%s': no value set" , ZSTR_VAL ( tmp ));
794
+ php_free_objid_query (objid_query , oid_ht , value_str , value_ht , st );
752
795
return false;
753
796
}
754
797
}
@@ -761,14 +804,14 @@ static bool php_snmp_parse_oid(
761
804
if (st & SNMP_CMD_WALK ) {
762
805
if (objid_query -> count > 1 ) {
763
806
php_snmp_error (object , PHP_SNMP_ERRNO_OID_PARSING_ERROR , "Multi OID walks are not supported!" );
764
- efree (objid_query -> vars );
807
+ php_free_objid_query (objid_query , oid_ht , value_str , value_ht , st );
765
808
return false;
766
809
}
767
810
objid_query -> vars [0 ].name_length = MAX_NAME_LEN ;
768
811
if (strlen (objid_query -> vars [0 ].oid )) { /* on a walk, an empty string means top of tree - no error */
769
812
if (!snmp_parse_oid (objid_query -> vars [0 ].oid , objid_query -> vars [0 ].name , & (objid_query -> vars [0 ].name_length ))) {
770
813
php_snmp_error (object , PHP_SNMP_ERRNO_OID_PARSING_ERROR , "Invalid object identifier: %s" , objid_query -> vars [0 ].oid );
771
- efree (objid_query -> vars );
814
+ php_free_objid_query (objid_query , oid_ht , value_str , value_ht , st );
772
815
return false;
773
816
}
774
817
} else {
@@ -780,7 +823,7 @@ static bool php_snmp_parse_oid(
780
823
objid_query -> vars [objid_query -> offset ].name_length = MAX_OID_LEN ;
781
824
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
825
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 );
826
+ php_free_objid_query (objid_query , oid_ht , value_str , value_ht , st );
784
827
return false;
785
828
}
786
829
}
@@ -1257,12 +1300,12 @@ static void php_snmp(INTERNAL_FUNCTION_PARAMETERS, int st, int version)
1257
1300
1258
1301
if (session_less_mode ) {
1259
1302
if (!netsnmp_session_init (& session , version , a1 , a2 , timeout , retries )) {
1260
- efree ( objid_query . vars );
1303
+ php_free_objid_query ( & objid_query , oid_ht , value_str , value_ht , st );
1261
1304
netsnmp_session_free (& session );
1262
1305
RETURN_FALSE ;
1263
1306
}
1264
1307
if (version == SNMP_VERSION_3 && !netsnmp_session_set_security (session , a3 , a4 , a5 , a6 , a7 , NULL , NULL )) {
1265
- efree ( objid_query . vars );
1308
+ php_free_objid_query ( & objid_query , oid_ht , value_str , value_ht , st );
1266
1309
netsnmp_session_free (& session );
1267
1310
/* Warning message sent already, just bail out */
1268
1311
RETURN_FALSE ;
@@ -1273,7 +1316,7 @@ static void php_snmp(INTERNAL_FUNCTION_PARAMETERS, int st, int version)
1273
1316
session = snmp_object -> session ;
1274
1317
if (!session ) {
1275
1318
zend_throw_error (NULL , "Invalid or uninitialized SNMP object" );
1276
- efree ( objid_query . vars );
1319
+ php_free_objid_query ( & objid_query , oid_ht , value_str , value_ht , st );
1277
1320
RETURN_THROWS ();
1278
1321
}
1279
1322
@@ -1299,7 +1342,7 @@ static void php_snmp(INTERNAL_FUNCTION_PARAMETERS, int st, int version)
1299
1342
1300
1343
php_snmp_internal (INTERNAL_FUNCTION_PARAM_PASSTHRU , st , session , & objid_query );
1301
1344
1302
- efree ( objid_query . vars );
1345
+ php_free_objid_query ( & objid_query , oid_ht , value_str , value_ht , st );
1303
1346
1304
1347
if (session_less_mode ) {
1305
1348
netsnmp_session_free (& session );
0 commit comments