{"record":{"id":"df7d2a35c3a5aba6","repo":"EllanJiang/GameFramework","slug":"code-length-is-invalid-utility-verifier","errorCode":null,"errorMessage":"Code length is invalid.","messagePattern":"Code length is invalid\\.","errorType":"exception","errorClass":"GameFrameworkException","httpStatus":null,"severity":"error","filePath":"GameFramework/Utility/Utility.Verifier.cs","lineNumber":154,"sourceCode":"                bytes[offset + 3] = (byte)(crc32 & 0xff);\n            }\n\n            internal static int GetCrc32(Stream stream, byte[] code, int length)\n            {\n                if (stream == null)\n                {\n                    throw new GameFrameworkException(\"Stream is invalid.\");\n                }\n\n                if (code == null)\n                {\n                    throw new GameFrameworkException(\"Code is invalid.\");\n                }\n\n                int codeLength = code.Length;\n                if (codeLength <= 0)\n                {\n                    throw new GameFrameworkException(\"Code length is invalid.\");\n                }\n\n                int bytesLength = (int)stream.Length;\n                if (length < 0 || length > bytesLength)\n                {\n                    length = bytesLength;\n                }\n\n                int codeIndex = 0;\n                while (true)\n                {\n                    int bytesRead = stream.Read(s_CachedBytes, 0, CachedBytesLength);\n                    if (bytesRead > 0)\n                    {\n                        if (length > 0)\n                        {\n                            for (int i = 0; i < bytesRead && i < length; i++)\n                            {","sourceCodeStart":136,"sourceCodeEnd":172,"githubUrl":"https://github.com/EllanJiang/GameFramework/blob/d0c010b05167c58e92350449d04864a91ca13fd2/GameFramework/Utility/Utility.Verifier.cs#L136-L172","documentation":"Utility.GetCrc32 throws GameFrameworkException with 'Code length is invalid.' when the byte[] code parameter has zero length. The code array seeds the CRC32 computation and must contain at least one byte; an empty array yields no usable seed.","triggerScenarios":"Calling Utility.Verifier.GetCrc32(stream, code, length) with code being a non-null but empty (Length == 0) byte array.","commonSituations":"code = new byte[0] created by a failed allocation or an empty file read; a seed loaded from a zero-byte file; an array produced by an empty serialization result.","solutions":["Pass a code array with at least 1 byte (standard CRC32 uses a 4-byte seed).","Validate code.Length > 0 before calling, or default to a 4-byte seed.","Fix the producer of the empty array (file read, network payload, config)."],"exampleFix":"// before\nbyte[] code = new byte[0];\nint crc = Utility.Verifier.GetCrc32(stream, code, length);\n// after\nbyte[] code = codeBytes != null && codeBytes.Length > 0 ? codeBytes : new byte[] { 0, 0, 0, 0 };\nint crc = Utility.Verifier.GetCrc32(stream, code, length);","handlingStrategy":"validation","validationCode":"if (code != null && code.Length > 0)\n{\n    int crc = Utility.Verifier.GetCrc32(stream, code, length);\n}","typeGuard":"bool IsValidCrcSeed(byte[] code) => code != null && code.Length > 0;","tryCatchPattern":"try\n{\n    int crc = Utility.Verifier.GetCrc32(stream, code, length);\n}\ncatch (GameFrameworkException ex) when (ex.Message.Contains(\"Code length\"))\n{\n    crc = Utility.Verifier.GetCrc32(stream, DefaultCrcSeed, 0); // fall back to default seed\n}","preventionTips":["Standardize on a 4-byte CRC seed constant for the project.","Validate arrays returned by file/network reads before using them as seeds.","Unit-test any helper that produces the seed array for the empty case."],"tags":["csharp","gameframework","empty-array","crc32","utility"],"backgroundTag":"empty-required-field","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"}