]> granicus.if.org Git - php/commitdiff
Promote warnings to exceptions in ext/phar
authorMáté Kocsis <kocsismate@woohoolabs.com>
Tue, 18 Aug 2020 11:38:18 +0000 (13:38 +0200)
committerMáté Kocsis <kocsismate@woohoolabs.com>
Tue, 25 Aug 2020 09:57:37 +0000 (11:57 +0200)
Closes GH-6008

ext/phar/func_interceptors.c
ext/phar/phar_object.c
ext/phar/phar_object.stub.php
ext/phar/phar_object_arginfo.h
ext/phar/tests/fgc_edgecases.phpt
ext/phar/tests/tar/phar_setdefaultstub.phpt
ext/phar/tests/zip/phar_setdefaultstub.phpt

index b3eea43ec8acef5698087ea9dda462b2a90e36b7..f7cfe2249f77714309c05542373be900e1e21032 100644 (file)
@@ -97,7 +97,8 @@ PHAR_FUNC(phar_file_get_contents) /* {{{ */
        zend_bool use_include_path = 0;
        php_stream *stream;
        zend_long offset = -1;
-       zend_long maxlen = PHP_STREAM_COPY_ALL;
+       zend_long maxlen;
+       zend_bool maxlen_is_null = 1;
        zval *zcontext = NULL;
 
        if (!PHAR_G(intercepted)) {
@@ -110,10 +111,14 @@ PHAR_FUNC(phar_file_get_contents) /* {{{ */
        }
 
        /* Parse arguments */
-       if (zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, ZEND_NUM_ARGS(), "p|br!ll", &filename, &filename_len, &use_include_path, &zcontext, &offset, &maxlen) == FAILURE) {
+       if (zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, ZEND_NUM_ARGS(), "p|br!ll!", &filename, &filename_len, &use_include_path, &zcontext, &offset, &maxlen, &maxlen_is_null) == FAILURE) {
                goto skip_phar;
        }
 
+       if (maxlen_is_null) {
+               maxlen = (ssize_t) PHP_STREAM_COPY_ALL;
+       }
+
        if (use_include_path || (!IS_ABSOLUTE_PATH(filename, filename_len) && !strstr(filename, "://"))) {
                char *arch, *entry, *fname;
                zend_string *entry_str = NULL;
@@ -135,10 +140,10 @@ PHAR_FUNC(phar_file_get_contents) /* {{{ */
                        /* fopen within phar, if :// is not in the url, then prepend phar://<archive>/ */
                        entry_len = filename_len;
 
-                       if (ZEND_NUM_ARGS() == 5 && maxlen < 0) {
+                       if (!maxlen_is_null && maxlen < 0) {
                                efree(arch);
-                               php_error_docref(NULL, E_WARNING, "Length must be greater than or equal to zero");
-                               RETURN_FALSE;
+                               zend_argument_value_error(5, "must be greater than or equal to 0");
+                               RETURN_THROWS();
                        }
 
                        /* retrieving a file defaults to within the current directory, so use this if possible */
index 97be3e900c1176bbb6c9bf39b65c9493cfafd428..efb7048cf6ab57254c95bc1f42495717b8e5dd5a 100644 (file)
@@ -2918,7 +2918,7 @@ PHP_METHOD(Phar, setDefaultStub)
        size_t index_len = 0, webindex_len = 0;
        int created_stub = 0;
 
-       if (zend_parse_parameters(ZEND_NUM_ARGS(), "|s!s", &index, &index_len, &webindex, &webindex_len) == FAILURE) {
+       if (zend_parse_parameters(ZEND_NUM_ARGS(), "|s!s!", &index, &index_len, &webindex, &webindex_len) == FAILURE) {
                RETURN_THROWS();
        }
 
@@ -2935,9 +2935,9 @@ PHP_METHOD(Phar, setDefaultStub)
                RETURN_THROWS();
        }
 
-       if (ZEND_NUM_ARGS() > 0 && (phar_obj->archive->is_tar || phar_obj->archive->is_zip)) {
-               php_error_docref(NULL, E_WARNING, "Method accepts no arguments for a tar- or zip-based phar stub, %d given", ZEND_NUM_ARGS());
-               RETURN_FALSE;
+       if ((index || webindex) && (phar_obj->archive->is_tar || phar_obj->archive->is_zip)) {
+               zend_argument_value_error(index ? 1 : 2, "must be null for a tar- or zip-based phar stub, string given");
+               RETURN_THROWS();
        }
 
        if (PHAR_G(readonly)) {
index 7f0eca1034f8ff05b1dceed7e06e6703f8e60ecc..d9b69f784f4d2802ae3868d55a55fcb2889de2b7 100644 (file)
@@ -125,7 +125,7 @@ class Phar extends RecursiveDirectoryIterator implements Countable, ArrayAccess
     public function setAlias(string $alias) {}
 
     /** @return bool */
-    public function setDefaultStub(?string $index = null, string $webindex = UNKNOWN) {}
+    public function setDefaultStub(?string $index = null, ?string $webindex = null) {}
 
     /**
      * @param mixed $metadata
@@ -398,7 +398,7 @@ class PharData extends RecursiveDirectoryIterator implements Countable, ArrayAcc
      * @return bool
      * @alias Phar::setDefaultStub
      */
-    public function setDefaultStub(?string $index = null, string $webindex = UNKNOWN) {}
+    public function setDefaultStub(?string $index = null, ?string $webindex = null) {}
 
     /**
      * @param mixed $metadata
index 67b5ba558029f1258832ddc27f2e38b1d1e1f011..95799bd48782282e7790a7bf54e1a58052acf0c2 100644 (file)
@@ -1,5 +1,5 @@
 /* This is a generated file, edit the .stub.php file instead.
- * Stub hash: f25efd47b496a7d06a30c77911a565a49e383bce */
+ * Stub hash: be0f8bcd0ef8fdac59700160dff7d0beb210aa48 */
 
 ZEND_BEGIN_ARG_INFO_EX(arginfo_class_Phar___construct, 0, 0, 1)
        ZEND_ARG_TYPE_INFO(0, filename, IS_STRING, 0)
@@ -125,7 +125,7 @@ ZEND_END_ARG_INFO()
 
 ZEND_BEGIN_ARG_INFO_EX(arginfo_class_Phar_setDefaultStub, 0, 0, 0)
        ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, index, IS_STRING, 1, "null")
-       ZEND_ARG_TYPE_INFO(0, webindex, IS_STRING, 0)
+       ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, webindex, IS_STRING, 1, "null")
 ZEND_END_ARG_INFO()
 
 ZEND_BEGIN_ARG_INFO_EX(arginfo_class_Phar_setMetadata, 0, 0, 1)
index a902b669c9aa782327e251b2f068df34937c4a4d..38ff03cce8b0e1a0ce889d75869c36e32ae209ca 100644 (file)
@@ -29,7 +29,11 @@ mkdir($pname . '/oops');
 file_put_contents($pname . '/foo/hi', '<?php
 echo file_get_contents("foo/" . basename(__FILE__));
 $context = stream_context_create();
-file_get_contents("./hi", 0, $context, 0, -1);
+try {
+    file_get_contents("./hi", 0, $context, 0, -1);
+} catch (ValueError $exception) {
+    echo $exception->getMessage() . "\n";
+}
 echo file_get_contents("fgc_edgecases.txt");
 set_include_path("' . addslashes(__DIR__) . '");
 echo file_get_contents("fgc_edgecases.txt", true);
@@ -53,7 +57,11 @@ blah
 <?php
 echo file_get_contents("foo/" . basename(__FILE__));
 $context = stream_context_create();
-file_get_contents("./hi", 0, $context, 0, -1);
+try {
+    file_get_contents("./hi", 0, $context, 0, -1);
+} catch (ValueError $exception) {
+    echo $exception->getMessage() . "\n";
+}
 echo file_get_contents("fgc_edgecases.txt");
 set_include_path("%stests");
 echo file_get_contents("fgc_edgecases.txt", true);
@@ -63,14 +71,17 @@ echo file_get_contents("./hi", 0, $context, 50000);
 echo file_get_contents("./hi");
 echo file_get_contents("./hi", 0, $context, 0, 0);
 ?>
-
-Warning: file_get_contents(): Length must be greater than or equal to zero in phar://%sfgc_edgecases.phar.php/foo/hi on line %d
+file_get_contents(): Argument #5 ($maxlen) must be greater than or equal to 0
 test
 test
 <?php
 echo file_get_contents("foo/" . basename(__FILE__));
 $context = stream_context_create();
-file_get_contents("./hi", 0, $context, 0, -1);
+try {
+    file_get_contents("./hi", 0, $context, 0, -1);
+} catch (ValueError $exception) {
+    echo $exception->getMessage() . "\n";
+}
 echo file_get_contents("fgc_edgecases.txt");
 set_include_path("%stests");
 echo file_get_contents("fgc_edgecases.txt", true);
@@ -87,7 +98,11 @@ Warning: file_get_contents(): Failed to seek to position 50000 in the stream in
 <?php
 echo file_get_contents("foo/" . basename(__FILE__));
 $context = stream_context_create();
-file_get_contents("./hi", 0, $context, 0, -1);
+try {
+    file_get_contents("./hi", 0, $context, 0, -1);
+} catch (ValueError $exception) {
+    echo $exception->getMessage() . "\n";
+}
 echo file_get_contents("fgc_edgecases.txt");
 set_include_path("%stests");
 echo file_get_contents("fgc_edgecases.txt", true);
index 6811c4c59dd26004f2786115db08989fd457e2c2..7404a5851eb89976f35a01b02526c2d69812aa0d 100644 (file)
@@ -33,18 +33,28 @@ echo "==========================================================================
 
 try {
     $phar->setDefaultStub('my/custom/thingy.php');
+} catch(ValueError $e) {
+    echo $e->getMessage(). "\n";
+}
+
+try {
     $phar->stopBuffering();
 } catch(Exception $e) {
     echo $e->getMessage(). "\n";
 }
-
 var_dump($phar->getStub());
 
 echo "============================================================================\n";
 echo "============================================================================\n";
 
+
 try {
     $phar->setDefaultStub('my/custom/thingy.php', 'the/web.php');
+} catch(ValueError $e) {
+    echo $e->getMessage(). "\n";
+}
+
+try {
     $phar->stopBuffering();
 } catch(Exception $e) {
     echo $e->getMessage(). "\n";
@@ -57,7 +67,7 @@ var_dump($phar->getStub());
 <?php
 unlink(__DIR__ . '/' . basename(__FILE__, '.clean.php') . '.phar.tar');
 ?>
---EXPECTF--
+--EXPECT--
 string(51) "<?php echo "Hello World\n"; __HALT_COMPILER(); ?>
 "
 ============================================================================
@@ -66,13 +76,11 @@ string(60) "<?php // tar-based phar archive stub file
 __HALT_COMPILER();"
 ============================================================================
 ============================================================================
-
-Warning: Phar::setDefaultStub(): Method accepts no arguments for a tar- or zip-based phar stub, 1 given in %sphar_setdefaultstub.php on line %d
+Phar::setDefaultStub(): Argument #1 ($index) must be null for a tar- or zip-based phar stub, string given
 string(60) "<?php // tar-based phar archive stub file
 __HALT_COMPILER();"
 ============================================================================
 ============================================================================
-
-Warning: Phar::setDefaultStub(): Method accepts no arguments for a tar- or zip-based phar stub, 2 given in %sphar_setdefaultstub.php on line %d
+Phar::setDefaultStub(): Argument #1 ($index) must be null for a tar- or zip-based phar stub, string given
 string(60) "<?php // tar-based phar archive stub file
 __HALT_COMPILER();"
index 9b5f3959574bc122e211ae9d27439cbb4a45cdf9..edbea6f2269c0516b445838ce28628d1308608bb 100644 (file)
@@ -33,6 +33,11 @@ echo "==========================================================================
 
 try {
     $phar->setDefaultStub('my/custom/thingy.php');
+} catch(Error $e) {
+    echo $e->getMessage(). "\n";
+}
+
+try {
     $phar->stopBuffering();
 } catch(Exception $e) {
     echo $e->getMessage(). "\n";
@@ -45,6 +50,11 @@ echo "==========================================================================
 
 try {
     $phar->setDefaultStub('my/custom/thingy.php', 'the/web.php');
+} catch(ValueError $e) {
+    echo $e->getMessage(). "\n";
+}
+
+try {
     $phar->stopBuffering();
 } catch(Exception $e) {
     echo $e->getMessage(). "\n";
@@ -57,7 +67,7 @@ var_dump($phar->getStub());
 <?php
 unlink(__DIR__ . '/' . basename(__FILE__, '.clean.php') . '.phar.zip');
 ?>
---EXPECTF--
+--EXPECT--
 string(51) "<?php echo "Hello World\n"; __HALT_COMPILER(); ?>
 "
 ============================================================================
@@ -66,13 +76,11 @@ string(60) "<?php // zip-based phar archive stub file
 __HALT_COMPILER();"
 ============================================================================
 ============================================================================
-
-Warning: Phar::setDefaultStub(): Method accepts no arguments for a tar- or zip-based phar stub, 1 given in %sphar_setdefaultstub.php on line %d
+Phar::setDefaultStub(): Argument #1 ($index) must be null for a tar- or zip-based phar stub, string given
 string(60) "<?php // zip-based phar archive stub file
 __HALT_COMPILER();"
 ============================================================================
 ============================================================================
-
-Warning: Phar::setDefaultStub(): Method accepts no arguments for a tar- or zip-based phar stub, 2 given in %sphar_setdefaultstub.php on line %d
+Phar::setDefaultStub(): Argument #1 ($index) must be null for a tar- or zip-based phar stub, string given
 string(60) "<?php // zip-based phar archive stub file
 __HALT_COMPILER();"