{"record":{"id":"0dc4425ded2d5c55","repo":"EllanJiang/GameFramework","slug":"length-is-invalid-fileinfo","errorCode":null,"errorMessage":"Length is invalid.","messagePattern":"Length is invalid\\.","errorType":"validation","errorClass":"GameFrameworkException","httpStatus":null,"severity":"error","filePath":"GameFramework/FileSystem/FileInfo.cs","lineNumber":42,"sourceCode":"        /// </summary>\n        /// <param name=\"name\">文件名称。</param>\n        /// <param name=\"offset\">文件偏移。</param>\n        /// <param name=\"length\">文件长度。</param>\n        public FileInfo(string name, long offset, int length)\n        {\n            if (string.IsNullOrEmpty(name))\n            {\n                throw new GameFrameworkException(\"Name is invalid.\");\n            }\n\n            if (offset < 0L)\n            {\n                throw new GameFrameworkException(\"Offset is invalid.\");\n            }\n\n            if (length < 0)\n            {\n                throw new GameFrameworkException(\"Length is invalid.\");\n            }\n\n            m_Name = name;\n            m_Offset = offset;\n            m_Length = length;\n        }\n\n        /// <summary>\n        /// 获取文件信息是否有效。\n        /// </summary>\n        public bool IsValid\n        {\n            get\n            {\n                return !string.IsNullOrEmpty(m_Name) && m_Offset >= 0L && m_Length >= 0;\n            }\n        }\n","sourceCodeStart":24,"sourceCodeEnd":60,"githubUrl":"https://github.com/EllanJiang/GameFramework/blob/d0c010b05167c58e92350449d04864a91ca13fd2/GameFramework/FileSystem/FileInfo.cs#L24-L60","documentation":"FileInfo's constructor validates that the entry length is non-negative. A negative length is meaningless for a byte-range file entry and would break subsequent reads, so GameFramework throws GameFrameworkException.","triggerScenarios":"Calling new FileInfo(name, offset, length) with a negative length, or building FileInfo from a corrupted archive header whose stored length is negative.","commonSituations":"Deserializing a damaged game filesystem file with garbage length fields; using -1 as a 'unknown size' sentinel; integer arithmetic bugs (subtraction producing negative sizes) in tools that generate archives.","solutions":["Verify the length passed to FileInfo is >= 0 before constructing","Fix the size computation that produced the negative value (check for reversed minuend/subtrahend or sentinel values)","Re-export or repair the filesystem archive if lengths come from a loaded header","Log the offending name/offset/length triple before construction to diagnose source data"],"exampleFix":"// before\nvar info = new FileInfo(name, offset, -1); // unknown size sentinel\n// after\nint length = ComputeLength(entry);\nif (length < 0) length = 0; // or throw with diagnostics\nvar info = new FileInfo(name, offset, length);","handlingStrategy":"validation","validationCode":"if (length < 0) throw new ArgumentOutOfRangeException(nameof(length), length, \"length must be >= 0\");\nvar info = new FileInfo(name, offset, length);","typeGuard":"bool IsValidLength(int length) => length >= 0;","tryCatchPattern":"try { var info = new FileInfo(name, offset, length); }\ncatch (GameFrameworkException ex) when (ex.Message == \"Length is invalid.\") { Log.Error($\"Bad length {length} for '{name}'\"); }","preventionTips":["Use 0 instead of -1 for unknown sizes","Check subtraction-based size computations for negative results","Validate lengths read from archive headers before use"],"tags":["gameframework","filesystem","argument-validation"],"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-15T23:17:13.987Z"}