EllanJiang/GameFramework · error · GameFrameworkException

Buffer is invalid.

Error message

Buffer is invalid.

What it means

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.

Solutions

  1. Allocate a buffer before the call: ReadFile(name, new byte[expectedLength]);
  2. Null-check or lazily initialize the buffer member before reading.
  3. Consider the name-only overload ReadFile(name) which allocates and returns the byte array itself.

Example fix

// before
byte[] buffer = null;
int read = fileSystem.ReadFile(name, buffer);
// after
byte[] buffer = new byte[fileSystem.GetFileInfo(name).Length];
int read = fileSystem.ReadFile(name, buffer);
Defensive patterns

Strategy: validation

Validate before calling

if (buffer != null) { int read = fileSystem.ReadFile(name, buffer); }

Type guard

bool IsValidBuffer(byte[] buffer) => buffer != null;

Try / catch

try { read = fileSystem.ReadFile(name, buffer); } catch (GameFrameworkException ex) { Log.Error("ReadFile buffer invalid: {0}", ex.Message); }

Prevention

When it happens

Trigger: Calling ReadFile(name, null) — the two-argument overload with a null byte[] buffer.

Common situations: A buffer field never initialized; a factory method returning null when allocation failed; uninitialized member reused across calls.

Related errors


AI-assisted analysis of EllanJiang/GameFramework@d0c010b051 (2026-09-15). Data as JSON: /api/errors/735910fc1dbe20f4. Report an issue: GitHub.

Appendix: source

Thrown at GameFramework/FileSystem/FileSystem.cs:371

            {
                m_Stream.Position = fileInfo.Offset;
                m_Stream.Read(buffer, 0, length);
            }

            return buffer;
        }

        /// <summary>
        /// 读取指定文件。
        /// </summary>
        /// <param name="name">要读取的文件名称。</param>
        /// <param name="buffer">存储读取文件内容的二进制流。</param>
        /// <returns>实际读取了多少字节。</returns>
        public int ReadFile(string name, byte[] buffer)
        {
            if (buffer == null)
            {
                throw new GameFrameworkException("Buffer is invalid.");
            }

            return ReadFile(name, buffer, 0, buffer.Length);
        }

        /// <summary>
        /// 读取指定文件。
        /// </summary>
        /// <param name="name">要读取的文件名称。</param>
        /// <param name="buffer">存储读取文件内容的二进制流。</param>
        /// <param name="startIndex">存储读取文件内容的二进制流的起始位置。</param>
        /// <returns>实际读取了多少字节。</returns>
        public int ReadFile(string name, byte[] buffer, int startIndex)
        {
            if (buffer == null)
            {
                throw new GameFrameworkException("Buffer is invalid.");
            }

View on GitHub (pinned to d0c010b051)