egametang/ET · error · Exception
from bson error: {type.FullName} {bytes.Length} {index} {cou
Error message
from bson error: {type.FullName} {bytes.Length} {index} {count} What it means
Thrown by the slice overload MongoHelper.Deserialize(Type, byte[], int index, int count) (line 141) wrapping BsonSerializer.Deserialize over a MemoryStream(bytes, index, count). Fails when index/count do not bound a complete BSON document, or the slice is corrupt. Inner exception IS preserved.
Source
Thrown at Packages/cn.etetet.core/Scripts/Core/Share/Serialize/MongoHelper.cs:141
return BsonSerializer.Deserialize(bytes, type);
}
catch (Exception e)
{
throw new Exception($"from bson error: {type.FullName} {bytes.Length}", e);
}
}
public static object Deserialize(Type type, byte[] bytes, int index, int count)
{
try
{
using MemoryStream memoryStream = new(bytes, index, count);
return BsonSerializer.Deserialize(memoryStream, type);
}
catch (Exception e)
{
throw new Exception($"from bson error: {type.FullName} {bytes.Length} {index} {count}", e);
}
}
public static object Deserialize(Type type, Stream stream)
{
try
{
return BsonSerializer.Deserialize(stream, type);
}
catch (Exception e)
{
throw new Exception($"from bson error: {type.FullName} {stream.Position} {stream.Length}", e);
}
}
public static T Deserialize<T>(byte[] bytes)
{
tryView on GitHub (pinned to 5cab01f7a8)
Solutions
- Validate index >= 0, count > 0, index + count <= bytes.Length before calling.
- Ensure the [index, index+count) region is exactly one BSON document (check its 4-byte leading length).
- Read the inner exception for the decode error.
- Fix framing logic to pass correct index/count.
Example fix
// before
var msg = MongoHelper.Deserialize(type, buf, offset, n);
// after
if (offset < 0 || n <= 0 || offset + n > buf.Length)
throw new ArgumentOutOfRangeException($"bad slice index={offset} count={n} len={buf.Length}");
var msg = MongoHelper.Deserialize(type, buf, offset, n); Defensive patterns
Strategy: validation
Validate before calling
if (bytes == null) throw new ArgumentNullException(nameof(bytes));
if (index < 0 || count <= 0 || (long)index + count > bytes.Length)
throw new ArgumentOutOfRangeException($"invalid slice idx={index} cnt={count} len={bytes.Length}"); Try / catch
try { return MongoHelper.Deserialize(type, bytes, index, count); }
catch (Exception e) { Log.Error($"Deserialize slice {type} idx={index} cnt={count} failed: {e}"); throw; } Prevention
- Validate index/count bounds before every slice deserialize.
- Confirm the slice is a complete document by reading its embedded length prefix.
When it happens
Trigger: Index/count describe a region that is not a whole BSON document (split across frames), index/count out of range, negative count, or the sliced bytes are corrupt/truncated.
Common situations: Decoding length-prefixed network messages where the body is a slice of a larger buffer, pooled buffer reuse with stale count, off-by-one framing bugs.
Related errors
- Serialize error {obj.GetType().FullName} {e}
- Serialize error {message.GetType().FullName} {e}
- from bson error: {type.FullName} {bytes.Length}
- from bson error: {type.FullName} {stream.Position} {stream.L
- from bson error: {typeof (T).FullName} {bytes.Length}
AI-assisted analysis of egametang/ET@5cab01f7a8 (2026-08-13).
Data as JSON: /api/errors/807d08b2edc3f29e.
Report an issue: GitHub.