From 17598d503d3eb2c8aa36338fba2d59e9584ee38d Mon Sep 17 00:00:00 2001 From: =?utf8?q?M=C3=A1t=C3=A9=20Kocsis?= Date: Wed, 20 Nov 2019 02:30:48 +0100 Subject: [PATCH] Promote warnings to exceptions in proc_open() function GH-5004 --- ext/standard/proc_open.c | 20 ++++++------- .../general_functions/proc_open_array.phpt | 11 ++++--- .../general_functions/proc_open_pipes3.phpt | 9 ++++-- .../general_functions/proc_open_redirect.phpt | 30 ++++++++++++------- 4 files changed, 43 insertions(+), 27 deletions(-) diff --git a/ext/standard/proc_open.c b/ext/standard/proc_open.c index 4906b3f828..85bd2c9f57 100644 --- a/ext/standard/proc_open.c +++ b/ext/standard/proc_open.c @@ -529,8 +529,8 @@ PHP_FUNCTION(proc_open) zval *arg_zv; uint32_t num_elems = zend_hash_num_elements(Z_ARRVAL_P(command_zv)); if (num_elems == 0) { - php_error_docref(NULL, E_WARNING, "Command array must have at least one element"); - RETURN_FALSE; + zend_value_error("Command array must have at least one element"); + return; } #ifdef PHP_WIN32 @@ -622,7 +622,7 @@ PHP_FUNCTION(proc_open) zval *ztype; if (str_index) { - php_error_docref(NULL, E_WARNING, "descriptor spec must be an integer indexed array"); + zend_value_error("Descriptor spec must be an integer indexed array"); goto exit_fail; } @@ -655,7 +655,7 @@ PHP_FUNCTION(proc_open) descriptors[ndesc].mode = DESC_FILE; } else if (Z_TYPE_P(descitem) != IS_ARRAY) { - php_error_docref(NULL, E_WARNING, "Descriptor item must be either an array or a File-Handle"); + zend_value_error("Descriptor item must be either an array or a File-Handle"); goto exit_fail; } else { @@ -664,7 +664,7 @@ PHP_FUNCTION(proc_open) goto exit_fail; } } else { - php_error_docref(NULL, E_WARNING, "Missing handle qualifier in array"); + zend_value_error("Missing handle qualifier in array"); goto exit_fail; } @@ -677,7 +677,7 @@ PHP_FUNCTION(proc_open) goto exit_fail; } } else { - php_error_docref(NULL, E_WARNING, "Missing mode parameter for 'pipe'"); + zend_value_error("Missing mode parameter for 'pipe'"); goto exit_fail; } @@ -718,7 +718,7 @@ PHP_FUNCTION(proc_open) goto exit_fail; } } else { - php_error_docref(NULL, E_WARNING, "Missing file name parameter for 'file'"); + zend_value_error("Missing file name parameter for 'file'"); goto exit_fail; } @@ -727,7 +727,7 @@ PHP_FUNCTION(proc_open) goto exit_fail; } } else { - php_error_docref(NULL, E_WARNING, "Missing mode parameter for 'file'"); + zend_value_error("Missing mode parameter for 'file'"); goto exit_fail; } @@ -760,11 +760,11 @@ PHP_FUNCTION(proc_open) php_file_descriptor_t childend; if (!ztarget) { - php_error_docref(NULL, E_WARNING, "Missing redirection target"); + zend_value_error("Missing redirection target"); goto exit_fail; } if (Z_TYPE_P(ztarget) != IS_LONG) { - php_error_docref(NULL, E_WARNING, "Redirection target must be an integer"); + zend_value_error("Redirection target must be an integer"); goto exit_fail; } diff --git a/ext/standard/tests/general_functions/proc_open_array.phpt b/ext/standard/tests/general_functions/proc_open_array.phpt index 8b42a55bac..99e1cc56cb 100644 --- a/ext/standard/tests/general_functions/proc_open_array.phpt +++ b/ext/standard/tests/general_functions/proc_open_array.phpt @@ -10,8 +10,12 @@ $ds = [ 2 => ['pipe', 'w'], ]; -echo "Empty command array:"; -var_dump(proc_open([], $ds, $pipes)); +echo "Empty command array:\n"; +try { + proc_open([], $ds, $pipes); +} catch (ValueError $exception) { + echo $exception->getMessage() . "\n"; +} echo "\nNul byte in program name:"; var_dump(proc_open(["php\0oops"], $ds, $pipes)); @@ -56,8 +60,7 @@ proc_close($proc); ?> --EXPECTF-- Empty command array: -Warning: proc_open(): Command array must have at least one element in %s on line %d -bool(false) +Command array must have at least one element Nul byte in program name: Warning: proc_open(): Command array element 1 contains a null byte in %s on line %d diff --git a/ext/standard/tests/general_functions/proc_open_pipes3.phpt b/ext/standard/tests/general_functions/proc_open_pipes3.phpt index 7671b1dce1..b83a0e1a07 100644 --- a/ext/standard/tests/general_functions/proc_open_pipes3.phpt +++ b/ext/standard/tests/general_functions/proc_open_pipes3.phpt @@ -14,7 +14,11 @@ $spec[$i] = array('pi'); proc_open("$php -n $callee", $spec, $pipes); $spec[$i] = 1; -proc_open("$php -n $callee", $spec, $pipes); +try { + proc_open("$php -n $callee", $spec, $pipes); +} catch (ValueError $exception) { + echo $exception->getMessage() . "\n"; +} $spec[$i] = array('pipe', "test"); proc_open("$php -n $callee", $spec, $pipes); @@ -28,8 +32,7 @@ echo "END\n"; ?> --EXPECTF-- Warning: proc_open(): pi is not a valid descriptor spec/mode in %s on line %d - -Warning: proc_open(): Descriptor item must be either an array or a File-Handle in %s on line %d +Descriptor item must be either an array or a File-Handle array(4) { [3]=> resource(%d) of type (Unknown) diff --git a/ext/standard/tests/general_functions/proc_open_redirect.phpt b/ext/standard/tests/general_functions/proc_open_redirect.phpt index 92da696ee9..bf0412c0a2 100644 --- a/ext/standard/tests/general_functions/proc_open_redirect.phpt +++ b/ext/standard/tests/general_functions/proc_open_redirect.phpt @@ -4,9 +4,23 @@ Redirection support in proc_open getMessage() . "\n"; +} + +try { + proc_open([$php], [['redirect', 'foo']], $pipes); +} catch (ValueError $exception) { + echo $exception->getMessage() . "\n"; +} + +try { + proc_open([$php], [['redirect', 42]], $pipes); +} catch (ValueError $exception) { + echo $exception->getMessage() . "\n"; +} echo "\nWith pipe:\n"; $cmd = [$php, '-r', 'echo "Test\n"; fprintf(STDERR, "Error");']; @@ -38,14 +52,10 @@ proc_close($proc); ?> --EXPECTF-- -Warning: proc_open(): Missing redirection target in %s on line %d -bool(false) - -Warning: proc_open(): Redirection target must be an integer in %s on line %d -bool(false) +Missing redirection target +Redirection target must be an integer -Warning: proc_open(): Redirection target 42 not found in %s on line %d -bool(false) +Warning: proc_open(): Redirection target 42 not found in %s With pipe: array(1) { -- 2.50.1