Improve infinite loop detection logic based on code review

- Remove unreachable condition check
- Add proper check for consecutive zero-length streams
- Verify both old and new streams to detect invalid state

Co-authored-by: adamhathcock <527620+adamhathcock@users.noreply.github.com>
This commit is contained in:
copilot-swe-agent[bot]
2026-01-31 11:04:17 +00:00
parent 8cd667d0c3
commit 7eed908ace

View File

@@ -223,7 +223,6 @@ public partial class SourceStream : Stream, IStreamStack
while (_prevSize + Current.Length < pos)
{
var currentLength = Current.Length;
var currentStreamIndex = _stream;
_prevSize += currentLength;
if (!SetStream(_stream + 1))
@@ -234,12 +233,13 @@ public partial class SourceStream : Stream, IStreamStack
);
}
// Check if we're making progress (stream changed or has non-zero length)
if (_stream == currentStreamIndex && currentLength == 0)
// Safety check: if we have a zero-length stream and we're still not
// making progress toward the target position, we're in an invalid state
if (currentLength == 0 && Current.Length == 0)
{
// Stream didn't change and has zero length - infinite loop detected
// Both old and new stream have zero length - cannot make progress
throw new InvalidOperationException(
$"Cannot seek to position {pos}. Stream has zero length and no next stream available."
$"Cannot seek to position {pos}. Encountered zero-length streams at position {_prevSize}."
);
}
}