huiyadanli/RevokeMsgPatcher · error · Exception

查询串与替换串长度不同!

Error message

查询串与替换串长度不同!

What it means

ComputChanges (in the RevokeMsgPatcher.Assistant authoring tool) diffs a search byte array against a replace byte array to produce per-match Change records. Because it patches bytes in place over an identical byte range, it requires searchBytes and replaceBytes to be the same length; it throws a plain Exception if their Length properties differ. This is an authoring/data error, not an end-user runtime condition.

Source

Thrown at RevokeMsgPatcher.Assistant/FormAssisant.cs:58

            byte[] replaceBytes = ByteUtil.HexStringToByteArray("1C E9 9D 00 00 00 8B 45 E8 8D 55 EC 52 89 5D EC EB 09 90 90 90 8B 08 50 FF 51 78 85 C0 79 2D 8D 45 0C C7 45 0C");
            //int[] indexs = FuzzyMatcher.MatchAll(fileByteArray, searchBytes);
            int[] indexs = FuzzyMatcher.MatchNotReplaced(fileByteArray, searchBytes, replaceBytes);
            txtInfo.AppendText("查找结果位置:" + string.Join(",", indexs) + Environment.NewLine);
            // 371130

            List<Change> changes = ComputChanges(indexs, searchBytes, replaceBytes);
            foreach (Change c in changes)
            {
                txtInfo.AppendText("替换位置:" + Convert.ToString(c.Position, 16) + " 替换内容:" + ByteUtil.ByteArrayToHexString(c.Content) + Environment.NewLine);
            }

        }

        public static List<Change> ComputChanges(int[] indexs, byte[] searchBytes, byte[] replaceBytes)
        {
            if (searchBytes.Length != replaceBytes.Length)
            {
                throw new Exception("查询串与替换串长度不同!");
            }
            // 一个替换串存在多个替换点的情况
            List<Change> changeOffsets = new List<Change>(); // 查询串与替换串变化偏移
            List<byte> diff = null;
            for (int i = 0; i < searchBytes.Length; i++)
            {
                if (searchBytes[i] != replaceBytes[i])
                {
                    if (diff == null)
                    {
                        diff = new List<byte>();
                        Change offset = new Change
                        {
                            Position = i
                        };
                        changeOffsets.Add(offset);
                    }
                    diff.Add(replaceBytes[i]);

View on GitHub (pinned to 89cbbb3f0c)

Solutions

  1. Make searchBytes and replaceBytes the same length (in-place hex edit requires matching length).
  2. Re-derive both arrays from equal-length hex strings, stripping spaces, and confirm both have an even nibble count.
  3. Add a length guard before calling ComputChanges and log both lengths to pinpoint the mismatch.

Example fix

// before
byte[] searchBytes  = ByteUtil.HexStringToByteArray("AA BB CC");      // 3 bytes
byte[] replaceBytes = ByteUtil.HexStringToByteArray("AA BB CC DD");   // 4 bytes -> throws
List<Change> changes = ComputChanges(indexs, searchBytes, replaceBytes);

// after
byte[] searchBytes  = ByteUtil.HexStringToByteArray("AA BB CC DD"); // 4
byte[] replaceBytes = ByteUtil.HexStringToByteArray("AA BB CC DD"); // 4
if (searchBytes.Length != replaceBytes.Length)
    throw new ArgumentException($"len mismatch {searchBytes.Length} vs {replaceBytes.Length}");
List<Change> changes = ComputChanges(indexs, searchBytes, replaceBytes);
Defensive patterns

Strategy: validation

Validate before calling

byte[] searchBytes = ByteUtil.HexStringToByteArray(searchHex);
byte[] replaceBytes = ByteUtil.HexStringToByteArray(replaceHex);
if (searchBytes.Length != replaceBytes.Length)
{
    txtInfo.AppendText($"长度不同: search={searchBytes.Length} replace={replaceBytes.Length}{Environment.NewLine}");
    return;
}
List<Change> changes = ComputChanges(indexs, searchBytes, replaceBytes);

Prevention

When it happens

Trigger: Calling FormAssisant.ComputChanges(indexs, searchBytes, replaceBytes) where the two byte arrays differ in length. Concretely: hand-editing the hex strings in btnSearch_Click so the search and replace strings are out of sync, or one having a stray space / odd character count after ByteUtil.HexStringToByteArray.

Common situations: Authoring a new patch in the Assistant tool; pasting a 32-byte search pattern but a 31- or 33-byte replacement; hex strings with inconsistent whitespace or an odd number of nibbles.

Related errors


AI-assisted analysis of huiyadanli/RevokeMsgPatcher@89cbbb3f0c (2026-08-13). Data as JSON: /api/errors/f3cc52a7d1b3ddaf. Report an issue: GitHub.