EllanJiang/GameFramework · error · GameFrameworkException

Buffer length is not enough.

Error message

Buffer length is not enough.

What it means

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.

Solutions

  1. Verify buffer.Length >= startIndex + structureSize before calling, and use Marshal.SizeOf<T> as structureSize.
  2. Check for truncation upstream: validate the total length of packets/files before parsing.
  3. Align binary format versions — include a version/size header in the data or bump the format version check.

Example fix

// before
var header = Utility.Marshal.BytesToStructure<Header>(32, data, 4); // data.Length == 20
// after
if (data.Length < 4 + Marshal.SizeOf<Header>())
    throw new InvalidDataException("Buffer too small for header");
var header = Utility.Marshal.BytesToStructure<Header>(Marshal.SizeOf<Header>(), data, 4);
Defensive patterns

Strategy: validation

Validate before calling

int size = Marshal.SizeOf<T>();
if (buffer == null || buffer.Length < startIndex + size)
    throw new InvalidDataException($"Buffer too small: need {startIndex + size}, have {buffer?.Length ?? 0}");
var value = Utility.Marshal.BytesToStructure<T>(size, buffer, startIndex);

Try / catch

try
{
    var v = Utility.Marshal.BytesToStructure<T>(size, buffer, offset);
}
catch (GameFrameworkException ex) when (ex.Message == "Buffer length is not enough.")
{
    // treat as truncated data: reject packet/file and re-request
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Understand the failure class

Background: "value must be between 0 and 1" / "out of range" / "must not be negative" errors: fixing range-validation failures across open-source libraries — this error's family across 42 libraries.

Related errors


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

Appendix: source

Thrown at GameFramework/Utility/Utility.Marshal.cs:231

            {
                if (structureSize < 0)
                {
                    throw new GameFrameworkException("Structure size is invalid.");
                }

                if (buffer == null)
                {
                    throw new GameFrameworkException("Buffer is invalid.");
                }

                if (startIndex < 0)
                {
                    throw new GameFrameworkException("Start index is invalid.");
                }

                if (startIndex + structureSize > buffer.Length)
                {
                    throw new GameFrameworkException("Buffer length is not enough.");
                }

                EnsureCachedHGlobalSize(structureSize);
                System.Runtime.InteropServices.Marshal.Copy(buffer, startIndex, s_CachedHGlobalPtr, structureSize);
                return (T)System.Runtime.InteropServices.Marshal.PtrToStructure(s_CachedHGlobalPtr, typeof(T));
            }
        }
    }
}

View on GitHub (pinned to d0c010b051)