ShellStream.Expect over-discards on undecodable bytes, driving ArrayBuffer.ActiveLength negative
Chưa có ai nhận issue này.
Đánh giá
- Độ khó
- 4/5
- Thời gian dự kiến
- 3-5 ngày
- Mức phù hợp với người mới
- 52/100
- Loại issue
- Lỗi
- Độ rõ ràng
- Khá rõ ràng
- Mức độ hoạt động
- Sôi nổi
- Công nghệ
- csharp
- Lĩnh vực
- networking
Hướng nghiên cứu
Start with ShellStream.ExpectRegex in src/Renci.SshNet/ShellStream.cs and ArrayBuffer.Discard in src/Renci.SshNet/Common/ArrayBuffer.cs. Reproduce the undecodable-byte arithmetic, then trace the existing Discard call sites and verify that consumed input bytes are counted without making ActiveLength negative. Done means the Release-only failure no longer occurs and the over-discard is surfaced at its origin if it recurs.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Mô tả
Summary
ShellStream.ExpectRegex decodes the read buffer to a string, matches against that string, and then
discards the byte count of the re-encoded match:
var bufferText = GetString(_readBuffer.ActiveLength);
...
var returnText = bufferText.Substring(0, match.Index + match.Length);
_readBuffer.Discard(_encoding.GetByteCount(returnText));
GetByteCount(returnText) equals the number of buffer bytes returnText was decoded from only when
the decode was lossless. It is not, whenever the consumed region contained a byte the encoding could
not decode: GetString replaces each maximal invalid subpart with U+FFFD, which re-encodes to
3 bytes regardless of how many bytes it replaced.
So Discard is asked for more bytes than the match occupied. And ArrayBuffer.Discard only checks
that bound with a Debug.Assert:
public void Discard(int byteCount)
{
Debug.Assert(byteCount <= ActiveLength, $"Expected {byteCount} <= {ActiveLength}");
_activeStart += byteCount;
if (_activeStart == _availableStart)
{
_activeStart = 0;
_availableStart = 0;
}
}
In a Release build the assert is gone, _activeStart overshoots past _availableStart, and
ActiveLength (_availableStart - _activeStart) goes negative. The _activeStart == _availableStart
reset cannot fire either, because the overshoot skipped equality — so the buffer never recovers.
The next iteration then calls GetString(_readBuffer.ActiveLength) with a negative length, and throws.
- https://github.com/sshnet/SSH.NET/blob/f099365c9d4cf2ade92b92c203bbb2b345d2cd74/src/Renci.SshNet/ShellStream.cs#L548
- https://github.com/sshnet/SSH.NET/blob/f099365c9d4cf2ade92b92c203bbb2b345d2cd74/src/Renci.SshNet/Common/ArrayBuffer.cs#L106
What it looks like in production
System.ArgumentOutOfRangeException: Non-negative number required. (Parameter 'count')
at Renci.SshNet.ShellStream.ExpectRegex(TimeSpan timeout, Int32 lookback, ExpectAction[] expectActions)
at <our shell wrapper>.ExecuteCommandAsync(String command)
Reproduced across several runs and several hosts, against Junos network devices, reading a large
configuration and then issuing a second command on the same ShellStream.
Note this is a Release-only symptom. Under Debug the assert fires instead, so a test run would
report the over-discard at its origin rather than as a later crash.
Minimal repro of the arithmetic (no SSH server needed)
The defect reduces to this identity. With the default Encoding.UTF8:
foreach (var bytes in new[]
{
new byte[] { 0x61, 0xF8, 0x62 }, // 'a', a lone 0xF8, 'b'
new byte[] { 0x61, 0xE2, 0x82, 0x62 }, // a truncated 3-byte sequence
new byte[] { 0x61, 0xFF, 0xFF, 0x62 },
new byte[] { 0x61, 0xC3, 0xB8, 0x62 }, // valid UTF-8, for contrast
})
{
var text = Encoding.UTF8.GetString(bytes);
Console.WriteLine($"in={bytes.Length} out={Encoding.UTF8.GetByteCount(text)}");
}
in=3 out=5 <- Discard() is asked for 2 bytes more than the match occupied
in=4 out=5 <- 1 byte more
in=4 out=8 <- 4 bytes more
in=4 out=4 <- lossless decode, correct
Every case where the two disagree drives ActiveLength negative by the difference.
ExpectRegex is the only one of the ten Discard(...) call sites in ShellStream that computes its
count by re-encoding; the others count in byte space (ActiveLength, indexOf…, bytesRead) and are
not affected.
Suggested direction
The number of bytes to discard is a fact about the input bytes, so re-encoding the output string
cannot recover it. Options, roughly by intrusiveness:
- Decode with a
Decoderand track how many input bytes produced the consumed prefix. - Keep a char-index → byte-offset mapping while building
bufferText. - Independently, consider making
ArrayBuffer.Discardclamp or throw rather thanDebug.Assert, so an
over-discard surfaces where it happens instead of as anArgumentOutOfRangeExceptionone call later.
Happy to test a patch — we have hosts that reproduce it reliably.
Environment
- SSH.NET 2026.0.0. The two lines above are byte-identical in 2025.1.0, 2026.0.0 and
develop. - .NET 10, Release build, Linux container.
- Ngôn ngữ chính
- C#
- Star
- 4.4k
- Fork
- 994
- Chỉ số merge pull request
- Không có pull request nào được merge trong 30 ngày
Chuẩn bị môi trường
- Không có Dockerfile hay tệp Docker Compose
- Không có mẫu pull request
- Đọc hướng dẫn đóng góp
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Issue khác của sshnet/SSH.NET
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 62/100
-
Độ khó 3/5 1-2 ngày Mức phù hợp với người mới 68/100
-
Using SshClient on Linux under Wine throws System.Security.Cryptography.CryptographicExceptionĐang mở
Độ khó 4/5 3-5 ngày Mức phù hợp với người mới 48/100
-
Độ khó 3/5 1-2 ngày Mức phù hợp với người mới 67/100
-
Độ khó 4/5 3-5 ngày Mức phù hợp với người mới 55/100
Tất cả issue của sshnet/SSH.NET
Issue tương tự
-
bug
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 75/100
AvaloniaUI/Avalonia#22323 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 88/100
microsoft/onnxruntime-genai#2633 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
FluentDataGrid: pinned column offsets leak into the grid of an open RowDetails (master-detail)Đang mởNot reproducible
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 82/100
microsoft/fluentui-blazor#5350 · 4 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày
-
python triage
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 85/100
microsoft/semantic-kernel#14491 ·
Maintainer thường phản hồi trong vòng 2 ngày
-
area:jobads-cv FE mvp P3
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 88/100
klasolsson81/jobbliggaren#1878 ·
Maintainer thường phản hồi trong vòng 1 ngày