From 55abe410bfe096719e49c5e12a117ee32b08b399 Mon Sep 17 00:00:00 2001 From: Philipp Wolfer Date: Wed, 23 Oct 2019 21:29:23 +0200 Subject: [PATCH] riff: fixed RiffChunk.write writing padding at wrong position --- mutagen/_riff.py | 2 +- tests/test__riff.py | 28 ++++++++++++++++++++++------ 2 files changed, 23 insertions(+), 7 deletions(-) diff --git a/mutagen/_riff.py b/mutagen/_riff.py index c5486bc2..269da4a7 100644 --- a/mutagen/_riff.py +++ b/mutagen/_riff.py @@ -116,7 +116,7 @@ def write(self, data): # Write the padding bytes padding = self.padding() if padding: - self._fileobj.seek(self.data_offset + self.data_size + 1) + self._fileobj.seek(self.data_offset + self.data_size) self._fileobj.write(b'\x00' * padding) def delete(self): diff --git a/tests/test__riff.py b/tests/test__riff.py index 688f9578..31a8ca0e 100644 --- a/tests/test__riff.py +++ b/tests/test__riff.py @@ -73,12 +73,28 @@ def test_insert_chunk(self): self.failUnlessEqual(new_riff[u'id3'].data_size, 0) def test_insert_padded_chunks(self): - self.riff_2_tmp.insert_chunk(u'TST1') - self.riff_2_tmp[u'TST1'].resize(3) - self.failUnlessEqual(1, self.riff_2_tmp[u'TST1'].padding()) - self.riff_2_tmp.insert_chunk(u'TST2') - self.riff_2_tmp[u'TST2'].resize(4) - self.failUnlessEqual(0, self.riff_2_tmp[u'TST2'].padding()) + padded = self.riff_2_tmp.insert_chunk(u'TST1') + unpadded = self.riff_2_tmp.insert_chunk(u'TST2') + # The second chunk needs no padding + unpadded.resize(4) + self.failUnlessEqual(4, unpadded.data_size) + self.failUnlessEqual(0, unpadded.padding()) + self.failUnlessEqual(12, unpadded.size) + # Resize the first chunk so it needs padding + padded.resize(3) + self.failUnlessEqual(3, padded.data_size) + self.failUnlessEqual(1, padded.padding()) + self.failUnlessEqual(12, padded.size) + self.failUnlessEqual(padded.offset + padded.size, unpadded.offset) + # Verify the padding byte gets written correctly + self.file_2_tmp.seek(padded.data_offset) + self.file_2_tmp.write(b'ABCD') + padded.write(b'ABC') + self.file_2_tmp.seek(padded.data_offset) + self.failUnlessEqual(b'ABC\x00', self.file_2_tmp.read(4)) + # Verify the second chunk got not overwritten + self.file_2_tmp.seek(unpadded.offset) + self.failUnlessEqual(b'TST2', self.file_2_tmp.read(4)) def test_delete_padded_chunks(self): riff_file = self.riff_2_tmp