{"record":{"id":"0ab8f797fb746701","repo":"EllanJiang/GameFramework","slug":"start-index-is-invalid","errorCode":null,"errorMessage":"Start index is invalid.","messagePattern":"Start index is invalid\\.","errorType":"exception","errorClass":"GameFrameworkException","httpStatus":null,"severity":"error","filePath":"GameFramework/Utility/Utility.Converter.cs","lineNumber":140,"sourceCode":"                GetBytes(value, buffer, 0);\n            }\n\n            /// <summary>\n            /// 以字节数组的形式获取指定的布尔值。\n            /// </summary>\n            /// <param name=\"value\">要转换的布尔值。</param>\n            /// <param name=\"buffer\">用于存放结果的字节数组。</param>\n            /// <param name=\"startIndex\">buffer 内的起始位置。</param>\n            public static void GetBytes(bool value, byte[] buffer, int startIndex)\n            {\n                if (buffer == null)\n                {\n                    throw new GameFrameworkException(\"Buffer is invalid.\");\n                }\n\n                if (startIndex < 0 || startIndex + 1 > buffer.Length)\n                {\n                    throw new GameFrameworkException(\"Start index is invalid.\");\n                }\n\n                buffer[startIndex] = value ? (byte)1 : (byte)0;\n            }\n\n            /// <summary>\n            /// 返回由字节数组中首字节转换来的布尔值。\n            /// </summary>\n            /// <param name=\"value\">字节数组。</param>\n            /// <returns>如果 value 中的首字节非零，则为 true，否则为 false。</returns>\n            public static bool GetBoolean(byte[] value)\n            {\n                return BitConverter.ToBoolean(value, 0);\n            }\n\n            /// <summary>\n            /// 返回由字节数组中指定位置的一个字节转换来的布尔值。\n            /// </summary>","sourceCodeStart":122,"sourceCodeEnd":158,"githubUrl":"https://github.com/EllanJiang/GameFramework/blob/d0c010b05167c58e92350449d04864a91ca13fd2/GameFramework/Utility/Utility.Converter.cs#L122-L158","documentation":"Utility.Converter.GetBytes(bool, byte[], int) throws this when startIndex is negative or startIndex + 1 exceeds buffer.Length, meaning there is not enough room to write the 1-byte bool. The check guarantees the write buffer[startIndex] = value ? (byte)1 : (byte)0 stays in bounds.","triggerScenarios":"Calling GetBytes(bool, buffer, startIndex) where startIndex < 0, or startIndex == buffer.Length or greater (e.g. writing at index 0 of an empty array, or reusing an offset past the buffer end).","commonSituations":"Cursor-style serialization loops where an offset counter runs past the buffer end; off-by-one errors when reserving space; passing the buffer's Length as startIndex instead of the last valid index.","solutions":["Ensure the buffer is at least startIndex + 1 bytes long before the call.","Validate startIndex >= 0 and startIndex < buffer.Length in the caller.","Fix offset arithmetic in the serialization loop (reserve size for each field).","If the buffer may be too small, grow it (e.g. Array.Resize) or allocate a larger scratch buffer."],"exampleFix":"// before\nint offset = buffer.Length;\nUtility.Converter.GetBytes(value, buffer, offset);\n// after\nint offset = buffer.Length;\nif (offset + 1 > buffer.Length) Array.Resize(ref buffer, offset + 1);\nUtility.Converter.GetBytes(value, buffer, offset);","handlingStrategy":"validation","validationCode":"if (startIndex < 0 || startIndex + 1 > buffer.Length) throw new ArgumentException(\"startIndex out of range for bool write\", nameof(startIndex));","typeGuard":"bool CanWriteAt(byte[] buffer, int startIndex, int size) => buffer != null && (uint)startIndex <= (uint)(buffer.Length - size);","tryCatchPattern":"try { Utility.Converter.GetBytes(value, buffer, offset); offset += 1; }\ncatch (GameFrameworkException ex) { /* grow buffer or abort packet */ }","preventionTips":["Reserve exact byte sizes per field (1 for bool) when advancing offsets","Pre-compute total message size before packing","Assert offset bounds in debug builds"],"tags":["argument-out-of-range","serialization","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"}