From 80c5f881366e32d484b572b9948df064a6f32573 Mon Sep 17 00:00:00 2001 From: Jeremy Powell Date: Tue, 19 Nov 2024 15:48:54 +1300 Subject: [PATCH] Validate directory tree --- OpenMcdf/DirectoryTree.cs | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/OpenMcdf/DirectoryTree.cs b/OpenMcdf/DirectoryTree.cs index cf2d605..2f8a08e 100644 --- a/OpenMcdf/DirectoryTree.cs +++ b/OpenMcdf/DirectoryTree.cs @@ -38,11 +38,27 @@ public bool TryGetDirectoryEntry(string name, out DirectoryEntry? entry) int compare = DirectoryEntryComparer.Compare(nameSpan, child.NameCharSpan); if (compare < 0) { - directories.TryGetDictionaryEntry(child.LeftSiblingId, out child); + directories.TryGetDictionaryEntry(child.LeftSiblingId, out DirectoryEntry? leftChild); + if (leftChild is not null) + { + compare = DirectoryEntryComparer.Compare(leftChild.NameCharSpan, child.NameCharSpan); + if (compare >= 0) + throw new FormatException("Directory tree is not sorted."); + } + + child = leftChild; } else if (compare > 0) { - directories.TryGetDictionaryEntry(child.RightSiblingId, out child); + directories.TryGetDictionaryEntry(child.RightSiblingId, out DirectoryEntry? rightChild); + if (rightChild is not null) + { + compare = DirectoryEntryComparer.Compare(rightChild.NameCharSpan, child.NameCharSpan); + if (compare <= 0) + throw new FormatException("Directory tree is not sorted."); + } + + child = rightChild; } else {