EllanJiang/GameFramework · error · GameFrameworkException

Buffer is invalid.

Error message

Buffer is invalid.

What it means

Utility.Converter.GetBytes(bool, byte[], int) throws this when the caller passes a null buffer array. The library requires a pre-allocated byte array to write the encoded bool into; it never allocates on the caller's behalf. The GameFrameworkException is thrown before any bounds check or write occurs.

Solutions

  1. Initialize the byte[] before calling GetBytes, e.g. new byte[1] for a bool.
  2. Add a null check (or Debug.Assert) on the buffer before the call.
  3. Check the code path that produces the buffer to find why it returns null instead of throwing earlier.
  4. Wrap the call in try-catch for defensive paths that cannot be validated ahead of time.

Example fix

// before
byte[] buffer = GetScratchBuffer(); // may return null
Utility.Converter.GetBytes(value, buffer, 0);
// after
byte[] buffer = GetScratchBuffer() ?? new byte[1];
Utility.Converter.GetBytes(value, buffer, 0);
Defensive patterns

Strategy: validation

Validate before calling

if (buffer == null) throw new ArgumentNullException(nameof(buffer));
if (buffer.Length < startIndex + 1) throw new ArgumentException("Buffer too small", nameof(buffer));

Type guard

bool IsValidBuffer(byte[] buffer, int startIndex, int size) => buffer != null && startIndex >= 0 && startIndex + size <= buffer.Length;

Try / catch

try { Utility.Converter.GetBytes(value, buffer, startIndex); }
catch (GameFrameworkException ex) { /* log and re-init buffer */ }

Prevention

When it happens

Trigger: Calling Utility.Converter.GetBytes(bool value, null, int startIndex), typically when the buffer variable was never initialized or a method returned null where a byte[] was expected.

Common situations: Hand-rolled serialization code where the byte[] is produced by another helper that returned null on failure; refactoring that removed buffer initialization; passing a property that lazily initializes and can return null.

Related errors


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

Appendix: source

Thrown at GameFramework/Utility/Utility.Converter.cs:135

            /// </summary>
            /// <param name="value">要转换的布尔值。</param>
            /// <param name="buffer">用于存放结果的字节数组。</param>
            public static void GetBytes(bool value, byte[] buffer)
            {
                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);

View on GitHub (pinned to d0c010b051)