EllanJiang/GameFramework · error · GameFrameworkException
Code length is invalid.
Error message
Code length is invalid.
What it means
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.
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.
Example fix
// before
byte[] key = Utility.Converter.GetBytes(keyString, Encoding.UTF8);
Utility.Converter.GetSelfXorBytes(data, 0, data.Length, key);
// after
byte[] key = Utility.Converter.GetBytes(keyString ?? "default-key", Encoding.UTF8);
if (key.Length == 0) throw new InvalidOperationException("Encryption key must not be empty");
Utility.Converter.GetSelfXorBytes(data, 0, data.Length, key); Defensive patterns
Strategy: validation
Validate before calling
if (code == null || code.Length == 0) throw new InvalidOperationException("XOR key must contain at least one byte"); Type guard
bool IsNonEmptyKey(byte[] code) => code != null && code.Length > 0;
Try / catch
try { Utility.Converter.GetSelfXorBytes(data, 0, data.Length, key); }
catch (GameFrameworkException ex) { Log.Error("Bad XOR key: {0}", ex.Message); /* fall back to a default key or disable encryption */ } Prevention
- 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.
When it happens
Trigger: 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.
Common situations: 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.
Understand the failure class
Background: "must not be empty", "cannot be empty" — required-field validation errors across open-source libraries — this error's family across 41 libraries.
Related errors
AI-assisted analysis of EllanJiang/GameFramework@d0c010b051 (2026-09-15).
Data as JSON: /api/errors/dcb156f70e8f359a.
Report an issue: GitHub.
Appendix: source
Thrown at GameFramework/Utility/Utility.Encryption.cs:117
/// <param name="startIndex">异或计算的开始位置。</param>
/// <param name="length">异或计算长度。</param>
/// <param name="code">异或二进制流。</param>
public static void GetSelfXorBytes(byte[] bytes, int startIndex, int length, byte[] code)
{
if (bytes == null)
{
return;
}
if (code == null)
{
throw new GameFrameworkException("Code is invalid.");
}
int codeLength = code.Length;
if (codeLength <= 0)
{
throw new GameFrameworkException("Code length is invalid.");
}
if (startIndex < 0 || length < 0 || startIndex + length > bytes.Length)
{
throw new GameFrameworkException("Start index or length is invalid.");
}
int codeIndex = startIndex % codeLength;
for (int i = startIndex; i < length; i++)
{
bytes[i] ^= code[codeIndex++];
codeIndex %= codeLength;
}
}
}
}
}
View on GitHub (pinned to d0c010b051)