@@ -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 char * get_command_from_array (zval * array , char * * * argv , int num_elems )
536
+ {
537
+ zval * arg_zv ;
538
+ char * command = NULL ;
539
+ int i = 0 ;
540
+
541
+ * argv = safe_emalloc (sizeof (char * ), num_elems + 1 , 0 );
542
+
543
+ ZEND_HASH_FOREACH_VAL (Z_ARRVAL_P (array ), arg_zv ) {
544
+ zend_string * arg_str = get_valid_arg_string (arg_zv , i + 1 );
545
+ if (!arg_str ) {
546
+ /* terminate argv with NULL so exit_fail code knows how many entries to free */
547
+ (* argv )[i ] = NULL ;
548
+ if (command != NULL ) {
549
+ efree (command );
550
+ }
551
+ return NULL ;
552
+ }
553
+
554
+ if (i == 0 ) {
555
+ command = estrdup (ZSTR_VAL (arg_str ));
556
+ }
557
+
558
+ (* argv )[i ++ ] = estrdup (ZSTR_VAL (arg_str ));
559
+ zend_string_release (arg_str );
560
+ } ZEND_HASH_FOREACH_END ();
561
+
562
+ (* argv )[i ] = NULL ;
563
+ return command ;
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 ));
@@ -753,7 +784,6 @@ PHP_FUNCTION(proc_open)
753
784
memset (& env , 0 , sizeof (env ));
754
785
755
786
if (Z_TYPE_P (command_zv ) == IS_ARRAY ) {
756
- zval * arg_zv ;
757
787
uint32_t num_elems = zend_hash_num_elements (Z_ARRVAL_P (command_zv ));
758
788
if (num_elems == 0 ) {
759
789
zend_argument_value_error (1 , "must have at least one element" );
@@ -767,26 +797,9 @@ PHP_FUNCTION(proc_open)
767
797
RETURN_FALSE ;
768
798
}
769
799
#else
770
- argv = safe_emalloc (sizeof (char * ), num_elems + 1 , 0 );
771
- i = 0 ;
772
- ZEND_HASH_FOREACH_VAL (Z_ARRVAL_P (command_zv ), arg_zv ) {
773
- zend_string * arg_str = get_valid_arg_string (arg_zv , i + 1 );
774
- if (!arg_str ) {
775
- argv [i ] = NULL ;
776
- goto exit_fail ;
777
- }
778
-
779
- if (i == 0 ) {
780
- command = estrdup (ZSTR_VAL (arg_str ));
781
- }
782
-
783
- argv [i ++ ] = estrdup (ZSTR_VAL (arg_str ));
784
- zend_string_release (arg_str );
785
- } ZEND_HASH_FOREACH_END ();
786
- argv [i ] = NULL ;
787
-
788
- /* As the array is non-empty, we should have found a command. */
789
- ZEND_ASSERT (command );
800
+ if ((command = get_command_from_array (command_zv , & argv , num_elems )) == NULL ) {
801
+ goto exit_fail ;
802
+ }
790
803
#endif
791
804
} else {
792
805
convert_to_string (command_zv );
0 commit comments