diff --git a/KaitaiStream.cs b/KaitaiStream.cs
index e4f8f56..e89ef1a 100644
--- a/KaitaiStream.cs
+++ b/KaitaiStream.cs
@@ -461,15 +461,21 @@ public byte[] EnsureFixedContents(byte[] expected)
///
/// Perform right-to-left strip on a byte array.
+ /// WARNING: Can return original byte array.
///
/// The data, as byte array
/// The padding byte, as integer
public static byte[] BytesStripRight(byte[] src, byte padByte)
{
int newLen = src.Length;
+ int maxLen = src.Length;
+
while (newLen > 0 && src[newLen - 1] == padByte)
newLen--;
+ if (newLen == maxLen)
+ return src;
+
byte[] dst = new byte[newLen];
Array.Copy(src, dst, newLen);
return dst;
@@ -477,6 +483,7 @@ public static byte[] BytesStripRight(byte[] src, byte padByte)
///
/// Perform left-to-right search of specified terminating byte, and cutoff remaining bytes.
+ /// WARNING: Can return original byte array.
///
/// The data, as byte array
/// The terminating byte, as integer
@@ -492,6 +499,9 @@ public static byte[] BytesTerminate(byte[] src, byte terminator, bool includeTer
if (includeTerminator && newLen < maxLen)
newLen++;
+ if (newLen == maxLen)
+ return src;
+
byte[] dst = new byte[newLen];
Array.Copy(src, dst, newLen);
return dst;