{"record":{"id":"fb866f10452a1347","repo":"EllanJiang/GameFramework","slug":"buffer-is-invalid-resourcemanager","errorCode":null,"errorMessage":"Buffer is invalid.","messagePattern":"Buffer is invalid\\.","errorType":"validation","errorClass":"GameFrameworkException","httpStatus":null,"severity":"error","filePath":"GameFramework/Resource/ResourceManager.cs","lineNumber":1885,"sourceCode":"            return m_ResourceLoader.LoadBinaryFromFileSystem(binaryAssetName);\n        }\n\n        /// <summary>\n        /// 从文件系统中加载二进制资源。\n        /// </summary>\n        /// <param name=\"binaryAssetName\">要加载二进制资源的名称。</param>\n        /// <param name=\"buffer\">存储加载二进制资源的二进制流。</param>\n        /// <returns>实际加载了多少字节。</returns>\n        public int LoadBinaryFromFileSystem(string binaryAssetName, byte[] buffer)\n        {\n            if (string.IsNullOrEmpty(binaryAssetName))\n            {\n                throw new GameFrameworkException(\"Binary asset name is invalid.\");\n            }\n\n            if (buffer == null)\n            {\n                throw new GameFrameworkException(\"Buffer is invalid.\");\n            }\n\n            return m_ResourceLoader.LoadBinaryFromFileSystem(binaryAssetName, buffer, 0, buffer.Length);\n        }\n\n        /// <summary>\n        /// 从文件系统中加载二进制资源。\n        /// </summary>\n        /// <param name=\"binaryAssetName\">要加载二进制资源的名称。</param>\n        /// <param name=\"buffer\">存储加载二进制资源的二进制流。</param>\n        /// <param name=\"startIndex\">存储加载二进制资源的二进制流的起始位置。</param>\n        /// <returns>实际加载了多少字节。</returns>\n        public int LoadBinaryFromFileSystem(string binaryAssetName, byte[] buffer, int startIndex)\n        {\n            if (string.IsNullOrEmpty(binaryAssetName))\n            {\n                throw new GameFrameworkException(\"Binary asset name is invalid.\");\n            }","sourceCodeStart":1867,"sourceCodeEnd":1903,"githubUrl":"https://github.com/EllanJiang/GameFramework/blob/d0c010b05167c58e92350449d04864a91ca13fd2/GameFramework/Resource/ResourceManager.cs#L1867-L1903","documentation":"The LoadBinaryFromFileSystem(string, byte[]) overload throws this GameFrameworkException when the buffer argument is null. The buffer is the caller-allocated destination for the binary data, so a null buffer makes the read impossible. The asset-name check has already passed at this point; only the null buffer is the problem.","triggerScenarios":"Calling LoadBinaryFromFileSystem(binaryAssetName, buffer) with buffer == null — e.g. an uninitialized array field, a helper method forwarding a null parameter, or a lazily-allocated buffer that was never allocated before the first read.","commonSituations":"Buffer sized from a header length that was never read; a pooled buffer returned null when the pool was empty; refactoring changed the buffer from a constructor-assigned field to one assigned later, but the load ran first.","solutions":["Allocate the buffer before the call, e.g. new byte[expectedSize].","Assert buffer != null (and buffer.Length >= expected size) before calling LoadBinaryFromFileSystem.","Fix the initialization/pooling path that left the buffer null.","Use the overload that allocates and returns the byte[] itself if you don't need to reuse a buffer."],"exampleFix":"// before\nint read = resourceManager.LoadBinaryFromFileSystem(name, buffer); // buffer is null\n// after\nif (buffer == null)\n{\n    buffer = new byte[expectedSize];\n}\nint read = resourceManager.LoadBinaryFromFileSystem(name, buffer, 0, buffer.Length);","handlingStrategy":"validation","validationCode":"if (buffer == null)\n    buffer = new byte[expectedSize];\nelse if (buffer.Length < expectedSize)\n    throw new InvalidOperationException($\"Buffer too small: {buffer.Length} < {expectedSize}\");","typeGuard":"bool IsUsableBuffer(byte[] buf) => buf != null && buf.Length > 0;","tryCatchPattern":"try\n{\n    bytesRead = resourceManager.LoadBinaryFromFileSystem(binaryAssetName, buffer);\n}\ncatch (GameFrameworkException ex) when (ex.Message.Contains(\"Buffer\"))\n{\n    buffer = new byte[expectedSize];\n    bytesRead = resourceManager.LoadBinaryFromFileSystem(binaryAssetName, buffer);\n}","preventionTips":["Allocate buffers immediately before use rather than lazily.","Check buffer != null and buffer.Length >= expected size at the call site.","Prefer the allocating overload LoadBinaryFromFileSystem(name) unless buffer reuse is a measured need.","Ensure buffer pools return real buffers or throw instead of returning null."],"tags":["csharp","unity","null-argument","buffer","resource-loading"],"backgroundTag":"null-argument","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"}