diff --git a/Analysis/TracedHeap.cs b/Analysis/TracedHeap.cs index d9d9c0b..7c32cd2 100644 --- a/Analysis/TracedHeap.cs +++ b/Analysis/TracedHeap.cs @@ -116,7 +116,16 @@ public TracedHeap(IRootSet rootSet, ILogger logger) m_objectAddressToPostorderIndex[lookupEntryIndex].PostorderIndex = postorderIndex; lookupEntryIndex++; - m_numberOfLiveBytes += m_traceableHeap.GetObjectSize(m_native.From(address), typeIndex, committedOnly: true); + int objectSize = m_traceableHeap.GetObjectSize(m_native.From(address), typeIndex, committedOnly: true); + if (objectSize < 0) + { + // This indicates a memory corruption (array or string with invalid length field) + LogWarning(m_traceableHeap.TypeSystem.QualifiedName(typeIndex), $"object {postorderIndex} has negative size {objectSize}"); + } + else + { + m_numberOfLiveBytes += objectSize; + } } } Array.Sort(m_objectAddressToPostorderIndex, (x, y) => x.Address.CompareTo(y.Address)); diff --git a/CommandInfrastructure/Command.cs b/CommandInfrastructure/Command.cs index 8690e8c..b32ec0c 100644 --- a/CommandInfrastructure/Command.cs +++ b/CommandInfrastructure/Command.cs @@ -547,7 +547,13 @@ protected void DumpObjectMemory(MemoryView objectView, int typeIndex, int indent int length = maxLength < stringLength ? maxLength : stringLength; for (int i = 0; i < length; i++) { - objectView.Read(CurrentTraceableHeap.TypeSystem.SystemStringFirstCharOffset + i * 2, out char c); + int offset = CurrentTraceableHeap.TypeSystem.SystemStringFirstCharOffset + i * 2; + if (offset + 2 > objectView.Size) + { + // Guard against crashing if there is a memory corruption (string with invalid length) + break; + } + objectView.Read(offset, out char c); sb.Append(c); }