{"record":{"id":"9b4be2933938dc1d","repo":"EllanJiang/GameFramework","slug":"length-is-invalid-downloadagenthelperupdatebyteseventargs","errorCode":null,"errorMessage":"Length is invalid.","messagePattern":"Length is invalid\\.","errorType":"exception","errorClass":"GameFrameworkException","httpStatus":null,"severity":"error","filePath":"GameFramework/Download/DownloadAgentHelperUpdateBytesEventArgs.cs","lineNumber":66,"sourceCode":"        /// <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()\n        {\n            m_Bytes = null;\n            Offset = 0;\n            Length = 0;\n        }","sourceCodeStart":48,"sourceCodeEnd":84,"githubUrl":"https://github.com/EllanJiang/GameFramework/blob/d0c010b05167c58e92350449d04864a91ca13fd2/GameFramework/Download/DownloadAgentHelperUpdateBytesEventArgs.cs#L48-L84","documentation":"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.","triggerScenarios":"Calling DownloadAgentHelperUpdateBytesEventArgs.Create with length <= 0, or with offset + length > bytes.Length (e.g. Create(buffer, buffer.Length - 2, 5)).","commonSituations":"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.","solutions":["Ensure length is > 0 and offset + length <= bytes.Length before calling Create","Use the return value of the underlying read operation as the length, not the requested count","Clamp length to Math.Min(requested, bytes.Length - offset)"],"exampleFix":"// before\nint length = requested;\nargs = DownloadAgentHelperUpdateBytesEventArgs.Create(buffer, offset, length);\n// after\nint length = Math.Min(requested, buffer.Length - offset);\nif (length > 0) args = DownloadAgentHelperUpdateBytesEventArgs.Create(buffer, offset, length);","handlingStrategy":"validation","validationCode":"if (bytes == null || length <= 0 || offset + length > bytes.Length) throw new ArgumentOutOfRangeException(nameof(length));","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 download byte length: {0}\", ex.Message); }","preventionTips":["Use Stream.Read's return value as length, and skip the event when it is 0","Clamp length to buffer bounds before reporting","Write tests for helpers that deliver data in chunks"],"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"}