From 41bc6f0686bef8ad81ffe663615cfbadd74e4c24 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luk=C3=A1=C5=A1=20Lalinsk=C3=BD?= Date: Sat, 10 Nov 2007 22:16:56 +0000 Subject: [PATCH] Fix off-by-one error in ByteVector::find. In case where the pattern has the same length as the bytevector, it should degrade to A == B, not just return "not found" (e.g. A.find(A) should return 0, not -1). git-svn-id: svn://anonsvn.kde.org/home/kde/trunk/kdesupport/taglib@735107 283d02a7-25f6-0310-bc7c-ecb5cbfe19da --- taglib/toolkit/tbytevector.cpp | 2 +- tests/test_bytevector.cpp | 9 +++++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/taglib/toolkit/tbytevector.cpp b/taglib/toolkit/tbytevector.cpp index 26444331..f8431ef0 100644 --- a/taglib/toolkit/tbytevector.cpp +++ b/taglib/toolkit/tbytevector.cpp @@ -95,7 +95,7 @@ namespace TagLib { template int vectorFind(const Vector &v, const Vector &pattern, uint offset, int byteAlign) { - if(pattern.size() > v.size() || offset >= v.size() - 1) + if(pattern.size() > v.size() || offset > v.size() - 1) return -1; // Let's go ahead and special case a pattern of size one since that's common diff --git a/tests/test_bytevector.cpp b/tests/test_bytevector.cpp index 56789fee..0a343e31 100644 --- a/tests/test_bytevector.cpp +++ b/tests/test_bytevector.cpp @@ -34,6 +34,7 @@ class TestByteVector : public CppUnit::TestFixture CPPUNIT_TEST_SUITE(TestByteVector); CPPUNIT_TEST(testByteVector); CPPUNIT_TEST(testFind1); + CPPUNIT_TEST(testFind2); CPPUNIT_TEST(testRfind1); CPPUNIT_TEST(testRfind2); CPPUNIT_TEST_SUITE_END(); @@ -128,6 +129,14 @@ public: CPPUNIT_ASSERT_EQUAL(-1, ByteVector("....SggO."). find("SggO", 8)); } + void testFind2() + { + CPPUNIT_ASSERT_EQUAL(0, ByteVector("\x01", 1).find("\x01")); + CPPUNIT_ASSERT_EQUAL(0, ByteVector("\x01\x02", 2).find("\x01\x02")); + CPPUNIT_ASSERT_EQUAL(-1, ByteVector("\x01", 1).find("\x02")); + CPPUNIT_ASSERT_EQUAL(-1, ByteVector("\x01\x02", 2).find("\x01\x03")); + } + void testRfind1() { CPPUNIT_ASSERT_EQUAL(-1, ByteVector(".OggS....").rfind("OggS", 0));