EllanJiang/GameFramework · error · GameFrameworkException

Length is invalid.

Error message

Length is invalid.

What it means

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.

Solutions

  1. Verify the length passed to FileInfo is >= 0 before constructing
  2. Fix the size computation that produced the negative value (check for reversed minuend/subtrahend or sentinel values)
  3. Re-export or repair the filesystem archive if lengths come from a loaded header
  4. Log the offending name/offset/length triple before construction to diagnose source data

Example fix

// before
var info = new FileInfo(name, offset, -1); // unknown size sentinel
// after
int length = ComputeLength(entry);
if (length < 0) length = 0; // or throw with diagnostics
var info = new FileInfo(name, offset, length);
Defensive patterns

Strategy: validation

Validate before calling

if (length < 0) throw new ArgumentOutOfRangeException(nameof(length), length, "length must be >= 0");
var info = new FileInfo(name, offset, length);

Type guard

bool IsValidLength(int length) => length >= 0;

Try / catch

try { var info = new FileInfo(name, offset, length); }
catch (GameFrameworkException ex) when (ex.Message == "Length is invalid.") { Log.Error($"Bad length {length} for '{name}'"); }

Prevention

When it happens

Trigger: Calling new FileInfo(name, offset, length) with a negative length, or building FileInfo from a corrupted archive header whose stored length is negative.

Common situations: 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.

Understand the failure class

Background: "value must be between 0 and 1" / "out of range" / "must not be negative" errors: fixing range-validation failures across open-source libraries — this error's family across 42 libraries.

Related errors


AI-assisted analysis of EllanJiang/GameFramework@d0c010b051 (2026-09-15). Data as JSON: /api/errors/0dc4425ded2d5c55. Report an issue: GitHub.

Appendix: source

Thrown at GameFramework/FileSystem/FileInfo.cs:42

        /// </summary>
        /// <param name="name">文件名称。</param>
        /// <param name="offset">文件偏移。</param>
        /// <param name="length">文件长度。</param>
        public FileInfo(string name, long offset, int length)
        {
            if (string.IsNullOrEmpty(name))
            {
                throw new GameFrameworkException("Name is invalid.");
            }

            if (offset < 0L)
            {
                throw new GameFrameworkException("Offset is invalid.");
            }

            if (length < 0)
            {
                throw new GameFrameworkException("Length is invalid.");
            }

            m_Name = name;
            m_Offset = offset;
            m_Length = length;
        }

        /// <summary>
        /// 获取文件信息是否有效。
        /// </summary>
        public bool IsValid
        {
            get
            {
                return !string.IsNullOrEmpty(m_Name) && m_Offset >= 0L && m_Length >= 0;
            }
        }

View on GitHub (pinned to d0c010b051)