EllanJiang/GameFramework · error · GameFrameworkException

Length is invalid.

Error message

Length is invalid.

What it means

DownloadAgentHelperUpdateBytesEventArgs.Create validates the reported byte-range length. This error means length <= 0 or offset + length exceeds the byte array bounds, i.e. the event would describe an invalid or out-of-bounds slice of the buffer.

Solutions

  1. Ensure length is > 0 and offset + length <= bytes.Length before calling Create
  2. Use the return value of the underlying read operation as the length, not the requested count
  3. Clamp length to Math.Min(requested, bytes.Length - offset)

Example fix

// before
int length = requested;
args = DownloadAgentHelperUpdateBytesEventArgs.Create(buffer, offset, length);
// after
int length = Math.Min(requested, buffer.Length - offset);
if (length > 0) args = DownloadAgentHelperUpdateBytesEventArgs.Create(buffer, offset, length);
Defensive patterns

Strategy: validation

Validate before calling

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

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 download byte length: {0}", ex.Message); }

Prevention

When it happens

Trigger: Calling DownloadAgentHelperUpdateBytesEventArgs.Create with length <= 0, or with offset + length > bytes.Length (e.g. Create(buffer, buffer.Length - 2, 5)).

Common situations: Custom download agent helpers passing a read count of 0 after a no-op read, misreading Stream.Read's return value, or computing length from a wrong buffer size after a version change in the helper API.

Related errors


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

Appendix: source

Thrown at GameFramework/Download/DownloadAgentHelperUpdateBytesEventArgs.cs:66

        /// <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()
        {
            m_Bytes = null;
            Offset = 0;
            Length = 0;
        }

View on GitHub (pinned to d0c010b051)