fix: IndexOfPattern alignment for multi-byte encodings
Stride the scan by pattern.Length (1 for ASCII/UTF-8, 2 for UTF-16, 4 for UTF-32) to avoid matching a misaligned null-terminator pattern mid-character.
This commit is contained in:
@@ -273,7 +273,8 @@ public abstract class MemoryBase : IDisposable
|
|||||||
private static int IndexOfPattern(byte[] data, byte[] pattern)
|
private static int IndexOfPattern(byte[] data, byte[] pattern)
|
||||||
{
|
{
|
||||||
int lastStart = data.Length - pattern.Length;
|
int lastStart = data.Length - pattern.Length;
|
||||||
for (int i = 0; i <= lastStart; i++)
|
int stride = Math.Max(1, pattern.Length);
|
||||||
|
for (int i = 0; i <= lastStart; i += stride)
|
||||||
{
|
{
|
||||||
bool match = true;
|
bool match = true;
|
||||||
for (int j = 0; j < pattern.Length; j++)
|
for (int j = 0; j < pattern.Length; j++)
|
||||||
|
|||||||
Reference in New Issue
Block a user