{"record":{"id":"9c61db7a067426fe","repo":"EllanJiang/GameFramework","slug":"structure-size-is-invalid","errorCode":null,"errorMessage":"Structure size is invalid.","messagePattern":"Structure size is invalid\\.","errorType":"exception","errorClass":"GameFrameworkException","httpStatus":null,"severity":"error","filePath":"GameFramework/Utility/Utility.Marshal.cs","lineNumber":89,"sourceCode":"            /// <param name=\"structure\">要转换的对象。</param>\n            /// <returns>存储转换结果的二进制流。</returns>\n            public static byte[] StructureToBytes<T>(T structure)\n            {\n                return StructureToBytes(structure, System.Runtime.InteropServices.Marshal.SizeOf(typeof(T)));\n            }\n\n            /// <summary>\n            /// 将数据从对象转换为二进制流。\n            /// </summary>\n            /// <typeparam name=\"T\">要转换的对象的类型。</typeparam>\n            /// <param name=\"structure\">要转换的对象。</param>\n            /// <param name=\"structureSize\">要转换的对象的大小。</param>\n            /// <returns>存储转换结果的二进制流。</returns>\n            internal static byte[] StructureToBytes<T>(T structure, int structureSize)\n            {\n                if (structureSize < 0)\n                {\n                    throw new GameFrameworkException(\"Structure size is invalid.\");\n                }\n\n                EnsureCachedHGlobalSize(structureSize);\n                System.Runtime.InteropServices.Marshal.StructureToPtr(structure, s_CachedHGlobalPtr, true);\n                byte[] result = new byte[structureSize];\n                System.Runtime.InteropServices.Marshal.Copy(s_CachedHGlobalPtr, result, 0, structureSize);\n                return result;\n            }\n\n            /// <summary>\n            /// 将数据从对象转换为二进制流。\n            /// </summary>\n            /// <typeparam name=\"T\">要转换的对象的类型。</typeparam>\n            /// <param name=\"structure\">要转换的对象。</param>\n            /// <param name=\"result\">存储转换结果的二进制流。</param>\n            public static void StructureToBytes<T>(T structure, byte[] result)\n            {\n                StructureToBytes(structure, System.Runtime.InteropServices.Marshal.SizeOf(typeof(T)), result, 0);","sourceCodeStart":71,"sourceCodeEnd":107,"githubUrl":"https://github.com/EllanJiang/GameFramework/blob/d0c010b05167c58e92350449d04864a91ca13fd2/GameFramework/Utility/Utility.Marshal.cs#L71-L107","documentation":"Utility.Marshal.StructureToBytes<T>(T, int) validates structureSize before using the cached unmanaged buffer. A negative size would produce an invalid allocation and array, so the library throws immediately. This is an internal overload; callers normally use the public wrapper that supplies Marshal.SizeOf automatically.","triggerScenarios":"Calling the internal overload StructureToBytes(structure, structureSize) with a negative size, typically from a buggy size computation or corrupted external size value.","commonSituations":"Custom serialization code caching a wrong size; sizes read from untrusted data streams used directly.","solutions":["Always pass Marshal.SizeOf<T>(structure) (or Utility.Marshal.SizeOf<T>()) as structureSize","Use the public StructureToBytes<T>(T) overload that computes the size itself","Validate any externally sourced size value before calling"],"exampleFix":"// before\nUtility.Marshal.StructureToBytes(s, untrustedSize); // may be negative\n// after\nint size = Utility.Marshal.SizeOf(s);\nbyte[] bytes = Utility.Marshal.StructureToBytes(s, size);","handlingStrategy":"validation","validationCode":"if (structureSize < 0) structureSize = Utility.Marshal.SizeOf(structure);","typeGuard":"bool IsValidStructureSize(int size) => size >= 0;","tryCatchPattern":null,"preventionTips":["Use Utility.Marshal.SizeOf<T>() for all structure sizes","Use the single-argument StructureToBytes overload that computes the size","Recompute sizes after changing struct definitions","Never trust sizes from untrusted sources"],"tags":["marshal","interop","argument-validation","gameframework"],"backgroundTag":"invalid-argument-value","analyzedSha":"d0c010b05167c58e92350449d04864a91ca13fd2","analyzedAt":"2026-09-15T13:37:15.352Z","contentChangedAt":"2026-09-15T13:37:15.352Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}