From 8c22f0c48540793452b70d913b2a6a719c881a33 Mon Sep 17 00:00:00 2001 From: Randy Date: Fri, 20 Oct 2023 03:59:21 -0700 Subject: [PATCH] [RegistryPreview]Fix wrong key path transversal when a key was a substring of a parent (#29311) Replaced String.Replace with String.Substring to avoid the removal of multiple instances of a string, rather than just the last one. --- .../RegistryPreviewUI/MainWindow.Utilities.cs | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/src/modules/registrypreview/RegistryPreviewUI/MainWindow.Utilities.cs b/src/modules/registrypreview/RegistryPreviewUI/MainWindow.Utilities.cs index 0941943639..450c3f5e78 100644 --- a/src/modules/registrypreview/RegistryPreviewUI/MainWindow.Utilities.cs +++ b/src/modules/registrypreview/RegistryPreviewUI/MainWindow.Utilities.cs @@ -774,7 +774,19 @@ namespace RegistryPreview // before moving onto the next node, tag the previous node and update the path previousNode = newNode; - fullPath = fullPath.Replace(string.Format(CultureInfo.InvariantCulture, @"\{0}", individualKeys[i]), string.Empty); + + // this used to use Replace but that would replace all instances of the same key name, which causes bugs. + try + { + int removeAt = fullPath.LastIndexOf(string.Format(CultureInfo.InvariantCulture, @"\{0}", individualKeys[i]), StringComparison.InvariantCulture); + if (removeAt > -1) + { + fullPath = fullPath.Substring(0, removeAt); + } + } + catch + { + } // One last check: if we get here, the parent of this node is not yet in the tree, so we need to add it as a RootNode if (i == 0)