Skip to content

Commit 1b4ac55

Browse files
committed
Introduce get_string_parameter helper in proc_open.c
1 parent 8dbf742 commit 1b4ac55

File tree

1 file changed

+18
-29
lines changed

1 file changed

+18
-29
lines changed

ext/standard/proc_open.c

Lines changed: 18 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -538,6 +538,19 @@ static struct php_proc_open_descriptor_item* alloc_descriptor_array(zval *descri
538538
return ecalloc(sizeof(struct php_proc_open_descriptor_item), ndescriptors);
539539
}
540540

541+
static int get_string_parameter(zval **dest, zval *array, int index, char *param_name)
542+
{
543+
if ((*dest = zend_hash_index_find(Z_ARRVAL_P(array), index)) == NULL) {
544+
zend_value_error("Missing %s", param_name);
545+
return FAILURE;
546+
}
547+
if (!try_convert_to_string(*dest)) {
548+
return FAILURE;
549+
}
550+
551+
return SUCCESS;
552+
}
553+
541554
static int set_proc_descriptor_to_blackhole(struct php_proc_open_descriptor_item *desc)
542555
{
543556
#ifdef PHP_WIN32
@@ -831,26 +844,13 @@ PHP_FUNCTION(proc_open)
831844
} else if (Z_TYPE_P(descitem) != IS_ARRAY) {
832845
zend_argument_value_error(2, "must only contain arrays and File-Handles");
833846
goto exit_fail;
834-
} else {
835-
836-
if ((ztype = zend_hash_index_find(Z_ARRVAL_P(descitem), 0)) != NULL) {
837-
if (!try_convert_to_string(ztype)) {
838-
goto exit_fail;
839-
}
840-
} else {
841-
zend_value_error("Missing handle qualifier in array");
847+
} else if (get_string_parameter(&ztype, descitem, 0, "handle qualifier") == FAILURE) {
842848
goto exit_fail;
843-
}
844-
849+
} else {
845850
if (strcmp(Z_STRVAL_P(ztype), "pipe") == 0) {
846851
zval *zmode;
847852

848-
if ((zmode = zend_hash_index_find(Z_ARRVAL_P(descitem), 1)) != NULL) {
849-
if (!try_convert_to_string(zmode)) {
850-
goto exit_fail;
851-
}
852-
} else {
853-
zend_value_error("Missing mode parameter for 'pipe'");
853+
if (get_string_parameter(&zmode, descitem, 1, "mode parameter for 'pipe'") == FAILURE) {
854854
goto exit_fail;
855855
}
856856

@@ -860,21 +860,10 @@ PHP_FUNCTION(proc_open)
860860
} else if (strcmp(Z_STRVAL_P(ztype), "file") == 0) {
861861
zval *zfile, *zmode;
862862

863-
if ((zfile = zend_hash_index_find(Z_ARRVAL_P(descitem), 1)) != NULL) {
864-
if (!try_convert_to_string(zfile)) {
865-
goto exit_fail;
866-
}
867-
} else {
868-
zend_value_error("Missing file name parameter for 'file'");
863+
if (get_string_parameter(&zfile, descitem, 1, "file name parameter for 'file'") == FAILURE) {
869864
goto exit_fail;
870865
}
871-
872-
if ((zmode = zend_hash_index_find(Z_ARRVAL_P(descitem), 2)) != NULL) {
873-
if (!try_convert_to_string(zmode)) {
874-
goto exit_fail;
875-
}
876-
} else {
877-
zend_value_error("Missing mode parameter for 'file'");
866+
if (get_string_parameter(&zmode, descitem, 2, "mode parameter for 'file'") == FAILURE) {
878867
goto exit_fail;
879868
}
880869

0 commit comments

Comments
 (0)