mutagen icon indicating copy to clipboard operation
mutagen copied to clipboard

struct.error when processing an mp4 file

Open jvoisin opened this issue 5 years ago • 4 comments

When running the following python script with the attached file, mutagen throws a struct.error exception:

import mutagen, sys

f = mutagen.File(sys.argv[1])
f.delete()
$  python3 mutest.py crash-116c56788bf36e630a7ab5c43bdd6e55b2ee2e0b706f5493dfb7a22e1ac1c81f
Traceback (most recent call last):
  File "mutest.py", line 4, in <module>
    f.delete()
  File "/home/jvoisin/app/AFL/ven/lib/python3.7/site-packages/mutagen/_util.py", line 140, in wrapper
    return func(self, h, *args, **kwargs)
  File "/home/jvoisin/app/AFL/ven/lib/python3.7/site-packages/mutagen/_file.py", line 120, in delete
    return self.tags.delete(filething)
  File "/home/jvoisin/app/AFL/ven/lib/python3.7/site-packages/mutagen/mp4/__init__.py", line 836, in delete
    self.save(filename, padding=lambda x: 0)
  File "/home/jvoisin/app/AFL/ven/lib/python3.7/site-packages/mutagen/_util.py", line 169, in wrapper
    return func(*args, **kwargs)
  File "/home/jvoisin/app/AFL/ven/lib/python3.7/site-packages/mutagen/_util.py", line 140, in wrapper
    return func(self, h, *args, **kwargs)
  File "/home/jvoisin/app/AFL/ven/lib/python3.7/site-packages/mutagen/mp4/__init__.py", line 425, in save
    self.__save(filething.fileobj, atoms, data, padding)
  File "/home/jvoisin/app/AFL/ven/lib/python3.7/site-packages/mutagen/mp4/__init__.py", line 433, in __save
    self.__save_existing(fileobj, atoms, path, data, padding)
  File "/home/jvoisin/app/AFL/ven/lib/python3.7/site-packages/mutagen/mp4/__init__.py", line 498, in __save_existing
    self.__update_parents(fileobj, path[:-1], delta)
  File "/home/jvoisin/app/AFL/ven/lib/python3.7/site-packages/mutagen/mp4/__init__.py", line 517, in __update_parents
    fileobj.write(cdata.to_uint_be(size + delta))
struct.error: argument out of range
zsh: exit 1     python3 mutest.py 
$

jvoisin avatar Dec 14 '19 18:12 jvoisin

Just for some context: Are these all real files or the result of fuzzing?

lazka avatar Dec 14 '19 18:12 lazka

It's the result of fuzzing :)

jvoisin avatar Dec 14 '19 18:12 jvoisin

ok, cool, thanks. Just asking since if the files were real we'd have to check how other software parses them and try to still load them instead of just erroring out in a controlled way.

lazka avatar Dec 14 '19 18:12 lazka

Oh, right, my bad, I should have thought about this. They are all randomly generated, and entirely invalid. The right way™ to handle them in my opinion is to raise an exception like mutagen.MutagenError or ValueError :)

jvoisin avatar Dec 14 '19 18:12 jvoisin

It's worth noting that the actual issue can no longer be reproduced directly with the provided file since it raises a controlled exception on loading after the fixes in #429.

But I think the proposed fix in #462 of handling this case is still valid, though.

phw avatar Feb 15 '23 08:02 phw

Fixed in #462

phw avatar Feb 16 '23 20:02 phw