From 4bc4fb0273cb1b2cbdb025558b5c3ae9d76b0394 Mon Sep 17 00:00:00 2001
From: Mathias Brodala <mbrodala@pagemachine.de>
Date: Fri, 18 Aug 2017 14:53:03 +0200
Subject: [PATCH] [TASK] Let GeneralUtility::makeInstance() throw exception on
 unknown class

Change-Id: I9e5a04c3ef0ee8fed53d22df4d6ea472266145e7
Resolves: #82131
Releases: master, 8.7
Reviewed-on: https://review.typo3.org/53731
Reviewed-by: Christian Kuhn <lolli@schwarzbu.ch>
Tested-by: Christian Kuhn <lolli@schwarzbu.ch>
Reviewed-by: Daniel Goerz <ervaude@gmail.com>
Tested-by: TYPO3com <no-reply@typo3.com>
Reviewed-by: Georg Ringer <georg.ringer@gmail.com>
Tested-by: Georg Ringer <georg.ringer@gmail.com>
---
 .../core/Classes/Utility/GeneralUtility.php   |  3 +++
 .../Tests/Unit/Utility/GeneralUtilityTest.php | 25 +++++++++++++++++++
 2 files changed, 28 insertions(+)

diff --git a/typo3/sysext/core/Classes/Utility/GeneralUtility.php b/typo3/sysext/core/Classes/Utility/GeneralUtility.php
index c0109bbc1602..b4d262433837 100644
--- a/typo3/sysext/core/Classes/Utility/GeneralUtility.php
+++ b/typo3/sysext/core/Classes/Utility/GeneralUtility.php
@@ -3487,6 +3487,9 @@ class GeneralUtility
         ) {
             return array_shift(self::$nonSingletonInstances[$finalClassName]);
         }
+        if (!class_exists($finalClassName)) {
+            throw new \InvalidArgumentException('Class "' . $className . '" not found', 1503060454);
+        }
         // Create new instance and call constructor with parameters
         $instance = new $finalClassName(...$constructorArguments);
         // Register new singleton instance
diff --git a/typo3/sysext/core/Tests/Unit/Utility/GeneralUtilityTest.php b/typo3/sysext/core/Tests/Unit/Utility/GeneralUtilityTest.php
index 9b35773d1c23..934b8e034491 100644
--- a/typo3/sysext/core/Tests/Unit/Utility/GeneralUtilityTest.php
+++ b/typo3/sysext/core/Tests/Unit/Utility/GeneralUtilityTest.php
@@ -3771,6 +3771,31 @@ class GeneralUtilityTest extends \TYPO3\TestingFramework\Core\Unit\UnitTestCase
         GeneralUtility::makeInstance(['foo']);
     }
 
+    /**
+     * @test
+     */
+    public function makeInstanceWithUnknownClassThrowsException()
+    {
+        $this->expectException(\InvalidArgumentException::class);
+        $this->expectExceptionCode(1503060454);
+
+        GeneralUtility::makeInstance('UnknownClass' . time());
+    }
+
+    /**
+     * @test
+     */
+    public function makeInstanceWithUnknownClassImplementationThrowsException()
+    {
+        $this->expectException(\InvalidArgumentException::class);
+        $this->expectExceptionCode(1503060454);
+
+        GeneralUtilityFixture::resetFinalClassNameCache();
+        $GLOBALS['TYPO3_CONF_VARS']['SYS']['Objects'][OriginalClassFixture::class] = ['className' => 'UnknownClass' . time()];
+
+        GeneralUtility::makeInstance(OriginalClassFixture::class);
+    }
+
     /**
      * @test
      */
-- 
GitLab