Skip to content

Commit

Permalink
riff: fixed RiffChunk.write writing padding at wrong position
Browse files Browse the repository at this point in the history
  • Loading branch information
phw committed Nov 15, 2019
1 parent fe67c09 commit 55abe41
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 7 deletions.
2 changes: 1 addition & 1 deletion mutagen/_riff.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
28 changes: 22 additions & 6 deletions tests/test__riff.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 55abe41

Please sign in to comment.