{"record":{"id":"a3010dae2ca59ea2","repo":"EllanJiang/GameFramework","slug":"bytes-is-invalid","errorCode":null,"errorMessage":"Bytes is invalid.","messagePattern":"Bytes is invalid\\.","errorType":"exception","errorClass":"GameFrameworkException","httpStatus":null,"severity":"error","filePath":"GameFramework/Download/DownloadAgentHelperUpdateBytesEventArgs.cs","lineNumber":56,"sourceCode":"        /// </summary>\n        public int Length\n        {\n            get;\n            private set;\n        }\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        }","sourceCodeStart":38,"sourceCodeEnd":74,"githubUrl":"https://github.com/EllanJiang/GameFramework/blob/d0c010b05167c58e92350449d04864a91ca13fd2/GameFramework/Download/DownloadAgentHelperUpdateBytesEventArgs.cs#L38-L74","documentation":"DownloadAgentHelperUpdateBytesEventArgs.Create(bytes, offset, length) throws this when the bytes array is null. This event carries a chunk of downloaded stream data to append; there is no valid update without a buffer. (The source region also shows the related 'Offset is invalid.' guard for out-of-range offsets.)","triggerScenarios":"A custom IDownloadAgentHelper passing a null buffer to the update-bytes callback; a read operation that failed and still invoked the update event with null; uninitialized stream buffers in custom helpers.","commonSituations":"Implementing a custom download agent over a network stream where a failed Read returns null; reusing a buffer that was disposed/never allocated.","solutions":["Ensure the helper allocates its byte buffer before downloading and passes it to the update event.","In the helper, skip the update callback (or raise the error callback) when the read produced no buffer.","Validate bytes != null inside the helper before invoking the event."],"exampleFix":"// before (custom IDownloadAgentHelper)\nonUpdateBytes(this, DownloadAgentHelperUpdateBytesEventArgs.Create(buffer, 0, read)); // buffer may be null\n// after\nif (buffer != null)\n    onUpdateBytes(this, DownloadAgentHelperUpdateBytesEventArgs.Create(buffer, 0, read));","handlingStrategy":"type-guard","validationCode":"if (bytes == null || offset < 0 || offset >= bytes.Length) raiseDownloadError();","typeGuard":"static bool IsValidChunk(byte[] b, int offset, int length) => b != null && offset >= 0 && offset < b.Length && length >= 0 && offset + length <= b.Length;","tryCatchPattern":"try { var e = DownloadAgentHelperUpdateBytesEventArgs.Create(bytes, offset, length); }\ncatch (GameFrameworkException) { Log.Error(\"Download helper produced an invalid byte chunk\"); }","preventionTips":["Allocate the download buffer before starting the read loop","Never invoke update events after a failed read; use the error event","Range-check offset/length against the buffer in the helper"],"tags":["download","null-reference","eventargs","csharp"],"backgroundTag":"null-argument","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"}