EllanJiang/GameFramework · error · GameFrameworkException

Offset is invalid.

Error message

Offset is invalid.

What it means

DownloadAgentHelperUpdateBytesEventArgs.Create validates the byte buffer, offset, and length before wrapping them into the event args. This error means the offset passed to Create is negative or points past the end of the byte array, so no valid slice of the buffer could be described. It guards the download pipeline from reading out-of-bounds when helpers report received data.

Solutions

  1. Check the offset value passed to Create is >= 0 and < bytes.Length before calling it
  2. Fix the custom IDownloadAgentHelper's UpdateBytes/complete callback so it passes the actual start index of received data
  3. Verify the byte buffer is non-empty and the offset refers to the first unread byte, not the next write position

Example fix

// before
args = DownloadAgentHelperUpdateBytesEventArgs.Create(buffer, buffer.Length, count);
// after
args = DownloadAgentHelperUpdateBytesEventArgs.Create(buffer, 0, count);
Defensive patterns

Strategy: validation

Validate before calling

if (bytes == null || offset < 0 || offset >= bytes.Length) throw new ArgumentOutOfRangeException(nameof(offset));

Type guard

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

Try / catch

try { var args = DownloadAgentHelperUpdateBytesEventArgs.Create(bytes, offset, length); } catch (GameFrameworkException ex) { Log.Warning("Invalid byte slice for download event: {0}", ex.Message); }

Prevention

When it happens

Trigger: Calling DownloadAgentHelperUpdateBytesEventArgs.Create with offset < 0, or offset >= bytes.Length (e.g. Create(bytes, bytes.Length, n) or Create(bytes, -1, n)).

Common situations: Custom IDownloadAgentHelper implementations that report a byte range using an off-by-one offset, passing a zero-length buffer, or using an offset computed from a failed read that returned -1.

Related errors


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

Appendix: source

Thrown at GameFramework/Download/DownloadAgentHelperUpdateBytesEventArgs.cs:61

        }

        /// <summary>
        /// 创建下载代理辅助器更新数据流事件。
        /// </summary>
        /// <param name="bytes">下载的数据流。</param>
        /// <param name="offset">数据流的偏移。</param>
        /// <param name="length">数据流的长度。</param>
        /// <returns>创建的下载代理辅助器更新数据流事件。</returns>
        public static DownloadAgentHelperUpdateBytesEventArgs Create(byte[] bytes, int offset, int length)
        {
            if (bytes == null)
            {
                throw new GameFrameworkException("Bytes is invalid.");
            }

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

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

            DownloadAgentHelperUpdateBytesEventArgs downloadAgentHelperUpdateBytesEventArgs = ReferencePool.Acquire<DownloadAgentHelperUpdateBytesEventArgs>();
            downloadAgentHelperUpdateBytesEventArgs.m_Bytes = bytes;
            downloadAgentHelperUpdateBytesEventArgs.Offset = offset;
            downloadAgentHelperUpdateBytesEventArgs.Length = length;
            return downloadAgentHelperUpdateBytesEventArgs;
        }

        /// <summary>
        /// 清理下载代理辅助器更新数据流事件。
        /// </summary>
        public override void Clear()

View on GitHub (pinned to d0c010b051)