]> granicus.if.org Git - taglib/commitdiff
Fix defect in ByteVectorStream::seek when Position==End.
authorKevin André <hyperquantum@gmail.com>
Thu, 15 Sep 2016 14:30:16 +0000 (16:30 +0200)
committerKevin André <hyperquantum@gmail.com>
Thu, 15 Sep 2016 14:30:16 +0000 (16:30 +0200)
taglib/toolkit/tbytevectorstream.cpp
tests/test_bytevectorstream.cpp

index 5e200b3d751c20cab3a751b592a1e59bfae62d58..74b2eced20aac4d5261a04224fc78cdbd5a5ef29 100644 (file)
@@ -137,7 +137,7 @@ void ByteVectorStream::seek(long offset, Position p)
     d->position += offset;
     break;
   case End:
-    d->position = length() - offset;
+    d->position = length() + offset; // offset is expected to be negative
     break;
   }
 }
index c7b0f1053ffd495f88aadec713196dd32d55dbd0..f8308c6dc055206fd9a50c3bada98bed94914110 100644 (file)
@@ -38,6 +38,7 @@ class TestByteVectorStream : public CppUnit::TestFixture
   CPPUNIT_TEST(testReadBlock);
   CPPUNIT_TEST(testRemoveBlock);
   CPPUNIT_TEST(testInsert);
+  CPPUNIT_TEST(testSeekEnd);
   CPPUNIT_TEST_SUITE_END();
 
 public:
@@ -112,6 +113,19 @@ public:
     CPPUNIT_ASSERT_EQUAL(ByteVector("yyx123foa"), *stream.data());
   }
 
+  void testSeekEnd()
+  {
+    ByteVector v("abcdefghijklmnopqrstuvwxyz");
+    ByteVectorStream stream(v);
+    CPPUNIT_ASSERT_EQUAL(26L, stream.length());
+
+    stream.seek(-4, IOStream::End);
+    CPPUNIT_ASSERT_EQUAL(ByteVector("w"), stream.readBlock(1));
+
+    stream.seek(-25, IOStream::End);
+    CPPUNIT_ASSERT_EQUAL(ByteVector("b"), stream.readBlock(1));
+  }
+
 };
 
 CPPUNIT_TEST_SUITE_REGISTRATION(TestByteVectorStream);