[BUGFIX] Avoid overly large regex in substituteMarkerArrayCached 32/45132/2
authorBenni Mack <benni@typo3.org>
Fri, 4 Dec 2015 22:54:45 +0000 (23:54 +0100)
committerBenni Mack <benni@typo3.org>
Fri, 4 Dec 2015 23:01:38 +0000 (00:01 +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/45132
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 f93b0c9..9c286c0 100644 (file)
@@ -1865,8 +1865,6 @@ class ContentObjectRenderer {
                                $this->substMarkerCache[$storeKey] = $storeArr;
                                $GLOBALS['TT']->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);
@@ -1878,15 +1876,29 @@ class ContentObjectRenderer {
                                                $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: