From 5898e8ef3c1bfb2aaa040bc9b86c6c402015865d Mon Sep 17 00:00:00 2001 From: =?utf8?q?M=C3=A1t=C3=A9=20Kocsis?= Date: Wed, 20 Nov 2019 02:29:18 +0100 Subject: [PATCH] Promote warning to exception in file_get_contents() function --- ext/standard/file.c | 4 ++-- ext/standard/tests/file/file_get_contents_error.phpt | 11 +++++++---- .../tests/file/file_get_contents_error002.phpt | 8 ++++++-- .../file_get_contents_file_put_contents_error.phpt | 11 +++++++---- 4 files changed, 22 insertions(+), 12 deletions(-) diff --git a/ext/standard/file.c b/ext/standard/file.c index c7727a65b8..58bbbaa52c 100644 --- a/ext/standard/file.c +++ b/ext/standard/file.c @@ -543,8 +543,8 @@ PHP_FUNCTION(file_get_contents) ZEND_PARSE_PARAMETERS_END(); if (ZEND_NUM_ARGS() == 5 && maxlen < 0) { - php_error_docref(NULL, E_WARNING, "length must be greater than or equal to zero"); - RETURN_FALSE; + zend_value_error("Length must be greater than or equal to zero"); + return; } context = php_stream_context_from_zval(zcontext, 0); diff --git a/ext/standard/tests/file/file_get_contents_error.phpt b/ext/standard/tests/file/file_get_contents_error.phpt index 9757b3be2a..8304105ccc 100644 --- a/ext/standard/tests/file/file_get_contents_error.phpt +++ b/ext/standard/tests/file/file_get_contents_error.phpt @@ -20,8 +20,12 @@ print( file_get_contents("/no/such/file/or/dir") ); create_files($file_path, 1, "text", 0755, 100, "w", "file", 1, "byte"); $file_handle = fopen($file_path."/file_put_contents_error.tmp", "w"); -echo "\n-- Testing for invalid negative maxlen values --"; -var_dump( file_get_contents($file_path."/file1.tmp", FALSE, $file_handle, 0, -5) ); +echo "\n-- Testing for invalid negative maxlen values --\n"; +try { + file_get_contents($file_path."/file1.tmp", FALSE, $file_handle, 0, -5); +} catch (ValueError $exception) { + echo $exception->getMessage() . "\n"; +} delete_files($file_path, 1); fclose($file_handle); @@ -47,7 +51,6 @@ if(file_exists($file_path."/file_put_contents1.tmp")) { Warning: file_get_contents(/no/such/file/or/dir): failed to open stream: No such file or directory in %s on line %d -- Testing for invalid negative maxlen values -- -Warning: file_get_contents(): length must be greater than or equal to zero in %s on line %d -bool(false) +Length must be greater than or equal to zero *** Done *** diff --git a/ext/standard/tests/file/file_get_contents_error002.phpt b/ext/standard/tests/file/file_get_contents_error002.phpt index 8ca1162672..4c9faf91ea 100644 --- a/ext/standard/tests/file/file_get_contents_error002.phpt +++ b/ext/standard/tests/file/file_get_contents_error002.phpt @@ -7,7 +7,11 @@ file_get_contents() test using negative parameter for length (last parameter) display_errors=false --FILE-- getMessage() . "\n"; +} ?> --EXPECT-- -bool(false) +Length must be greater than or equal to zero diff --git a/ext/standard/tests/file/file_get_contents_file_put_contents_error.phpt b/ext/standard/tests/file/file_get_contents_file_put_contents_error.phpt index a50317e8db..b111ff9f5d 100644 --- a/ext/standard/tests/file/file_get_contents_file_put_contents_error.phpt +++ b/ext/standard/tests/file/file_get_contents_file_put_contents_error.phpt @@ -20,9 +20,13 @@ print( file_get_contents("/no/such/file/or/dir") ); $file_handle = fopen($file_path."/file_put_contents.tmp", "w"); -echo "\n-- Testing for invalid negative maxlen values --"; +echo "\n-- Testing for invalid negative maxlen values --\n"; file_put_contents($file_path."/file_put_contents1.tmp", "Garbage data in the file"); -var_dump( file_get_contents($file_path."/file_put_contents1.tmp", FALSE, NULL, 0, -5) ); +try { + file_get_contents($file_path."/file_put_contents1.tmp", FALSE, NULL, 0, -5); +} catch (ValueError $exception) { + echo $exception->getMessage() . "\n"; +} fclose($file_handle); @@ -43,7 +47,6 @@ unlink($file_path."/file_put_contents1.tmp"); Warning: file_get_contents(/no/such/file/or/dir): failed to open stream: No such file or directory in %s on line %d -- Testing for invalid negative maxlen values -- -Warning: file_get_contents(): length must be greater than or equal to zero in %s on line %d -bool(false) +Length must be greater than or equal to zero *** Done *** -- 2.40.0