From c9873d5ea8f2f27405214dc66db193bc7567b0a5 Mon Sep 17 00:00:00 2001
From: Oliver Hader <oliver@typo3.org>
Date: Thu, 24 Aug 2023 13:09:00 +0200
Subject: [PATCH] [BUGFIX] Fix several undefined array key issues in
 DataHandler scope

DataMapProcessor tries to access `$possibleChainedIds[0]` which
might not be defined, since that array is the result of a previous
`array_intersect` invokation, which keeps the array keys of the
original input array.

DataHandler tries to access `$this->copyMappingArray[$table][$uid]`,
which might not be defined in case the previous copy command could
not be executed (e.g. since the record was deleted already).

Resolves: #101748
Releases: main, 12.4, 11.5
Change-Id: Ibcbc412c7df62ce17dfa2c0f6fc120ccebb953d8
Reviewed-on: https://review.typo3.org/c/Packages/TYPO3.CMS/+/80669
Reviewed-by: Andreas Fernandez <a.fernandez@scripting-base.de>
Reviewed-by: Oliver Klee <typo3-coding@oliverklee.de>
Tested-by: Andreas Fernandez <a.fernandez@scripting-base.de>
Tested-by: core-ci <typo3@b13.com>
---
 Build/phpstan/phpstan-baseline.neon                       | 2 +-
 typo3/sysext/core/Classes/DataHandling/DataHandler.php    | 8 ++++----
 .../DataHandling/Localization/DataMapProcessor.php        | 3 ++-
 3 files changed, 7 insertions(+), 6 deletions(-)

diff --git a/Build/phpstan/phpstan-baseline.neon b/Build/phpstan/phpstan-baseline.neon
index c7f536218e1b..6f276a7a46ac 100644
--- a/Build/phpstan/phpstan-baseline.neon
+++ b/Build/phpstan/phpstan-baseline.neon
@@ -741,7 +741,7 @@ parameters:
 			path: ../../typo3/sysext/core/Classes/DataHandling/DataHandler.php
 
 		-
-			message: "#^Offset string does not exist on array\\{\\}\\.$#"
+			message: "#^Offset string on array\\{\\} on left side of \\?\\? does not exist\\.$#"
 			count: 1
 			path: ../../typo3/sysext/core/Classes/DataHandling/DataHandler.php
 
diff --git a/typo3/sysext/core/Classes/DataHandling/DataHandler.php b/typo3/sysext/core/Classes/DataHandling/DataHandler.php
index c07c4b449e70..95d4a72d51de 100644
--- a/typo3/sysext/core/Classes/DataHandling/DataHandler.php
+++ b/typo3/sysext/core/Classes/DataHandling/DataHandler.php
@@ -3174,7 +3174,7 @@ class DataHandler implements LoggerAwareInterface
                                 } else {
                                     $this->copyRecord($table, (int)$id, $target, true, [], '', 0, $ignoreLocalization);
                                 }
-                                $procId = $this->copyMappingArray[$table][$id];
+                                $procId = $this->copyMappingArray[$table][$id] ?? null;
                                 break;
                             case 'localize':
                                 $this->useTransOrigPointerField = true;
@@ -3195,7 +3195,7 @@ class DataHandler implements LoggerAwareInterface
                                 break;
                         }
                         $this->useTransOrigPointerField = $backupUseTransOrigPointerField;
-                        if (is_array($pasteUpdate)) {
+                        if (is_array($pasteUpdate) && $procId > 0) {
                             $pasteDatamap[$table][$procId] = $pasteUpdate;
                         }
                     }
@@ -3403,7 +3403,7 @@ class DataHandler implements LoggerAwareInterface
                 $CPtable = $this->int_pageTreeInfo([], $uid, (int)$this->copyTree, $theNewRootID);
                 // Now copying the subpages:
                 foreach ($CPtable as $thePageUid => $thePagePid) {
-                    $newPid = $this->copyMappingArray['pages'][$thePagePid];
+                    $newPid = $this->copyMappingArray['pages'][$thePagePid] ?? null;
                     if (isset($newPid)) {
                         $this->copySpecificPage($thePageUid, $newPid, $copyTablesAlongWithPage);
                     } else {
@@ -6395,7 +6395,7 @@ class DataHandler implements LoggerAwareInterface
                     $thePidToUpdate = $theUidToUpdate;
                 } elseif (isset($this->registerDBPids[$table][$uid])) {
                     $thePidToUpdate = $this->registerDBPids[$table][$uid];
-                    $thePidToUpdate = $this->copyMappingArray_merged['pages'][$thePidToUpdate];
+                    $thePidToUpdate = $this->copyMappingArray_merged['pages'][$thePidToUpdate] ?? null;
                 }
 
                 // Update child records if change to pid is required
diff --git a/typo3/sysext/core/Classes/DataHandling/Localization/DataMapProcessor.php b/typo3/sysext/core/Classes/DataHandling/Localization/DataMapProcessor.php
index 088d1cbcea16..241256f67d14 100644
--- a/typo3/sysext/core/Classes/DataHandling/Localization/DataMapProcessor.php
+++ b/typo3/sysext/core/Classes/DataHandling/Localization/DataMapProcessor.php
@@ -1085,7 +1085,8 @@ class DataMapProcessor
                     $ancestorIdMap[$ancestorId]
                 );
                 if (!empty($possibleChainedIds)) {
-                    $ancestorId = $possibleChainedIds[0];
+                    // use the first found id from `$possibleChainedIds`
+                    $ancestorId = reset($possibleChainedIds);
                     $dependentIdMap[$ancestorId] = $dependentId;
                 }
             }
-- 
GitLab