[BUGFIX] Avoid overly large regex in substituteMarkerArrayCached 97/45097/2
authorMarkus Klein <markus.klein@typo3.org>
Wed, 2 Dec 2015 20:15:11 +0000 (21:15 +0100)
committerBenni Mack <benni@typo3.org>
Fri, 4 Dec 2015 22:52:09 +0000 (23:52 +0100)
Fetch the actually used markers from the content and only
generate the replace regex for those.
This avoids problems where 1000 markers may be passed in,
but only 10 are actually used.

Resolves: #44270
Releases: master, 6.2
Change-Id: I05f60960949e945249b045a8ae8e8430f7d8f7e6
Reviewed-on: https://review.typo3.org/45097
Reviewed-by: Morton Jonuschat <m.jonuschat@mojocode.de>
Tested-by: Morton Jonuschat <m.jonuschat@mojocode.de>
Reviewed-by: Benni Mack <benni@typo3.org>
Tested-by: Benni Mack <benni@typo3.org>
typo3/sysext/frontend/Classes/ContentObject/ContentObjectRenderer.php

index 3d73128..c89ba30 100644 (file)
@@ -2190,8 +2190,6 @@ class ContentObjectRenderer
                 $this->substMarkerCache[$storeKey] = $storeArr;
                 $timeTracker->setTSlogMessage('Cached from DB', 0);
             } else {
                 $this->substMarkerCache[$storeKey] = $storeArr;
                 $timeTracker->setTSlogMessage('Cached from DB', 0);
             } else {
-                // Initialize storeArr
-                $storeArr = array();
                 // Finding subparts and substituting them with the subpart as a marker
                 foreach ($sPkeys as $sPK) {
                     $content = $this->substituteSubpart($content, $sPK, $sPK);
                 // Finding subparts and substituting them with the subpart as a marker
                 foreach ($sPkeys as $sPK) {
                     $content = $this->substituteSubpart($content, $sPK, $sPK);
@@ -2203,15 +2201,29 @@ class ContentObjectRenderer
                         $wPK
                     ));
                 }
                         $wPK
                     ));
                 }
-                // Traverse keys and quote them for reg ex.
-                foreach ($aKeys as $tK => $tV) {
-                    $aKeys[$tK] = preg_quote($tV, '/');
+
+                $storeArr = array();
+                $result = preg_match_all('/###([\w:-]+)###/', $content, $usedMarkers);
+                if ($result !== false && !empty($usedMarkers[1])) {
+                    $tagArray = array_flip($usedMarkers[1]);
+
+                    $aKeys = array_flip($aKeys);
+                    $bKeys = array();
+                    // Traverse keys and quote them for reg ex.
+                    foreach ($tagArray as $tV => $tK) {
+                        $tV = '###' . $tV . '###';
+                        if (isset($aKeys[$tV])) {
+                            $bKeys[$tK] = preg_quote($tV, '/');
+                        }
+                    }
+                    $regex = '/' . implode('|', $bKeys) . '/';
+                    // Doing regex's
+                    if (preg_match_all($regex, $content, $keyList) !== false) {
+                        $storeArr['c'] = preg_split($regex, $content);
+                        $storeArr['k'] = $keyList[0];
+                    }
                 }
                 }
-                $regex = '/' . implode('|', $aKeys) . '/';
-                // Doing regex's
-                $storeArr['c'] = preg_split($regex, $content);
-                preg_match_all($regex, $content, $keyList);
-                $storeArr['k'] = $keyList[0];
+
                 // Setting cache:
                 $this->substMarkerCache[$storeKey] = $storeArr;
                 // Storing the cached data:
                 // Setting cache:
                 $this->substMarkerCache[$storeKey] = $storeArr;
                 // Storing the cached data: