EllanJiang/GameFramework · error · GameFrameworkException

Start index or length is invalid.

Error message

Start index or length is invalid.

What it means

Utility.Encryption.GetSelfXorBytes throws GameFrameworkException("Start index or length is invalid.") when startIndex < 0, length < 0, or startIndex + length exceeds bytes.Length — i.e. the requested range does not fit in the buffer. The library validates the slice bounds before performing the XOR loop to avoid corrupting memory or throwing IndexOutOfRangeException mid-operation.

Solutions

  1. Clamp or recompute startIndex/length so 0 <= startIndex and startIndex + length <= bytes.Length.
  2. Verify the offsets/lengths used — especially if they were parsed from the data — before calling.
  3. Catch GameFrameworkException around the call when handling untrusted data, and reject the buffer instead.

Example fix

// before
Utility.Converter.GetSelfXorBytes(data, headerLen, payloadLen, key);
// after
int start = Math.Max(0, headerLen);
int len = Math.Min(payloadLen, data.Length - start);
if (len > 0) Utility.Converter.GetSelfXorBytes(data, start, len, key);
Defensive patterns

Strategy: validation

Validate before calling

if (startIndex < 0 || length < 0 || startIndex + length > bytes.Length) throw new ArgumentOutOfRangeException(nameof(startIndex), "Range must satisfy 0 <= startIndex and startIndex + length <= bytes.Length");

Type guard

bool IsValidRange(byte[] bytes, int startIndex, int length) => bytes != null && startIndex >= 0 && length >= 0 && startIndex + length <= bytes.Length;

Try / catch

try { Utility.Converter.GetSelfXorBytes(data, offset, len, key); }
catch (GameFrameworkException ex) { Log.Warn("XOR range invalid: {0}", ex.Message); /* treat payload as corrupt and reject */ }

Prevention

When it happens

Trigger: Calling GetSelfXorBytes with a range outside the buffer, e.g. GetSelfXorBytes(data10, 5, 10, key) (5+10 > 10), a negative offset/length from computed offsets, or passing a smaller buffer than the length captured earlier.

Common situations: Protocol parsing where offset/length were read from the data itself and are wrong or malicious; buffers reallocated to a smaller size after the length was computed; off-by-one errors when stripping headers.

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

Appendix: source

Thrown at GameFramework/Utility/Utility.Encryption.cs:122

                if (bytes == null)
                {
                    return;
                }

                if (code == null)
                {
                    throw new GameFrameworkException("Code is invalid.");
                }

                int codeLength = code.Length;
                if (codeLength <= 0)
                {
                    throw new GameFrameworkException("Code length is invalid.");
                }

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

                int codeIndex = startIndex % codeLength;
                for (int i = startIndex; i < length; i++)
                {
                    bytes[i] ^= code[codeIndex++];
                    codeIndex %= codeLength;
                }
            }
        }
    }
}

View on GitHub (pinned to d0c010b051)