]> granicus.if.org Git - php/commitdiff
Convert file_info resources to objects
authorChristoph M. Becker <cmbecker69@gmx.de>
Fri, 18 Dec 2020 18:10:53 +0000 (19:10 +0100)
committerChristoph M. Becker <cmbecker69@gmx.de>
Sun, 20 Dec 2020 17:19:22 +0000 (18:19 +0100)
Besides our general desire to get rid of the legacy resource types,
this is particularly appealing for fileinfo, because there are already
respective objects.

Closes GH-5987.

13 files changed:
UPGRADING
ext/fileinfo/fileinfo.c
ext/fileinfo/fileinfo.stub.php
ext/fileinfo/fileinfo_arginfo.h
ext/fileinfo/tests/bug61964-mb.phpt
ext/fileinfo/tests/bug61964.phpt
ext/fileinfo/tests/finfo_close_basic.phpt
ext/fileinfo/tests/finfo_close_error.phpt
ext/fileinfo/tests/finfo_open_001.phpt
ext/fileinfo/tests/finfo_open_basic.phpt
ext/fileinfo/tests/finfo_open_error.phpt
ext/fileinfo/tests/finfo_open_variation1.phpt
ext/opcache/Optimizer/zend_func_info.c

index 6cccf69bf0397d5829039ec82e5d9ebb7ec09464..c659515f51b2d4b8f6d24b1306ada7ea42a295b9 100644 (file)
--- a/UPGRADING
+++ b/UPGRADING
@@ -19,6 +19,10 @@ PHP 8.1 UPGRADE NOTES
 1. Backward Incompatible Changes
 ========================================
 
+- Fileinfo:
+  . The fileinfo functions now accept and return, respectively, finfo objects
+    instead of resources.
+
 - MySQLi:
   . mysqli_fetch_fields() and mysqli_fetch_field_direct() will now always return
     zero for max_length. You can compute this information by iterating over the
index add971b6d643cb8fa21151da563773dda9ce2c1b..5670debe4a39d4d655c5eab72576bc21c2289a4e 100644 (file)
@@ -51,22 +51,12 @@ typedef struct _finfo_object {
        zend_object zo;
 } finfo_object;
 
-#define FILEINFO_DECLARE_INIT_OBJECT(object) \
-       zval *object = getThis();
-
 static inline finfo_object *php_finfo_fetch_object(zend_object *obj) {
        return (finfo_object *)((char*)(obj) - XtOffsetOf(finfo_object, zo));
 }
 
 #define Z_FINFO_P(zv) php_finfo_fetch_object(Z_OBJ_P((zv)))
 
-#define FILEINFO_REGISTER_OBJECT(_object, _ptr) \
-{ \
-       finfo_object *obj; \
-    obj = Z_FINFO_P(_object); \
-    obj->ptr = _ptr; \
-}
-
 #define FILEINFO_FROM_OBJECT(finfo, object) \
 { \
        finfo_object *obj = Z_FINFO_P(object); \
@@ -112,20 +102,6 @@ PHP_FILEINFO_API zend_object *finfo_objects_new(zend_class_entry *class_type)
                                options, magic_errno(magic), magic_error(magic)); \
                RETURN_FALSE; \
        }
-
-/* True global resources - no need for thread safety here */
-static int le_fileinfo;
-/* }}} */
-
-void finfo_resource_destructor(zend_resource *rsrc) /* {{{ */
-{
-       if (rsrc->ptr) {
-               php_fileinfo *finfo = (php_fileinfo *) rsrc->ptr;
-               magic_close(finfo->magic);
-               efree(rsrc->ptr);
-               rsrc->ptr = NULL;
-       }
-}
 /* }}} */
 
 /* {{{ PHP_MINIT_FUNCTION */
@@ -144,8 +120,6 @@ PHP_MINIT_FUNCTION(finfo)
        finfo_object_handlers.free_obj = finfo_objects_free;
        finfo_object_handlers.clone_obj = NULL;
 
-       le_fileinfo = zend_register_list_destructors_ex(finfo_resource_destructor, NULL, "file_info", module_number);
-
        REGISTER_LONG_CONSTANT("FILEINFO_NONE",                 MAGIC_NONE, CONST_CS|CONST_PERSISTENT);
        REGISTER_LONG_CONSTANT("FILEINFO_SYMLINK",              MAGIC_SYMLINK, CONST_CS|CONST_PERSISTENT);
        REGISTER_LONG_CONSTANT("FILEINFO_MIME",                 MAGIC_MIME, CONST_CS|CONST_PERSISTENT);
@@ -204,14 +178,14 @@ PHP_MINFO_FUNCTION(fileinfo)
 }
 /* }}} */
 
-/* {{{ Create a new fileinfo resource. */
+/* {{{ Construct a new fileinfo object. */
 PHP_FUNCTION(finfo_open)
 {
        zend_long options = MAGIC_NONE;
        char *file = NULL;
        size_t file_len = 0;
        php_fileinfo *finfo;
-       FILEINFO_DECLARE_INIT_OBJECT(object)
+       zval *object = getThis();
        char resolved_path[MAXPATHLEN];
        zend_error_handling zeh;
 
@@ -287,30 +261,28 @@ PHP_FUNCTION(finfo_open)
        }
 
        if (object) {
+               finfo_object *obj;
                zend_restore_error_handling(&zeh);
-               FILEINFO_REGISTER_OBJECT(object, finfo);
+               obj = Z_FINFO_P(object);
+               obj->ptr = finfo;
        } else {
-               RETURN_RES(zend_register_resource(finfo, le_fileinfo));
+               zend_object *zobj = finfo_objects_new(finfo_class_entry);
+               finfo_object *obj = php_finfo_fetch_object(zobj);
+               obj->ptr = finfo;
+               RETURN_OBJ(zobj);
        }
 }
 /* }}} */
 
-/* {{{ Close fileinfo resource. */
+/* {{{ Close fileinfo object - a NOP. */
 PHP_FUNCTION(finfo_close)
 {
-       php_fileinfo *finfo;
-       zval *zfinfo;
-
-       if (zend_parse_parameters(ZEND_NUM_ARGS(), "r", &zfinfo) == FAILURE) {
-               RETURN_THROWS();
-       }
+       zval *self;
 
-       if ((finfo = (php_fileinfo *)zend_fetch_resource(Z_RES_P(zfinfo), "file_info", le_fileinfo)) == NULL) {
+       if (zend_parse_parameters(ZEND_NUM_ARGS(), "O", &self, finfo_class_entry) == FAILURE) {
                RETURN_THROWS();
        }
 
-       zend_list_close(Z_RES_P(zfinfo));
-
        RETURN_TRUE;
 }
 /* }}} */
@@ -320,22 +292,12 @@ PHP_FUNCTION(finfo_set_flags)
 {
        zend_long options;
        php_fileinfo *finfo;
-       zval *zfinfo;
-       FILEINFO_DECLARE_INIT_OBJECT(object)
+       zval *self;
 
-       if (object) {
-               if (zend_parse_parameters(ZEND_NUM_ARGS(), "l", &options) == FAILURE) {
-                       RETURN_THROWS();
-               }
-               FILEINFO_FROM_OBJECT(finfo, object);
-       } else {
-               if (zend_parse_parameters(ZEND_NUM_ARGS(), "rl", &zfinfo, &options) == FAILURE) {
-                       RETURN_THROWS();
-               }
-               if ((finfo = (php_fileinfo *)zend_fetch_resource(Z_RES_P(zfinfo), "file_info", le_fileinfo)) == NULL) {
-                       RETURN_THROWS();
-               }
+       if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "Ol", &self, finfo_class_entry, &options) == FAILURE) {
+               RETURN_THROWS();
        }
+       FILEINFO_FROM_OBJECT(finfo, self);
 
        FINFO_SET_OPTION(finfo->magic, options)
        finfo->options = options;
@@ -354,12 +316,10 @@ static void _php_finfo_get_type(INTERNAL_FUNCTION_PARAMETERS, int mode, int mime
        char *ret_val = NULL, *buffer = NULL;
        size_t buffer_len;
        php_fileinfo *finfo = NULL;
-       zval *zfinfo, *zcontext = NULL;
+       zval *zcontext = NULL;
        zval *what;
        char mime_directory[] = "directory";
-
        struct magic_set *magic = NULL;
-       FILEINFO_DECLARE_INIT_OBJECT(object)
 
        if (mimetype_emu) {
 
@@ -389,19 +349,12 @@ static void _php_finfo_get_type(INTERNAL_FUNCTION_PARAMETERS, int mode, int mime
                        php_error_docref(NULL, E_WARNING, "Failed to load magic database");
                        goto common;
                }
-       } else if (object) {
-               if (zend_parse_parameters(ZEND_NUM_ARGS(), "s|lr!", &buffer, &buffer_len, &options, &zcontext) == FAILURE) {
-                       RETURN_THROWS();
-               }
-               FILEINFO_FROM_OBJECT(finfo, object);
-               magic = finfo->magic;
        } else {
-               if (zend_parse_parameters(ZEND_NUM_ARGS(), "rs|lr!", &zfinfo, &buffer, &buffer_len, &options, &zcontext) == FAILURE) {
-                       RETURN_THROWS();
-               }
-               if ((finfo = (php_fileinfo *)zend_fetch_resource(Z_RES_P(zfinfo), "file_info", le_fileinfo)) == NULL) {
+               zval *self;
+               if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "Os|lr!", &self, finfo_class_entry, &buffer, &buffer_len, &options, &zcontext) == FAILURE) {
                        RETURN_THROWS();
                }
+               FILEINFO_FROM_OBJECT(finfo, self);
                magic = finfo->magic;
        }
 
index 9355a96a2b192bfad2b20ec5110868f9a743c7ec..5a498b79c1182c6d9c6ceb3ce9c0d11acb0d6faa 100644 (file)
@@ -28,30 +28,21 @@ class finfo
     public function set_flags(int $flags) {}
 }
 
-/** @return resource|false */
-function finfo_open(int $flags = FILEINFO_NONE, string $magic_database = "") {}
+function finfo_open(int $flags = FILEINFO_NONE, string $magic_database = ""): finfo|false {}
 
-/**
- * @param resource $finfo
- */
-function finfo_close($finfo): bool {}
+function finfo_close(finfo $finfo): bool {}
 
-/**
- * @param resource $finfo
- */
-function finfo_set_flags($finfo, int $flags): bool {}
+function finfo_set_flags(finfo $finfo, int $flags): bool {}
 
 /**
- * @param resource $finfo
  * @param resource|null $context
  */
-function finfo_file($finfo, string $filename, int $flags = FILEINFO_NONE, $context = null): string|false {}
+function finfo_file(finfo $finfo, string $filename, int $flags = FILEINFO_NONE, $context = null): string|false {}
 
 /**
- * @param resource $finfo
  * @param resource|null $context
  */
-function finfo_buffer($finfo, string $string, int $flags = FILEINFO_NONE, $context = null): string|false {}
+function finfo_buffer(finfo $finfo, string $string, int $flags = FILEINFO_NONE, $context = null): string|false {}
 
 /**
  * @param resource|string $filename
index 80850a6502e6117819a29f96eebae7efa6c6f4e9..3fe1f28a02ac0cf35fe35b17ea1ca3cc92556ed8 100644 (file)
@@ -1,29 +1,29 @@
 /* This is a generated file, edit the .stub.php file instead.
- * Stub hash: be858509df27550b51d8a7a51a3629eceb6d0aa6 */
+ * Stub hash: 1282a20b1d007bbcc9c0d4efe400db43a5450307 */
 
-ZEND_BEGIN_ARG_INFO_EX(arginfo_finfo_open, 0, 0, 0)
+ZEND_BEGIN_ARG_WITH_RETURN_OBJ_TYPE_MASK_EX(arginfo_finfo_open, 0, 0, finfo, MAY_BE_FALSE)
        ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, flags, IS_LONG, 0, "FILEINFO_NONE")
        ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, magic_database, IS_STRING, 0, "\"\"")
 ZEND_END_ARG_INFO()
 
 ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_finfo_close, 0, 1, _IS_BOOL, 0)
-       ZEND_ARG_INFO(0, finfo)
+       ZEND_ARG_OBJ_INFO(0, finfo, finfo, 0)
 ZEND_END_ARG_INFO()
 
 ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_finfo_set_flags, 0, 2, _IS_BOOL, 0)
-       ZEND_ARG_INFO(0, finfo)
+       ZEND_ARG_OBJ_INFO(0, finfo, finfo, 0)
        ZEND_ARG_TYPE_INFO(0, flags, IS_LONG, 0)
 ZEND_END_ARG_INFO()
 
 ZEND_BEGIN_ARG_WITH_RETURN_TYPE_MASK_EX(arginfo_finfo_file, 0, 2, MAY_BE_STRING|MAY_BE_FALSE)
-       ZEND_ARG_INFO(0, finfo)
+       ZEND_ARG_OBJ_INFO(0, finfo, finfo, 0)
        ZEND_ARG_TYPE_INFO(0, filename, IS_STRING, 0)
        ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, flags, IS_LONG, 0, "FILEINFO_NONE")
        ZEND_ARG_INFO_WITH_DEFAULT_VALUE(0, context, "null")
 ZEND_END_ARG_INFO()
 
 ZEND_BEGIN_ARG_WITH_RETURN_TYPE_MASK_EX(arginfo_finfo_buffer, 0, 2, MAY_BE_STRING|MAY_BE_FALSE)
-       ZEND_ARG_INFO(0, finfo)
+       ZEND_ARG_OBJ_INFO(0, finfo, finfo, 0)
        ZEND_ARG_TYPE_INFO(0, string, IS_STRING, 0)
        ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, flags, IS_LONG, 0, "FILEINFO_NONE")
        ZEND_ARG_INFO_WITH_DEFAULT_VALUE(0, context, "null")
@@ -33,7 +33,10 @@ ZEND_BEGIN_ARG_WITH_RETURN_TYPE_MASK_EX(arginfo_mime_content_type, 0, 1, MAY_BE_
        ZEND_ARG_INFO(0, filename)
 ZEND_END_ARG_INFO()
 
-#define arginfo_class_finfo___construct arginfo_finfo_open
+ZEND_BEGIN_ARG_INFO_EX(arginfo_class_finfo___construct, 0, 0, 0)
+       ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, flags, IS_LONG, 0, "FILEINFO_NONE")
+       ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, magic_database, IS_STRING, 0, "\"\"")
+ZEND_END_ARG_INFO()
 
 ZEND_BEGIN_ARG_INFO_EX(arginfo_class_finfo_file, 0, 0, 1)
        ZEND_ARG_TYPE_INFO(0, filename, IS_STRING, 0)
index 1142ad7eeda29916032764c05fa3ca2f8026f788..c4a6976f2e04f1eee5f0a53a60e62309ec7a6165 100644 (file)
@@ -45,8 +45,10 @@ rmdir($dir);
 ?>
 --EXPECTF--
 bool(false)%A
-resource(%d) of type (file_info)
-resource(%d) of type (file_info)
+object(finfo)#%d (0) {
+}
+object(finfo)#%d (0) {
+}
 bool(false)%A
 Warning: finfo_open(): offset `string' invalid in %sbug61964-mb.php on line %d
 
index ecb12fe75214d852574f1b6fbbe95626575d5231..7bd97a019cef6c86f69e58788a8a977110169489 100644 (file)
@@ -45,8 +45,10 @@ rmdir($dir);
 ?>
 --EXPECTF--
 bool(false)%A
-resource(%d) of type (file_info)
-resource(%d) of type (file_info)
+object(finfo)#%d (0) {
+}
+object(finfo)#%d (0) {
+}
 bool(false)%A
 Warning: finfo_open(): offset `string' invalid in %sbug61964.php on line %d
 
index 1ff05dff21df7f37a5a19d481fc257466e3813b1..f4fe98de6202cafd7b940ad631c2ed41e411a379 100644 (file)
@@ -21,7 +21,8 @@ unset( $finfo );
 ?>
 --EXPECTF--
 *** Testing finfo_close() : basic functionality ***
-resource(%d) of type (file_info)
+object(finfo)#%d (0) {
+}
 bool(true)
 object(finfo)#%d (%d) {
 }
index b42510f1307537c748a5901deb1471398d9c4c30..a32baf845053cd651b6a856f5861676e31cbb69b 100644 (file)
@@ -19,4 +19,4 @@ try {
 *** Testing finfo_close() : error conditions ***
 
 -- Testing finfo_close() function with wrong resource type --
-finfo_close(): supplied resource is not a valid file_info resource
+finfo_close(): Argument #1 ($finfo) must be of type finfo, resource given
index ef36bc21f08e825a5fb7672b1bbb3e41fe740bfe..c7c73c1b5cde9ae5c5f7dad3ae1ce16be96cd24e 100644 (file)
@@ -20,8 +20,10 @@ var_dump(finfo_open(FILEINFO_MIME, '/foo/bar/inexistent'));
 ?>
 --EXPECTF--
 finfo_open(): Argument #2 ($magic_database) must not contain any null bytes
-resource(%d) of type (file_info)
-resource(%d) of type (file_info)
+object(finfo)#%d (0) {
+}
+object(finfo)#%d (0) {
+}
 
 Warning: finfo_open(%s123): Failed to open stream: No such file or directory in %s on line %d
 
index bd1b03c5a38e547136231258a187df8a78beb1ac..bf0a597d7426394e72e22aa0cb5d47bc8a48b5c3 100644 (file)
@@ -25,14 +25,21 @@ var_dump( new finfo() );
 ?>
 --EXPECTF--
 *** Testing finfo_open() : basic functionality ***
-resource(%d) of type (file_info)
-resource(%d) of type (file_info)
-resource(%d) of type (file_info)
-resource(%d) of type (file_info)
-resource(%d) of type (file_info)
-resource(%d) of type (file_info)
-resource(%d) of type (file_info)
-object(finfo)#%d (%d) {
-}
-object(finfo)#%d (%d) {
+object(finfo)#%d (0) {
+}
+object(finfo)#%d (0) {
+}
+object(finfo)#%d (0) {
+}
+object(finfo)#%d (0) {
+}
+object(finfo)#%d (0) {
+}
+object(finfo)#%d (0) {
+}
+object(finfo)#%d (0) {
+}
+object(finfo)#%d (0) {
+}
+object(finfo)#%d (0) {
 }
index 881c26f6648276d266baa8d39551558a96afda14..0d697ec8183478e768e7c34315689bba72e5cb89 100644 (file)
@@ -1,7 +1,7 @@
 --TEST--
 Test finfo_open() function : error functionality
 --SKIPIF--
-<?php require_once(__DIR__ . '/skipif.inc');
+<?php require_once(__DIR__ . '/skipif.inc')?>
 --FILE--
 <?php
 $magicFile = __DIR__ . DIRECTORY_SEPARATOR . 'magic';
@@ -35,6 +35,7 @@ Warning: finfo_open(): Failed to load magic database at "%sfoobarfile" in %sfinf
 bool(false)
 
 Warning: finfo_open(): using regular magic file `%smagic' in %sfinfo_open_error.php on line %d
-resource(6) of type (file_info)
+object(finfo)#%d (0) {
+}
 finfo_open(): Argument #1 ($flags) must be of type int, string given
 finfo::__construct(): Argument #1 ($flags) must be of type int, string given
index 93a3b108a68c20267a46b3dd7c93f913302ac37f..035902fcef5cd3f7c3a8cabfab1b57661869708f 100644 (file)
@@ -16,5 +16,7 @@ var_dump( finfo_open( FILEINFO_DEVICES | FILEINFO_RAW, $magicFile ) );
 ?>
 --EXPECTF--
 *** Testing finfo_open() : variations in opening ***
-resource(%d) of type (file_info)
-resource(%d) of type (file_info)
+object(finfo)#%d (0) {
+}
+object(finfo)#%d (0) {
+}
index e3b4da720c4d17ad77efd6ae4ab115b901f768f7..19ab4dbffd8f1a7daa49eb349ea1cb95e72b8311 100644 (file)
@@ -776,7 +776,7 @@ static const func_info_t func_infos[] = {
 #endif
 
        /* ext/fileinfo */
-       F1("finfo_open",                                                        MAY_BE_FALSE | MAY_BE_RESOURCE),
+       F1("finfo_open",                                                        MAY_BE_FALSE | MAY_BE_OBJECT),
        F1("finfo_file",                                                        MAY_BE_FALSE | MAY_BE_STRING),
        F1("finfo_buffer",                                                      MAY_BE_FALSE | MAY_BE_STRING),
        F1("mime_content_type",                                         MAY_BE_FALSE | MAY_BE_STRING),