EllanJiang/GameFramework · error · GameFrameworkException

Offset or length is invalid.

Error message

Offset or length is invalid.

What it means

Utility.Verifier.GetCrc32(bytes, offset, length) validates the range after the null check: it throws GameFrameworkException("Offset or length is invalid.") when offset < 0, length < 0, or offset + length exceeds the array length. It prevents reading outside the buffer during CRC hashing.

Solutions

  1. Clamp the range before calling: offset >= 0, length >= 0, offset + length <= bytes.Length
  2. Validate the header/size source that produced the length value
  3. Catch GameFrameworkException and log offset, length, and bytes.Length for diagnosis

Example fix

// before
int crc = Utility.Verifier.GetCrc32(data, offset, size); // size from file header
// after
size = Math.Min(size, data.Length - offset);
int crc = Utility.Verifier.GetCrc32(data, offset, size);
Defensive patterns

Strategy: validation

Validate before calling

if (offset < 0) throw new ArgumentOutOfRangeException(nameof(offset));
if (length < 0 || offset + length > bytes.Length) throw new ArgumentOutOfRangeException(nameof(length));
length = Math.Min(length, bytes.Length - offset);

Type guard

static bool IsInRange(byte[] bytes, int offset, int length) => offset >= 0 && length >= 0 && offset + length <= bytes.Length;

Try / catch

try { return Utility.Verifier.GetCrc32(data, offset, size); }
catch (GameFrameworkException ex) { Log.Warning("Bad CRC range: {0}", ex.Message); return 0; }

Prevention

When it happens

Trigger: Calling GetCrc32(bytes, offset, length) with a negative offset or length, or an offset+length window that overruns bytes.Length — e.g. a length taken from a header that exceeds the actual buffer.

Common situations: Parsing a binary file where a size field was misread; off-by-one in slice computation; stale length value after the buffer was reallocated smaller.

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/a55d691fa5fc1281. Report an issue: GitHub.

Appendix: source

Thrown at GameFramework/Utility/Utility.Verifier.cs:55

            }

            /// <summary>
            /// 计算二进制流的 CRC32。
            /// </summary>
            /// <param name="bytes">指定的二进制流。</param>
            /// <param name="offset">二进制流的偏移。</param>
            /// <param name="length">二进制流的长度。</param>
            /// <returns>计算后的 CRC32。</returns>
            public static int GetCrc32(byte[] bytes, int offset, int length)
            {
                if (bytes == null)
                {
                    throw new GameFrameworkException("Bytes is invalid.");
                }

                if (offset < 0 || length < 0 || offset + length > bytes.Length)
                {
                    throw new GameFrameworkException("Offset or length is invalid.");
                }

                s_Algorithm.HashCore(bytes, offset, length);
                int result = (int)s_Algorithm.HashFinal();
                s_Algorithm.Initialize();
                return result;
            }

            /// <summary>
            /// 计算二进制流的 CRC32。
            /// </summary>
            /// <param name="stream">指定的二进制流。</param>
            /// <returns>计算后的 CRC32。</returns>
            public static int GetCrc32(Stream stream)
            {
                if (stream == null)
                {
                    throw new GameFrameworkException("Stream is invalid.");

View on GitHub (pinned to d0c010b051)