Skip to content

Commit

Permalink
Improved stability for mp4 parser when dealing with corrupted FTYP bo…
Browse files Browse the repository at this point in the history
…xes (#241)

* Fixing issues when inconsistent size information on ftyp boxes caused MP4 parser to throw errors instead of rejecting the files
  • Loading branch information
rcpalacio committed Sep 19, 2023
1 parent a8a4d07 commit 2d9456d
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/parsers/mp4_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ def call(io)
private

def file_format(box_tree)
major_brand = box_tree.find { |box| box.type == 'ftyp' }&.fields[:major_brand]
major_brand = box_tree.find { |box| box.type == 'ftyp' }&.fields&.[](:major_brand)
BRAND_FORMATS[major_brand.downcase] if major_brand
end

Expand Down
11 changes: 11 additions & 0 deletions spec/parsers/mp4_parser_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,17 @@
end
end

context "when when magic bytes are present but the 'ftyp' box can't be parsed" do
let(:io) do
input = [0xFF].pack('N') + 'ftyp' + 'avc1 ' + [0x1].pack('N')
StringIO.new(input)
end

it 'should be nil' do
expect(subject.call(io)).to be_nil
end
end

Dir.glob(fixtures_dir + '/MP4/valid/video/*.*').sort.each do |path|
context "for #{path}" do
let(:result) { subject.call(File.open(path, 'rb')) }
Expand Down

0 comments on commit 2d9456d

Please sign in to comment.