{"record":{"id":"dcb156f70e8f359a","repo":"EllanJiang/GameFramework","slug":"code-length-is-invalid","errorCode":null,"errorMessage":"Code length is invalid.","messagePattern":"Code length is invalid\\.","errorType":"exception","errorClass":"GameFrameworkException","httpStatus":null,"severity":"error","filePath":"GameFramework/Utility/Utility.Encryption.cs","lineNumber":117,"sourceCode":"            /// <param name=\"startIndex\">异或计算的开始位置。</param>\n            /// <param name=\"length\">异或计算长度。</param>\n            /// <param name=\"code\">异或二进制流。</param>\n            public static void GetSelfXorBytes(byte[] bytes, int startIndex, int length, byte[] code)\n            {\n                if (bytes == null)\n                {\n                    return;\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                if (startIndex < 0 || length < 0 || startIndex + length > bytes.Length)\n                {\n                    throw new GameFrameworkException(\"Start index or length is invalid.\");\n                }\n\n                int codeIndex = startIndex % codeLength;\n                for (int i = startIndex; i < length; i++)\n                {\n                    bytes[i] ^= code[codeIndex++];\n                    codeIndex %= codeLength;\n                }\n            }\n        }\n    }\n}\n","sourceCodeStart":99,"sourceCodeEnd":135,"githubUrl":"https://github.com/EllanJiang/GameFramework/blob/d0c010b05167c58e92350449d04864a91ca13fd2/GameFramework/Utility/Utility.Encryption.cs#L99-L135","documentation":"Utility.Encryption.GetSelfXorBytes throws GameFrameworkException(\"Code length is invalid.\") when the XOR key array is non-null but empty (Length <= 0). The algorithm indexes code[startIndex % codeLength], so an empty key would divide by zero; the library rejects it explicitly before touching the buffer.","triggerScenarios":"Calling GetSelfXorBytes/GetQuickSelfXorBytes/GetXorBytes with new byte[0] or an array truncated to zero elements, e.g. a key decoded from an empty config string or an empty file.","commonSituations":"Key stored in a text/config file that is empty or whitespace-trimmed to nothing; a key-generation routine that returned an empty array; a byte array built from a zero-length string via GetBytes.","solutions":["Provide a key with at least one byte; validate key length > 0 before calling.","Check the source that produces the key (file, config, constant) — it is yielding an empty value.","Fall back to a default key or skip encryption with a logged warning when the key is empty."],"exampleFix":"// before\nbyte[] key = Utility.Converter.GetBytes(keyString, Encoding.UTF8);\nUtility.Converter.GetSelfXorBytes(data, 0, data.Length, key);\n// after\nbyte[] key = Utility.Converter.GetBytes(keyString ?? \"default-key\", Encoding.UTF8);\nif (key.Length == 0) throw new InvalidOperationException(\"Encryption key must not be empty\");\nUtility.Converter.GetSelfXorBytes(data, 0, data.Length, key);","handlingStrategy":"validation","validationCode":"if (code == null || code.Length == 0) throw new InvalidOperationException(\"XOR key must contain at least one byte\");","typeGuard":"bool IsNonEmptyKey(byte[] code) => code != null && code.Length > 0;","tryCatchPattern":"try { Utility.Converter.GetSelfXorBytes(data, 0, data.Length, key); }\ncatch (GameFrameworkException ex) { Log.Error(\"Bad XOR key: {0}\", ex.Message); /* fall back to a default key or disable encryption */ }","preventionTips":["Validate key length immediately after loading/deriving the key.","Check config/file sources of the key for empty content.","Use a build-time constant or validated secret for keys instead of empty-string-derived arrays."],"tags":["empty-array","encryption","xor","key-validation"],"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-15T23:17:13.987Z"}