{"record":{"id":"807d08b2edc3f29e","repo":"egametang/ET","slug":"from-bson-error-type-fullname-bytes-length-i","errorCode":null,"errorMessage":"from bson error: {type.FullName} {bytes.Length} {index} {count}","messagePattern":"from bson error: (.+?) (.+?) (.+?) (.+?)","errorType":"exception","errorClass":"Exception","httpStatus":null,"severity":"error","filePath":"Packages/cn.etetet.core/Scripts/Core/Share/Serialize/MongoHelper.cs","lineNumber":141,"sourceCode":"                return BsonSerializer.Deserialize(bytes, type);\n            }\n            catch (Exception e)\n            {\n                throw new Exception($\"from bson error: {type.FullName} {bytes.Length}\", e);\n            }\n        }\n\n        public static object Deserialize(Type type, byte[] bytes, int index, int count)\n        {\n            try\n            {\n                using MemoryStream memoryStream = new(bytes, index, count);\n                \n                return BsonSerializer.Deserialize(memoryStream, type);\n            }\n            catch (Exception e)\n            {\n                throw new Exception($\"from bson error: {type.FullName} {bytes.Length} {index} {count}\", e);\n            }\n        }\n\n        public static object Deserialize(Type type, Stream stream)\n        {\n            try\n            {\n                return BsonSerializer.Deserialize(stream, type);\n            }\n            catch (Exception e)\n            {\n                throw new Exception($\"from bson error: {type.FullName} {stream.Position} {stream.Length}\", e);\n            }\n        }\n\n        public static T Deserialize<T>(byte[] bytes)\n        {\n            try","sourceCodeStart":123,"sourceCodeEnd":159,"githubUrl":"https://github.com/egametang/ET/blob/5cab01f7a8bee5f49f4781eebe9e2b1c6d7ebe0f/Packages/cn.etetet.core/Scripts/Core/Share/Serialize/MongoHelper.cs#L123-L159","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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."],"exampleFix":"// before\nvar msg = MongoHelper.Deserialize(type, buf, offset, n);\n\n// after\nif (offset < 0 || n <= 0 || offset + n > buf.Length)\n    throw new ArgumentOutOfRangeException($\"bad slice index={offset} count={n} len={buf.Length}\");\nvar msg = MongoHelper.Deserialize(type, buf, offset, n);","handlingStrategy":"validation","validationCode":"if (bytes == null) throw new ArgumentNullException(nameof(bytes));\nif (index < 0 || count <= 0 || (long)index + count > bytes.Length)\n    throw new ArgumentOutOfRangeException($\"invalid slice idx={index} cnt={count} len={bytes.Length}\");","typeGuard":null,"tryCatchPattern":"try { return MongoHelper.Deserialize(type, bytes, index, count); }\ncatch (Exception e) { Log.Error($\"Deserialize slice {type} idx={index} cnt={count} failed: {e}\"); throw; }","preventionTips":["Validate index/count bounds before every slice deserialize.","Confirm the slice is a complete document by reading its embedded length prefix."],"tags":["serialization","mongodb","bson","network"],"backgroundTag":null,"analyzedSha":"5cab01f7a8bee5f49f4781eebe9e2b1c6d7ebe0f","analyzedAt":"2026-08-13T21:10:40.377Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}