{"record":{"id":"a55d691fa5fc1281","repo":"EllanJiang/GameFramework","slug":"offset-or-length-is-invalid-utility-verifier","errorCode":null,"errorMessage":"Offset or length is invalid.","messagePattern":"Offset or length is invalid\\.","errorType":"exception","errorClass":"GameFrameworkException","httpStatus":null,"severity":"error","filePath":"GameFramework/Utility/Utility.Verifier.cs","lineNumber":55,"sourceCode":"            }\n\n            /// <summary>\n            /// 计算二进制流的 CRC32。\n            /// </summary>\n            /// <param name=\"bytes\">指定的二进制流。</param>\n            /// <param name=\"offset\">二进制流的偏移。</param>\n            /// <param name=\"length\">二进制流的长度。</param>\n            /// <returns>计算后的 CRC32。</returns>\n            public static int GetCrc32(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 || length < 0 || offset + length > bytes.Length)\n                {\n                    throw new GameFrameworkException(\"Offset or length is invalid.\");\n                }\n\n                s_Algorithm.HashCore(bytes, offset, length);\n                int result = (int)s_Algorithm.HashFinal();\n                s_Algorithm.Initialize();\n                return result;\n            }\n\n            /// <summary>\n            /// 计算二进制流的 CRC32。\n            /// </summary>\n            /// <param name=\"stream\">指定的二进制流。</param>\n            /// <returns>计算后的 CRC32。</returns>\n            public static int GetCrc32(Stream stream)\n            {\n                if (stream == null)\n                {\n                    throw new GameFrameworkException(\"Stream is invalid.\");","sourceCodeStart":37,"sourceCodeEnd":73,"githubUrl":"https://github.com/EllanJiang/GameFramework/blob/d0c010b05167c58e92350449d04864a91ca13fd2/GameFramework/Utility/Utility.Verifier.cs#L37-L73","documentation":"Utility.Verifier.GetCrc32(bytes, offset, length) validates the range after the null check: it throws GameFrameworkException(\"Offset or length is invalid.\") when offset < 0, length < 0, or offset + length exceeds the array length. It prevents reading outside the buffer during CRC hashing.","triggerScenarios":"Calling GetCrc32(bytes, offset, length) with a negative offset or length, or an offset+length window that overruns bytes.Length — e.g. a length taken from a header that exceeds the actual buffer.","commonSituations":"Parsing a binary file where a size field was misread; off-by-one in slice computation; stale length value after the buffer was reallocated smaller.","solutions":["Clamp the range before calling: offset >= 0, length >= 0, offset + length <= bytes.Length","Validate the header/size source that produced the length value","Catch GameFrameworkException and log offset, length, and bytes.Length for diagnosis"],"exampleFix":"// before\nint crc = Utility.Verifier.GetCrc32(data, offset, size); // size from file header\n// after\nsize = Math.Min(size, data.Length - offset);\nint crc = Utility.Verifier.GetCrc32(data, offset, size);","handlingStrategy":"validation","validationCode":"if (offset < 0) throw new ArgumentOutOfRangeException(nameof(offset));\nif (length < 0 || offset + length > bytes.Length) throw new ArgumentOutOfRangeException(nameof(length));\nlength = Math.Min(length, bytes.Length - offset);","typeGuard":"static bool IsInRange(byte[] bytes, int offset, int length) => offset >= 0 && length >= 0 && offset + length <= bytes.Length;","tryCatchPattern":"try { return Utility.Verifier.GetCrc32(data, offset, size); }\ncatch (GameFrameworkException ex) { Log.Warning(\"Bad CRC range: {0}\", ex.Message); return 0; }","preventionTips":["Clamp lengths read from untrusted headers","Log offset/length/buffer-length when a range check fails","Use helper methods for slicing instead of manual arithmetic"],"tags":["argument-out-of-range","checksum","crc32","gameframework"],"backgroundTag":"value-out-of-range","analyzedSha":"d0c010b05167c58e92350449d04864a91ca13fd2","analyzedAt":"2026-09-15T13:37:15.352Z","contentChangedAt":"2026-09-15T13:37:15.352Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}