Reduce undefined behavior in item dictionaries

This commit is contained in:
Matt Nadareski
2025-01-14 22:28:18 -05:00
parent c20cf2a695
commit 6b31acaa85
2 changed files with 32 additions and 14 deletions

View File

@@ -197,18 +197,16 @@ namespace SabreTools.DatFiles
{ {
foreach (string key in SortedKeys) foreach (string key in SortedKeys)
{ {
// Perform filtering on items // Get the unfiltered list
List<DatItem> list = GetItemsForBucket(key, filter: true); List<DatItem> list = GetItemsForBucket(key, filter: false);
// Remove the current list foreach (DatItem datItem in list)
RemoveBucket(key); {
if (datItem.GetBoolFieldValue(DatItem.RemoveKey) != true)
continue;
// Add the filtered list back RemoveItem(key, datItem);
#if NET40_OR_GREATER || NETCOREAPP }
_ = _items.TryAdd(key, list);
#else
_items[key] = list;
#endif
} }
} }

View File

@@ -308,13 +308,13 @@ namespace SabreTools.DatFiles
foreach (long itemIndex in itemIndices) foreach (long itemIndex in itemIndices)
{ {
#if NET40_OR_GREATER || NETCOREAPP #if NET40_OR_GREATER || NETCOREAPP
if (!_items.TryGetValue(itemIndex, out var datItem)) if (!_items.TryGetValue(itemIndex, out var datItem) || datItem == null)
continue; continue;
#else #else
var datItem = _items[itemIndex]; var datItem = _items[itemIndex];
#endif #endif
if (datItem == null || datItem.GetBoolFieldValue(DatItem.RemoveKey) != true) if (datItem.GetBoolFieldValue(DatItem.RemoveKey) != true)
continue; continue;
RemoveItem(itemIndex); RemoveItem(itemIndex);
@@ -545,15 +545,35 @@ namespace SabreTools.DatFiles
public bool RemoveBucket(string key) public bool RemoveBucket(string key)
{ {
#if NET40_OR_GREATER || NETCOREAPP #if NET40_OR_GREATER || NETCOREAPP
return _buckets.TryRemove(key, out var list); bool removed = _buckets.TryRemove(key, out var list);
#else #else
if (!_buckets.ContainsKey(key)) if (!_buckets.ContainsKey(key))
return false; return false;
bool removed = true;
var list = _buckets[key]; var list = _buckets[key];
_buckets.Remove(key); _buckets.Remove(key);
return true;
#endif #endif
if (list == null)
return removed;
foreach (var index in list)
{
#if NET40_OR_GREATER || NETCOREAPP
if (!_items.TryGetValue(index, out var datItem) || datItem == null)
continue;
#else
if (!_items.ContainsKey(index))
continue;
var datItem = _items[index];
#endif
RemoveItem(index);
DatStatistics.RemoveItemStatistics(datItem);
}
return removed;
} }
/// <summary> /// <summary>