EllanJiang/GameFramework · error · GameFrameworkException

Start index or length is invalid.

Error message

Start index or length is invalid.

What it means

ParseData(byte[], int, int, object) throws GameFrameworkException('Start index or length is invalid.') when startIndex < 0, length < 0, or startIndex + length exceeds dataBytes.Length. The (startIndex, length) pair must describe a valid in-bounds slice of the buffer before it is handed to the data provider helper.

Solutions

  1. Clamp/validate: ensure startIndex >= 0, length >= 0 and startIndex + length <= dataBytes.Length before calling ParseData (Math.Min against remaining bytes works well).
  2. Fix the offset/length computation, e.g. pass dataBytes.Length - startIndex as length instead of the full buffer length.
  3. For header-driven formats, verify the declared sizes against the actual buffer size and treat mismatches as corrupt data.
  4. If the file is truncated, re-download/re-export the data file rather than forcing a parse.

Example fix

// before
table.ParseData(buffer, offset, buffer.Length, userData); // may overrun
// after
int length = Math.Min(buffer.Length - offset, declaredLength);
if (offset < 0 || length < 0 || offset + length > buffer.Length)
{
    throw new IOException("Invalid data slice.");
}
table.ParseData(buffer, offset, length, userData);
Defensive patterns

Strategy: validation

Validate before calling

bool IsValidSlice(byte[] b, int start, int len) => start >= 0 && len >= 0 && start + len <= b.Length;

Type guard

bool InBounds(byte[] b, int start, int len) => b != null && (uint)start <= (uint)b.Length && len >= 0 && start + len <= b.Length;

Try / catch

try { provider.ParseData(bytes, start, len, userData); } catch (GameFrameworkException ex) when (ex.Message.Contains("Start index or length")) { /* log offset math bug */ }

Prevention

When it happens

Trigger: Passing a negative startIndex or length; passing a length larger than dataBytes.Length - startIndex (e.g. using the full-file length with an offset into the buffer); slicing a bigger buffer with a miscomputed segment size; reading a declared record length from a corrupt/short payload.

Common situations: Parsing multiple records out of one buffer where per-record offsets/sizes are read from headers that are wrong or truncated; off-by-one errors in offset arithmetic; loading truncated files whose header claims more bytes than were actually read.

Related errors


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

Appendix: source

Thrown at GameFramework/Base/DataProvider/DataProvider.cs:356

        /// <param name="startIndex">内容二进制流的起始位置。</param>
        /// <param name="length">内容二进制流的长度。</param>
        /// <param name="userData">用户自定义数据。</param>
        /// <returns>是否解析内容成功。</returns>
        public bool ParseData(byte[] dataBytes, int startIndex, int length, object userData)
        {
            if (m_DataProviderHelper == null)
            {
                throw new GameFrameworkException("You must set data helper first.");
            }

            if (dataBytes == null)
            {
                throw new GameFrameworkException("Data bytes is invalid.");
            }

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

            try
            {
                return m_DataProviderHelper.ParseData(m_Owner, dataBytes, startIndex, length, userData);
            }
            catch (Exception exception)
            {
                if (exception is GameFrameworkException)
                {
                    throw;
                }

                throw new GameFrameworkException(Utility.Text.Format("Can not parse data bytes with exception '{0}'.", exception), exception);
            }
        }

        /// <summary>

View on GitHub (pinned to d0c010b051)