{"record":{"id":"735910fc1dbe20f4","repo":"EllanJiang/GameFramework","slug":"buffer-is-invalid","errorCode":null,"errorMessage":"Buffer is invalid.","messagePattern":"Buffer is invalid\\.","errorType":"validation","errorClass":"GameFrameworkException","httpStatus":null,"severity":"error","filePath":"GameFramework/FileSystem/FileSystem.cs","lineNumber":371,"sourceCode":"            {\n                m_Stream.Position = fileInfo.Offset;\n                m_Stream.Read(buffer, 0, length);\n            }\n\n            return buffer;\n        }\n\n        /// <summary>\n        /// 读取指定文件。\n        /// </summary>\n        /// <param name=\"name\">要读取的文件名称。</param>\n        /// <param name=\"buffer\">存储读取文件内容的二进制流。</param>\n        /// <returns>实际读取了多少字节。</returns>\n        public int ReadFile(string name, byte[] buffer)\n        {\n            if (buffer == null)\n            {\n                throw new GameFrameworkException(\"Buffer is invalid.\");\n            }\n\n            return ReadFile(name, buffer, 0, buffer.Length);\n        }\n\n        /// <summary>\n        /// 读取指定文件。\n        /// </summary>\n        /// <param name=\"name\">要读取的文件名称。</param>\n        /// <param name=\"buffer\">存储读取文件内容的二进制流。</param>\n        /// <param name=\"startIndex\">存储读取文件内容的二进制流的起始位置。</param>\n        /// <returns>实际读取了多少字节。</returns>\n        public int ReadFile(string name, byte[] buffer, int startIndex)\n        {\n            if (buffer == null)\n            {\n                throw new GameFrameworkException(\"Buffer is invalid.\");\n            }","sourceCodeStart":353,"sourceCodeEnd":389,"githubUrl":"https://github.com/EllanJiang/GameFramework/blob/d0c010b05167c58e92350449d04864a91ca13fd2/GameFramework/FileSystem/FileSystem.cs#L353-L389","documentation":"ReadFile(string, byte[]) reads file content into the caller-provided buffer. A null buffer has no capacity to receive data, so the library throws GameFrameworkException(\"Buffer is invalid.\"). Provide a non-null byte[]; this overload delegates to the (name, buffer, 0, buffer.Length) overload with the full buffer length.","triggerScenarios":"Calling ReadFile(name, null) — the two-argument overload with a null byte[] buffer.","commonSituations":"A buffer field never initialized; a factory method returning null when allocation failed; uninitialized member reused across calls.","solutions":["Allocate a buffer before the call: ReadFile(name, new byte[expectedLength]);","Null-check or lazily initialize the buffer member before reading.","Consider the name-only overload ReadFile(name) which allocates and returns the byte array itself."],"exampleFix":"// before\nbyte[] buffer = null;\nint read = fileSystem.ReadFile(name, buffer);\n// after\nbyte[] buffer = new byte[fileSystem.GetFileInfo(name).Length];\nint read = fileSystem.ReadFile(name, buffer);","handlingStrategy":"validation","validationCode":"if (buffer != null) { int read = fileSystem.ReadFile(name, buffer); }","typeGuard":"bool IsValidBuffer(byte[] buffer) => buffer != null;","tryCatchPattern":"try { read = fileSystem.ReadFile(name, buffer); } catch (GameFrameworkException ex) { Log.Error(\"ReadFile buffer invalid: {0}\", ex.Message); }","preventionTips":["Allocate buffers at acquisition time, not lazily.","Prefer the name-only overload unless a reusable buffer is required.","Initialize buffer fields in constructors."],"tags":["csharp","null-argument","buffer"],"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"}