From 5430aa938d1519091b9894e4fb8be0008e5548b0 Mon Sep 17 00:00:00 2001
From: Helmut Hummel <typo3@helhum.io>
Date: Fri, 19 Jun 2020 12:26:52 +0200
Subject: [PATCH] [BUGFIX] Avoid fatal error in image rendering when processing
 fails

Fatal error can occur, when the file variable isn't a string,
which only happens, when file processing failed or the resolved
file isn't an image.

Nevertheless the fatal error should be avoided.

Also clean up some variable usage to not change the type
of variable and remove some not needed isset() check
for better readability.

Resolves: #91681
Releases: master, 10.4
Change-Id: Ibde8e1105fa7c0e233656b5dd49e083b6c7259ab
Reviewed-on: https://review.typo3.org/c/Packages/TYPO3.CMS/+/64908
Reviewed-by: Oliver Klee <typo3-coding@oliverklee.de>
Reviewed-by: Georg Ringer <georg.ringer@gmail.com>
Reviewed-by: Oliver Bartsch <bo@cedev.de>
Reviewed-by: Helmut Hummel <typo3@helhum.io>
Tested-by: core-ci <typo3@b13.com>
Tested-by: Georg Ringer <georg.ringer@gmail.com>
Tested-by: Oliver Bartsch <bo@cedev.de>
Tested-by: Helmut Hummel <typo3@helhum.io>
---
 .../ContentObject/ContentObjectRenderer.php    | 18 +++++++++---------
 1 file changed, 9 insertions(+), 9 deletions(-)

diff --git a/typo3/sysext/frontend/Classes/ContentObject/ContentObjectRenderer.php b/typo3/sysext/frontend/Classes/ContentObject/ContentObjectRenderer.php
index e7c9109f674c..a2895e5d15e9 100644
--- a/typo3/sysext/frontend/Classes/ContentObject/ContentObjectRenderer.php
+++ b/typo3/sysext/frontend/Classes/ContentObject/ContentObjectRenderer.php
@@ -4076,15 +4076,15 @@ class ContentObjectRenderer implements LoggerAwareInterface
                     if (MathUtility::canBeInterpretedAsInteger($file)) {
                         $treatIdAsReference = $this->stdWrapValue('treatIdAsReference', $fileArray ?? []);
                         if (!empty($treatIdAsReference)) {
-                            $file = $this->getResourceFactory()->getFileReferenceObject($file);
-                            $fileObject = $file->getOriginalFile();
+                            $fileReference = $this->getResourceFactory()->getFileReferenceObject($file);
+                            $fileObject = $fileReference->getOriginalFile();
                         } else {
                             $fileObject = $this->getResourceFactory()->getFileObject($file);
                         }
                     } elseif (preg_match('/^(0|[1-9][0-9]*):/', $file)) { // combined identifier
                         $fileObject = $this->getResourceFactory()->retrieveFileOrFolderObject($file);
                     } else {
-                        if (isset($importedFile) && !empty($importedFile) && !empty($fileArray['import'])) {
+                        if ($importedFile && !empty($fileArray['import'])) {
                             $file = $fileArray['import'] . $file;
                         }
                         $fileObject = $this->getResourceFactory()->retrieveFileOrFolderObject($file);
@@ -4109,8 +4109,8 @@ class ContentObjectRenderer implements LoggerAwareInterface
                 $processingConfiguration['noScale'] = $this->stdWrapValue('noScale', $fileArray ?? []);
                 $processingConfiguration['additionalParameters'] = $this->stdWrapValue('params', $fileArray ?? []);
                 $processingConfiguration['frame'] = (int)$this->stdWrapValue('frame', $fileArray ?? []);
-                if ($file instanceof FileReference) {
-                    $processingConfiguration['crop'] = $this->getCropAreaFromFileReference($file, $fileArray);
+                if (isset($fileReference)) {
+                    $processingConfiguration['crop'] = $this->getCropAreaFromFileReference($fileReference, $fileArray);
                 } else {
                     $processingConfiguration['crop'] = $this->getCropAreaFromFromTypoScriptSettings($fileObject, $fileArray);
                 }
@@ -4154,11 +4154,11 @@ class ContentObjectRenderer implements LoggerAwareInterface
                 }
             }
         }
-        // If image was processed by GIFBUILDER:
-        // ($imageResource indicates that it was processed the regular way)
-        if (!isset($imageResource)) {
+        // Triggered when the resolved file object isn't considered as image, processing failed and likely other scenarios
+        // This code path dates back to pre FAL times and should be deprecated and removed eventually
+        if (!isset($imageResource) && is_string($file)) {
             try {
-                $theImage = GeneralUtility::makeInstance(FilePathSanitizer::class)->sanitize((string)$file);
+                $theImage = GeneralUtility::makeInstance(FilePathSanitizer::class)->sanitize($file);
                 $info = GeneralUtility::makeInstance(GifBuilder::class)->imageMagickConvert($theImage, 'WEB');
                 $info['origFile'] = $theImage;
                 // This is needed by \TYPO3\CMS\Frontend\Imaging\GifBuilder, ln 100ff in order for the setup-array to create a unique filename hash.
-- 
GitLab