You need to compare the minimum number of bytes, not the maximum, to avoid reading invalid data. Or if you're looking for strict equality, return failure if the lengths don't match.