{"record":{"id":"2829e3fb905d158b","repo":"EllanJiang/GameFramework","slug":"offset-is-invalid","errorCode":null,"errorMessage":"Offset is invalid.","messagePattern":"Offset is invalid\\.","errorType":"exception","errorClass":"GameFrameworkException","httpStatus":null,"severity":"error","filePath":"GameFramework/Download/DownloadAgentHelperUpdateBytesEventArgs.cs","lineNumber":61,"sourceCode":"        }\n\n        /// <summary>\n        /// 创建下载代理辅助器更新数据流事件。\n        /// </summary>\n        /// <param name=\"bytes\">下载的数据流。</param>\n        /// <param name=\"offset\">数据流的偏移。</param>\n        /// <param name=\"length\">数据流的长度。</param>\n        /// <returns>创建的下载代理辅助器更新数据流事件。</returns>\n        public static DownloadAgentHelperUpdateBytesEventArgs Create(byte[] bytes, int offset, int length)\n        {\n            if (bytes == null)\n            {\n                throw new GameFrameworkException(\"Bytes is invalid.\");\n            }\n\n            if (offset < 0 || offset >= bytes.Length)\n            {\n                throw new GameFrameworkException(\"Offset is invalid.\");\n            }\n\n            if (length <= 0 || offset + length > bytes.Length)\n            {\n                throw new GameFrameworkException(\"Length is invalid.\");\n            }\n\n            DownloadAgentHelperUpdateBytesEventArgs downloadAgentHelperUpdateBytesEventArgs = ReferencePool.Acquire<DownloadAgentHelperUpdateBytesEventArgs>();\n            downloadAgentHelperUpdateBytesEventArgs.m_Bytes = bytes;\n            downloadAgentHelperUpdateBytesEventArgs.Offset = offset;\n            downloadAgentHelperUpdateBytesEventArgs.Length = length;\n            return downloadAgentHelperUpdateBytesEventArgs;\n        }\n\n        /// <summary>\n        /// 清理下载代理辅助器更新数据流事件。\n        /// </summary>\n        public override void Clear()","sourceCodeStart":43,"sourceCodeEnd":79,"githubUrl":"https://github.com/EllanJiang/GameFramework/blob/d0c010b05167c58e92350449d04864a91ca13fd2/GameFramework/Download/DownloadAgentHelperUpdateBytesEventArgs.cs#L43-L79","documentation":"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.","triggerScenarios":"Calling DownloadAgentHelperUpdateBytesEventArgs.Create with offset < 0, or offset >= bytes.Length (e.g. Create(bytes, bytes.Length, n) or Create(bytes, -1, n)).","commonSituations":"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.","solutions":["Check the offset value passed to Create is >= 0 and < bytes.Length before calling it","Fix the custom IDownloadAgentHelper's UpdateBytes/complete callback so it passes the actual start index of received data","Verify the byte buffer is non-empty and the offset refers to the first unread byte, not the next write position"],"exampleFix":"// before\nargs = DownloadAgentHelperUpdateBytesEventArgs.Create(buffer, buffer.Length, count);\n// after\nargs = DownloadAgentHelperUpdateBytesEventArgs.Create(buffer, 0, count);","handlingStrategy":"validation","validationCode":"if (bytes == null || offset < 0 || offset >= bytes.Length) throw new ArgumentOutOfRangeException(nameof(offset));","typeGuard":"bool IsValidSlice(byte[] bytes, int offset, int length) => bytes != null && offset >= 0 && length > 0 && offset + length <= bytes.Length;","tryCatchPattern":"try { var args = DownloadAgentHelperUpdateBytesEventArgs.Create(bytes, offset, length); } catch (GameFrameworkException ex) { Log.Warning(\"Invalid byte slice for download event: {0}\", ex.Message); }","preventionTips":["Always slice buffers with offset 0 unless intentionally resuming mid-buffer","Unit-test custom download helpers with empty and partial buffers","Never pass a read function's -1 error return as an offset"],"tags":["download","argument-validation","unity","gameframework"],"backgroundTag":"argument-out-of-range","analyzedSha":"d0c010b05167c58e92350449d04864a91ca13fd2","analyzedAt":"2026-09-15T13:37:15.352Z","contentChangedAt":"2026-09-15T13:37:15.352Z","schemaVersion":2},"datasetVersion":"2026-09-15T23:17:13.987Z"}