EllanJiang/GameFramework · error · GameFrameworkException
Start index is invalid.
Error message
Start index is invalid.
What it means
Utility.Converter.GetBytes(bool, byte[], int) throws this when startIndex is negative or startIndex + 1 exceeds buffer.Length, meaning there is not enough room to write the 1-byte bool. The check guarantees the write buffer[startIndex] = value ? (byte)1 : (byte)0 stays in bounds.
Solutions
- Ensure the buffer is at least startIndex + 1 bytes long before the call.
- Validate startIndex >= 0 and startIndex < buffer.Length in the caller.
- Fix offset arithmetic in the serialization loop (reserve size for each field).
- If the buffer may be too small, grow it (e.g. Array.Resize) or allocate a larger scratch buffer.
Example fix
// before int offset = buffer.Length; Utility.Converter.GetBytes(value, buffer, offset); // after int offset = buffer.Length; if (offset + 1 > buffer.Length) Array.Resize(ref buffer, offset + 1); Utility.Converter.GetBytes(value, buffer, offset);
Defensive patterns
Strategy: validation
Validate before calling
if (startIndex < 0 || startIndex + 1 > buffer.Length) throw new ArgumentException("startIndex out of range for bool write", nameof(startIndex)); Type guard
bool CanWriteAt(byte[] buffer, int startIndex, int size) => buffer != null && (uint)startIndex <= (uint)(buffer.Length - size);
Try / catch
try { Utility.Converter.GetBytes(value, buffer, offset); offset += 1; }
catch (GameFrameworkException ex) { /* grow buffer or abort packet */ } Prevention
- Reserve exact byte sizes per field (1 for bool) when advancing offsets
- Pre-compute total message size before packing
- Assert offset bounds in debug builds
When it happens
Trigger: Calling GetBytes(bool, buffer, startIndex) where startIndex < 0, or startIndex == buffer.Length or greater (e.g. writing at index 0 of an empty array, or reusing an offset past the buffer end).
Common situations: Cursor-style serialization loops where an offset counter runs past the buffer end; off-by-one errors when reserving space; passing the buffer's Length as startIndex instead of the last valid index.
Related errors
- Serialized packet failure.
- Buffer is invalid.
- Can not convert to JSON with exception
- Offset or length is invalid.
- Start index or length is invalid.
AI-assisted analysis of EllanJiang/GameFramework@d0c010b051 (2026-09-15).
Data as JSON: /api/errors/0ab8f797fb746701.
Report an issue: GitHub.
Appendix: source
Thrown at GameFramework/Utility/Utility.Converter.cs:140
GetBytes(value, buffer, 0);
}
/// <summary>
/// 以字节数组的形式获取指定的布尔值。
/// </summary>
/// <param name="value">要转换的布尔值。</param>
/// <param name="buffer">用于存放结果的字节数组。</param>
/// <param name="startIndex">buffer 内的起始位置。</param>
public static void GetBytes(bool value, byte[] buffer, int startIndex)
{
if (buffer == null)
{
throw new GameFrameworkException("Buffer is invalid.");
}
if (startIndex < 0 || startIndex + 1 > buffer.Length)
{
throw new GameFrameworkException("Start index is invalid.");
}
buffer[startIndex] = value ? (byte)1 : (byte)0;
}
/// <summary>
/// 返回由字节数组中首字节转换来的布尔值。
/// </summary>
/// <param name="value">字节数组。</param>
/// <returns>如果 value 中的首字节非零,则为 true,否则为 false。</returns>
public static bool GetBoolean(byte[] value)
{
return BitConverter.ToBoolean(value, 0);
}
/// <summary>
/// 返回由字节数组中指定位置的一个字节转换来的布尔值。
/// </summary>View on GitHub (pinned to d0c010b051)