pardeike/Harmony · error · ArgumentOutOfRangeException

position( ) + count( ) > buffer.Length( )

Error message

position({position}) + count({count}) > buffer.Length({buffer.Length})

What it means

ByteBuffer.CheckCanRead validates that reading 'count' bytes at the current position stays inside the underlying buffer. When position + count exceeds buffer.Length, the requested read would run past the end of the emitted IL body, so Harmony throws ArgumentOutOfRangeException for 'count'.

Solutions

  1. Ensure the target method has a real IL body (not abstract, extern, or runtime-provided) before Harmony parses it
  2. Check that the method's assembly is not trimmed/stripped (IL2CPP, ILLink, obfuscators)
  3. Upgrade Harmony to the latest version — earlier parsers could compute wrong offsets on exotic method bodies
  4. Wrap the patch call and inspect the method body yourself with MethodBase.GetMethodBody() to confirm it is readable

Example fix

// before
harmony.Patch(abstractOrNativeMethod, ...);
// after
if (method.IsAbstract || method.IsNative || method.GetMethodBody() is null)
    throw new InvalidOperationException("Cannot patch method without IL body");
harmony.Patch(method, ...);
Defensive patterns

Strategy: try-catch

Validate before calling

var body = method.GetMethodBody();
if (body is null || body.GetILAsByteArray().Length == 0) throw new InvalidOperationException("Method has no readable IL body");

Try / catch

try { harmony.Patch(method, ...); }
catch (ArgumentOutOfRangeException ex) when (ex.ParamName == "count") { /* method body unreadable/truncated: skip or use alternate patching */ }

Prevention

When it happens

Trigger: Any of ReadByte/ReadBytes/ReadInt16/ReadInt32/ReadInt64/ReadSingle called when fewer than the requested bytes remain — typically a malformed or truncated method body, or a reader whose position was advanced incorrectly (e.g. misparsed exception/variable headers).

Common situations: Reading IL of a method from an assembly built/trimmed in an unusual way (IL2CPP, trimming, obfuscation); parsing a dynamic or abstract method with no real body; Harmony version mismatch producing a bad offset while decoding a method body.

Related errors


AI-assisted analysis of pardeike/Harmony@e7872dc170 (2026-09-15). Data as JSON: /api/errors/64b5e52ab43238b5. Report an issue: GitHub.

Appendix: source

Thrown at Harmony/Internal/ByteBuffer.cs:99

		internal double ReadDouble()
		{
			if (!BitConverter.IsLittleEndian)
			{
				var bytes = ReadBytes(8);
				Array.Reverse(bytes);
				return BitConverter.ToDouble(bytes, 0);
			}

			CheckCanRead(8);
			var value = BitConverter.ToDouble(buffer, position);
			position += 8;
			return value;
		}

		void CheckCanRead(int count)
		{
			if (position + count > buffer.Length)
				throw new ArgumentOutOfRangeException(nameof(count), $"position({position}) + count({count}) > buffer.Length({buffer.Length})");
		}
	}
}

View on GitHub (pinned to e7872dc170)