{"record":{"id":"3d7ce7e114e20fd4","repo":"EllanJiang/GameFramework","slug":"buffer-length-is-not-enough","errorCode":null,"errorMessage":"Buffer length is not enough.","messagePattern":"Buffer length is not enough\\.","errorType":"exception","errorClass":"GameFrameworkException","httpStatus":null,"severity":"error","filePath":"GameFramework/Utility/Utility.Marshal.cs","lineNumber":231,"sourceCode":"            {\n                if (structureSize < 0)\n                {\n                    throw new GameFrameworkException(\"Structure size is invalid.\");\n                }\n\n                if (buffer == null)\n                {\n                    throw new GameFrameworkException(\"Buffer is invalid.\");\n                }\n\n                if (startIndex < 0)\n                {\n                    throw new GameFrameworkException(\"Start index is invalid.\");\n                }\n\n                if (startIndex + structureSize > buffer.Length)\n                {\n                    throw new GameFrameworkException(\"Buffer length is not enough.\");\n                }\n\n                EnsureCachedHGlobalSize(structureSize);\n                System.Runtime.InteropServices.Marshal.Copy(buffer, startIndex, s_CachedHGlobalPtr, structureSize);\n                return (T)System.Runtime.InteropServices.Marshal.PtrToStructure(s_CachedHGlobalPtr, typeof(T));\n            }\n        }\n    }\n}\n","sourceCodeStart":213,"sourceCodeEnd":241,"githubUrl":"https://github.com/EllanJiang/GameFramework/blob/d0c010b05167c58e92350449d04864a91ca13fd2/GameFramework/Utility/Utility.Marshal.cs#L213-L241","documentation":"BytesToStructure checks that buffer contains structureSize bytes starting at startIndex. If startIndex + structureSize exceeds buffer.Length, the read would run past the end of the array during Marshal.Copy, so the library throws GameFrameworkException. This protects against truncated or misaligned binary data.","triggerScenarios":"Calling Utility.Marshal.BytesToStructure on a buffer shorter than startIndex + structureSize — e.g. a truncated packet, a file written by an older version with smaller records, or reading at the wrong offset.","commonSituations":"Version drift between writer and reader of a binary format (struct gained fields); corrupted/partial downloads in resource pipelines; assuming buffer alignment after skipping a variable-length header.","solutions":["Verify buffer.Length >= startIndex + structureSize before calling, and use Marshal.SizeOf<T> as structureSize.","Check for truncation upstream: validate the total length of packets/files before parsing.","Align binary format versions — include a version/size header in the data or bump the format version check."],"exampleFix":"// before\nvar header = Utility.Marshal.BytesToStructure<Header>(32, data, 4); // data.Length == 20\n// after\nif (data.Length < 4 + Marshal.SizeOf<Header>())\n    throw new InvalidDataException(\"Buffer too small for header\");\nvar header = Utility.Marshal.BytesToStructure<Header>(Marshal.SizeOf<Header>(), data, 4);","handlingStrategy":"validation","validationCode":"int size = Marshal.SizeOf<T>();\nif (buffer == null || buffer.Length < startIndex + size)\n    throw new InvalidDataException($\"Buffer too small: need {startIndex + size}, have {buffer?.Length ?? 0}\");\nvar value = Utility.Marshal.BytesToStructure<T>(size, buffer, startIndex);","typeGuard":null,"tryCatchPattern":"try\n{\n    var v = Utility.Marshal.BytesToStructure<T>(size, buffer, offset);\n}\ncatch (GameFrameworkException ex) when (ex.Message == \"Buffer length is not enough.\")\n{\n    // treat as truncated data: reject packet/file and re-request\n}","preventionTips":["Validate total data length against expected record layout before parsing.","Include a version/size field in binary formats to detect writer/reader drift.","Verify checksums/lengths on downloaded or received buffers before deserialization."],"tags":["buffer-size","marshaling","gameframework"],"backgroundTag":"value-out-of-range","analyzedSha":"d0c010b05167c58e92350449d04864a91ca13fd2","analyzedAt":"2026-09-15T13:37:15.352Z","contentChangedAt":"2026-09-15T13:37:15.352Z","schemaVersion":2},"datasetVersion":"2026-09-15T23:17:13.987Z"}