@@ -627,20 +627,34 @@ static void php_snmp_internal(INTERNAL_FUNCTION_PARAMETERS, int st,
627
627
/* }}} */
628
628
629
629
static void php_snmp_zend_string_release_from_char_pointer (char * ptr ) {
630
- zend_string * pptr = (zend_string * )(ptr - XtOffsetOf (zend_string , val ));
631
- zend_string_release (pptr );
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
+ }
632
636
}
633
637
634
- static void php_free_objid_query (struct objid_query * objid_query , zend_string * oid_str , zend_string * value_str , HashTable * value_ht , int st ) {
635
- if (!oid_str ) {
636
- for (int i = 0 ; i < objid_query -> count ; i ++ ) {
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_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 ) {
637
652
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
- }
653
+ if (!arg -> oid ) {
654
+ break ;
642
655
}
643
- php_snmp_zend_string_release_from_char_pointer (arg -> oid );
656
+ PHP_FREE_OBJID_VAL (arg );
657
+ i ++ ;
644
658
}
645
659
}
646
660
efree (objid_query -> vars );
@@ -694,11 +708,12 @@ static bool php_snmp_parse_oid(
694
708
return false;
695
709
}
696
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 ));
697
712
objid_query -> array_output = (st & SNMP_CMD_SET ) == 0 ;
698
713
ZEND_HASH_FOREACH_VAL (oid_ht , tmp_oid ) {
699
714
zend_string * tmp = zval_try_get_string (tmp_oid );
700
715
if (!tmp ) {
701
- efree (objid_query -> vars );
716
+ php_free_objid_query (objid_query , oid_ht , value_str , value_ht , st );
702
717
return false;
703
718
}
704
719
objid_query -> vars [objid_query -> count ].oid = ZSTR_VAL (tmp );
@@ -725,23 +740,23 @@ static bool php_snmp_parse_oid(
725
740
}
726
741
}
727
742
if (idx_type < type_ht -> nNumUsed ) {
728
- zval new ;
729
- ZVAL_COPY_VALUE (& new , tmp_type );
730
- if (!try_convert_to_string (& new )) {
731
- php_free_objid_query (objid_query , oid_str , value_str , value_ht , st );
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 );
732
746
return false;
733
747
}
734
- if (Z_STRLEN ( new ) != 1 ) {
748
+ if (ZSTR_LEN ( type ) != 1 ) {
735
749
zend_value_error ("Type must be a single character" );
736
- php_free_objid_query (objid_query , oid_str , value_str , value_ht , st );
750
+ zend_string_release (type );
751
+ php_free_objid_query (objid_query , oid_ht , value_str , value_ht , st );
737
752
return false;
738
753
}
739
- pptr = Z_STRVAL ( new );
754
+ pptr = ZSTR_VAL ( type );
740
755
objid_query -> vars [objid_query -> count ].type = * pptr ;
741
756
idx_type ++ ;
742
757
} else {
743
- php_error_docref (NULL , E_WARNING , "'%s': no type set" , Z_STRVAL_P ( tmp_oid ));
744
- php_free_objid_query (objid_query , oid_str , value_str , value_ht , st );
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 );
745
760
return false;
746
761
}
747
762
}
@@ -769,14 +784,14 @@ static bool php_snmp_parse_oid(
769
784
if (idx_value < value_ht -> nNumUsed ) {
770
785
zend_string * tmp = zval_try_get_string (tmp_value );
771
786
if (!tmp ) {
772
- php_free_objid_query (objid_query , oid_str , value_str , value_ht , st );
787
+ php_free_objid_query (objid_query , oid_ht , value_str , value_ht , st );
773
788
return false;
774
789
}
775
790
objid_query -> vars [objid_query -> count ].value = ZSTR_VAL (tmp );
776
791
idx_value ++ ;
777
792
} else {
778
- php_error_docref (NULL , E_WARNING , "'%s': no value set" , Z_STRVAL_P ( tmp_oid ));
779
- php_free_objid_query (objid_query , oid_str , value_str , value_ht , st );
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 );
780
795
return false;
781
796
}
782
797
}
@@ -789,14 +804,14 @@ static bool php_snmp_parse_oid(
789
804
if (st & SNMP_CMD_WALK ) {
790
805
if (objid_query -> count > 1 ) {
791
806
php_snmp_error (object , PHP_SNMP_ERRNO_OID_PARSING_ERROR , "Multi OID walks are not supported!" );
792
- php_free_objid_query (objid_query , oid_str , value_str , value_ht , st );
807
+ php_free_objid_query (objid_query , oid_ht , value_str , value_ht , st );
793
808
return false;
794
809
}
795
810
objid_query -> vars [0 ].name_length = MAX_NAME_LEN ;
796
811
if (strlen (objid_query -> vars [0 ].oid )) { /* on a walk, an empty string means top of tree - no error */
797
812
if (!snmp_parse_oid (objid_query -> vars [0 ].oid , objid_query -> vars [0 ].name , & (objid_query -> vars [0 ].name_length ))) {
798
813
php_snmp_error (object , PHP_SNMP_ERRNO_OID_PARSING_ERROR , "Invalid object identifier: %s" , objid_query -> vars [0 ].oid );
799
- php_free_objid_query (objid_query , oid_str , value_str , value_ht , st );
814
+ php_free_objid_query (objid_query , oid_ht , value_str , value_ht , st );
800
815
return false;
801
816
}
802
817
} else {
@@ -808,7 +823,7 @@ static bool php_snmp_parse_oid(
808
823
objid_query -> vars [objid_query -> offset ].name_length = MAX_OID_LEN ;
809
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 ))) {
810
825
php_snmp_error (object , PHP_SNMP_ERRNO_OID_PARSING_ERROR , "Invalid object identifier: %s" , objid_query -> vars [objid_query -> offset ].oid );
811
- php_free_objid_query (objid_query , oid_str , value_str , value_ht , st );
826
+ php_free_objid_query (objid_query , oid_ht , value_str , value_ht , st );
812
827
return false;
813
828
}
814
829
}
@@ -1285,12 +1300,12 @@ static void php_snmp(INTERNAL_FUNCTION_PARAMETERS, int st, int version)
1285
1300
1286
1301
if (session_less_mode ) {
1287
1302
if (!netsnmp_session_init (& session , version , a1 , a2 , timeout , retries )) {
1288
- php_free_objid_query (& objid_query , oid_str , value_str , value_ht , st );
1303
+ php_free_objid_query (& objid_query , oid_ht , value_str , value_ht , st );
1289
1304
netsnmp_session_free (& session );
1290
1305
RETURN_FALSE ;
1291
1306
}
1292
1307
if (version == SNMP_VERSION_3 && !netsnmp_session_set_security (session , a3 , a4 , a5 , a6 , a7 , NULL , NULL )) {
1293
- php_free_objid_query (& objid_query , oid_str , value_str , value_ht , st );
1308
+ php_free_objid_query (& objid_query , oid_ht , value_str , value_ht , st );
1294
1309
netsnmp_session_free (& session );
1295
1310
/* Warning message sent already, just bail out */
1296
1311
RETURN_FALSE ;
@@ -1301,7 +1316,7 @@ static void php_snmp(INTERNAL_FUNCTION_PARAMETERS, int st, int version)
1301
1316
session = snmp_object -> session ;
1302
1317
if (!session ) {
1303
1318
zend_throw_error (NULL , "Invalid or uninitialized SNMP object" );
1304
- php_free_objid_query (& objid_query , oid_str , value_str , value_ht , st );
1319
+ php_free_objid_query (& objid_query , oid_ht , value_str , value_ht , st );
1305
1320
RETURN_THROWS ();
1306
1321
}
1307
1322
@@ -1327,15 +1342,15 @@ static void php_snmp(INTERNAL_FUNCTION_PARAMETERS, int st, int version)
1327
1342
1328
1343
php_snmp_internal (INTERNAL_FUNCTION_PARAM_PASSTHRU , st , session , & objid_query );
1329
1344
1345
+ php_free_objid_query (& objid_query , oid_ht , value_str , value_ht , st );
1346
+
1330
1347
if (session_less_mode ) {
1331
1348
netsnmp_session_free (& session );
1332
1349
} else {
1333
1350
netsnmp_ds_set_boolean (NETSNMP_DS_LIBRARY_ID , NETSNMP_DS_LIB_PRINT_NUMERIC_ENUM , glob_snmp_object .enum_print );
1334
1351
netsnmp_ds_set_boolean (NETSNMP_DS_LIBRARY_ID , NETSNMP_DS_LIB_QUICK_PRINT , glob_snmp_object .quick_print );
1335
1352
netsnmp_ds_set_int (NETSNMP_DS_LIBRARY_ID , NETSNMP_DS_LIB_OID_OUTPUT_FORMAT , glob_snmp_object .oid_output_format );
1336
1353
}
1337
-
1338
- php_free_objid_query (& objid_query , oid_str , value_str , value_ht , st );
1339
1354
}
1340
1355
/* }}} */
1341
1356
0 commit comments