From 3d41cb012a536ace5c523f3629cfcc56dabbe4b6 Mon Sep 17 00:00:00 2001 From: Gina Peter Banyard Date: Sun, 30 Mar 2025 17:49:32 +0100 Subject: [PATCH] ext/fileinfo: Use magic_setflags() directly The only way this function returns -1 is if: > magic_setflags() returns -1 on systems that don't support utime(3), or utimes(2) when MAGIC_PRESERVE_ATIME is set. This is extremely unlikely and if this would happen we currently have a return type violation. --- ext/fileinfo/fileinfo.c | 19 +++++++------------ 1 file changed, 7 insertions(+), 12 deletions(-) diff --git a/ext/fileinfo/fileinfo.c b/ext/fileinfo/fileinfo.c index 52172dfbd69..4311afff180 100644 --- a/ext/fileinfo/fileinfo.c +++ b/ext/fileinfo/fileinfo.c @@ -94,14 +94,6 @@ PHP_FILEINFO_API zend_object *finfo_objects_new(zend_class_entry *class_type) } /* }}} */ -#define FINFO_SET_OPTION(magic, options) \ - if (magic_setflags(magic, options) == -1) { \ - php_error_docref(NULL, E_WARNING, "Failed to set option '" ZEND_LONG_FMT "' %d:%s", \ - options, magic_errno(magic), magic_error(magic)); \ - RETURN_FALSE; \ - } -/* }}} */ - /* {{{ PHP_MINIT_FUNCTION */ PHP_MINIT_FUNCTION(finfo) { @@ -276,7 +268,9 @@ PHP_FUNCTION(finfo_set_flags) } FILEINFO_FROM_OBJECT(finfo, self); - FINFO_SET_OPTION(finfo->magic, options) + /* We do not check the return value as it can only ever fail if options contains MAGIC_PRESERVE_ATIME + * and the system neither has utime(3) nor utimes(2). Something incredibly unlikely. */ + magic_setflags(finfo->magic, options); finfo->options = options; RETURN_TRUE; @@ -337,7 +331,9 @@ static void _php_finfo_get_type(INTERNAL_FUNCTION_PARAMETERS, int mode, int mime /* Set options for the current file/buffer. */ if (options) { - FINFO_SET_OPTION(magic, options) + /* We do not check the return value as it can only ever fail if options contains MAGIC_PRESERVE_ATIME + * and the system neither has utime(3) nor utimes(2). Something incredibly unlikely. */ + magic_setflags(magic, options); } switch (mode) { @@ -436,9 +432,8 @@ clean: /* Restore options */ if (options) { - FINFO_SET_OPTION(magic, finfo->options) + magic_setflags(magic, finfo->options); } - return; } /* }}} */