Skip to content

Commit a15dc8d

Browse files
committed
Introduce get_command_from_array helper in proc_open.c
1 parent 1b4ac55 commit a15dc8d

File tree

1 file changed

+34
-21
lines changed

1 file changed

+34
-21
lines changed

ext/standard/proc_open.c

Lines changed: 34 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -532,6 +532,37 @@ static int convert_command_to_use_shell(wchar_t **cmdw, size_t cmdw_len)
532532
}
533533
#endif
534534

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+
535566
static struct php_proc_open_descriptor_item* alloc_descriptor_array(zval *descriptorspec)
536567
{
537568
int ndescriptors = zend_hash_num_elements(Z_ARRVAL_P(descriptorspec));
@@ -753,7 +784,6 @@ PHP_FUNCTION(proc_open)
753784
memset(&env, 0, sizeof(env));
754785

755786
if (Z_TYPE_P(command_zv) == IS_ARRAY) {
756-
zval *arg_zv;
757787
uint32_t num_elems = zend_hash_num_elements(Z_ARRVAL_P(command_zv));
758788
if (num_elems == 0) {
759789
zend_argument_value_error(1, "must have at least one element");
@@ -767,26 +797,9 @@ PHP_FUNCTION(proc_open)
767797
RETURN_FALSE;
768798
}
769799
#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+
}
790803
#endif
791804
} else {
792805
convert_to_string(command_zv);

0 commit comments

Comments
 (0)