Skip to content

Commit 9d3df23

Browse files
committed
Introduce get_string_parameter helper in proc_open.c
1 parent c9f042b commit 9d3df23

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
@@ -540,6 +540,19 @@ static struct php_proc_open_descriptor_item* alloc_descriptor_array(zval *descri
540540
return descriptors;
541541
}
542542

543+
static int get_string_parameter(zval **dest, zval *array, int index, char *param_name)
544+
{
545+
if ((*dest = zend_hash_index_find(Z_ARRVAL_P(array), index)) == NULL) {
546+
zend_value_error("Missing %s", param_name);
547+
return FAILURE;
548+
}
549+
if (!try_convert_to_string(*dest)) {
550+
return FAILURE;
551+
}
552+
553+
return SUCCESS;
554+
}
555+
543556
static int set_proc_descriptor_to_blackhole(struct php_proc_open_descriptor_item *desc)
544557
{
545558
#ifdef PHP_WIN32
@@ -833,26 +846,13 @@ PHP_FUNCTION(proc_open)
833846
} else if (Z_TYPE_P(descitem) != IS_ARRAY) {
834847
zend_argument_value_error(2, "must only contain arrays and File-Handles");
835848
goto exit_fail;
836-
} else {
837-
838-
if ((ztype = zend_hash_index_find(Z_ARRVAL_P(descitem), 0)) != NULL) {
839-
if (!try_convert_to_string(ztype)) {
840-
goto exit_fail;
841-
}
842-
} else {
843-
zend_value_error("Missing handle qualifier in array");
849+
} else if (get_string_parameter(&ztype, descitem, 0, "handle qualifier") == FAILURE) {
844850
goto exit_fail;
845-
}
846-
851+
} else {
847852
if (strcmp(Z_STRVAL_P(ztype), "pipe") == 0) {
848853
zval *zmode;
849854

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

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

865-
if ((zfile = zend_hash_index_find(Z_ARRVAL_P(descitem), 1)) != NULL) {
866-
if (!try_convert_to_string(zfile)) {
867-
goto exit_fail;
868-
}
869-
} else {
870-
zend_value_error("Missing file name parameter for 'file'");
865+
if (get_string_parameter(&zfile, descitem, 1, "file name parameter for 'file'") == FAILURE) {
871866
goto exit_fail;
872867
}
873-
874-
if ((zmode = zend_hash_index_find(Z_ARRVAL_P(descitem), 2)) != NULL) {
875-
if (!try_convert_to_string(zmode)) {
876-
goto exit_fail;
877-
}
878-
} else {
879-
zend_value_error("Missing mode parameter for 'file'");
868+
if (get_string_parameter(&zmode, descitem, 2, "mode parameter for 'file'") == FAILURE) {
880869
goto exit_fail;
881870
}
882871

0 commit comments

Comments
 (0)