Skip to content

Commit 7142bf8

Browse files
committed
Introduce get_command_from_array helper in proc_open.c
1 parent 9d3df23 commit 7142bf8

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 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+
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));
@@ -755,7 +786,6 @@ PHP_FUNCTION(proc_open)
755786
memset(&env, 0, sizeof(env));
756787

757788
if (Z_TYPE_P(command_zv) == IS_ARRAY) {
758-
zval *arg_zv;
759789
uint32_t num_elems = zend_hash_num_elements(Z_ARRVAL_P(command_zv));
760790
if (num_elems == 0) {
761791
zend_argument_value_error(1, "must have at least one element");
@@ -769,26 +799,9 @@ PHP_FUNCTION(proc_open)
769799
RETURN_FALSE;
770800
}
771801
#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+
}
792805
#endif
793806
} else {
794807
convert_to_string(command_zv);

0 commit comments

Comments
 (0)