@@ -532,6 +532,37 @@ static int convert_command_to_use_shell(wchar_t **cmdw, size_t cmdw_len)
532
532
}
533
533
#endif
534
534
535
+ static int get_command_from_array (zval * array , char * * * argv , char * * command , int num_elems )
536
+ {
537
+ zval * arg_zv ;
538
+ int i = 0 ;
539
+
540
+ * argv = safe_emalloc (sizeof (char * ), num_elems + 1 , 0 );
541
+
542
+ ZEND_HASH_FOREACH_VAL (Z_ARRVAL_P (array ), arg_zv ) {
543
+ zend_string * arg_str = get_valid_arg_string (arg_zv , i + 1 );
544
+ if (!arg_str ) {
545
+ /* terminate argv with NULL so exit_fail code knows how many entries to free */
546
+ (* argv )[i ] = NULL ;
547
+ return FAILURE ;
548
+ }
549
+
550
+ if (i == 0 ) {
551
+ * command = estrdup (ZSTR_VAL (arg_str ));
552
+ }
553
+
554
+ (* argv )[i ++ ] = estrdup (ZSTR_VAL (arg_str ));
555
+ zend_string_release (arg_str );
556
+ } ZEND_HASH_FOREACH_END ();
557
+
558
+ (* argv )[i ] = NULL ;
559
+
560
+ /* As the array is non-empty, we should have found a command. */
561
+ ZEND_ASSERT (command );
562
+
563
+ return SUCCESS ;
564
+ }
565
+
535
566
static struct php_proc_open_descriptor_item * alloc_descriptor_array (zval * descriptorspec )
536
567
{
537
568
int ndescriptors = zend_hash_num_elements (Z_ARRVAL_P (descriptorspec ));
@@ -755,7 +786,6 @@ PHP_FUNCTION(proc_open)
755
786
memset (& env , 0 , sizeof (env ));
756
787
757
788
if (Z_TYPE_P (command_zv ) == IS_ARRAY ) {
758
- zval * arg_zv ;
759
789
uint32_t num_elems = zend_hash_num_elements (Z_ARRVAL_P (command_zv ));
760
790
if (num_elems == 0 ) {
761
791
zend_argument_value_error (1 , "must have at least one element" );
@@ -769,26 +799,9 @@ PHP_FUNCTION(proc_open)
769
799
RETURN_FALSE ;
770
800
}
771
801
#else
772
- argv = safe_emalloc (sizeof (char * ), num_elems + 1 , 0 );
773
- i = 0 ;
774
- ZEND_HASH_FOREACH_VAL (Z_ARRVAL_P (command_zv ), arg_zv ) {
775
- zend_string * arg_str = get_valid_arg_string (arg_zv , i + 1 );
776
- if (!arg_str ) {
777
- argv [i ] = NULL ;
778
- goto exit_fail ;
779
- }
780
-
781
- if (i == 0 ) {
782
- command = estrdup (ZSTR_VAL (arg_str ));
783
- }
784
-
785
- argv [i ++ ] = estrdup (ZSTR_VAL (arg_str ));
786
- zend_string_release (arg_str );
787
- } ZEND_HASH_FOREACH_END ();
788
- argv [i ] = NULL ;
789
-
790
- /* As the array is non-empty, we should have found a command. */
791
- ZEND_ASSERT (command );
802
+ if (get_command_from_array (command_zv , & argv , & command , num_elems ) == FAILURE ) {
803
+ goto exit_fail ;
804
+ }
792
805
#endif
793
806
} else {
794
807
convert_to_string (command_zv );
0 commit comments