Truly independent web browser
LibMedia: Skip trailing bytes too short to hold an MP4 box
sideshowbarker176d3b02dcfc
diff --git a/Libraries/LibMedia/Containers/ISOBMFF/Reader.cpp b/Libraries/LibMedia/Containers/ISOBMFF/Reader.cppindex be30953fc5b..8849f69744d 100644--- a/Libraries/LibMedia/Containers/ISOBMFF/Reader.cpp+++ b/Libraries/LibMedia/Containers/ISOBMFF/Reader.cpp@@ -148,6 +148,15 @@ DecoderErrorOr<void> Reader::parse_child_boxes(Streamer& streamer, BoxHeader conreturn DecoderError::format(DecoderErrorCategory::Corrupted, "Read past the end of the {} box", parent.type);while (!parent_end.has_value() || streamer.position() < parent_end.value()) {+ // A box header takes at least a 32-bit size and a four-character type, so fewer bytes than that at the end of+ // the parent can't start another box. Skip them, leaving the streamer at the parent's end: QuickTime files may+ // end a sample entry's child boxes with a 32-bit zero as a terminator.+ static constexpr size_t MINIMUM_BOX_HEADER_SIZE = sizeof(u32) + sizeof(FourCC);+ if (parent_end.has_value() && parent_end.value() - streamer.position() < MINIMUM_BOX_HEADER_SIZE) {+ TRY(streamer.seek_to_position(parent_end.value()));+ break;+ }+auto child_or_error = read_box_header(streamer);if (child_or_error.is_error()) {if (!parent_end.has_value() && child_or_error.error().category() == DecoderErrorCategory::EndOfStream)diff --git a/Tests/LibMedia/TestISOBMFF.cpp b/Tests/LibMedia/TestISOBMFF.cppindex dd1df576d48..3b2f78696ab 100644--- a/Tests/LibMedia/TestISOBMFF.cpp+++ b/Tests/LibMedia/TestISOBMFF.cpp@@ -1373,6 +1373,55 @@ TEST_CASE(parses_vp_codec_configuration_box)EXPECT_EQ(entry.cicp->transfer_characteristics(), Media::TransferCharacteristics::SMPTE2084);}+static void append_vp9_configuration_box_and_quicktime_terminator(ByteBuffer& bytes)+{+ append_vp9_configuration_box(bytes);+ append_u32(bytes, 0);+}++static void append_vp9_configuration_box_and_three_trailing_bytes(ByteBuffer& bytes)+{+ append_vp9_configuration_box(bytes);+ bytes.append(1);+ bytes.append(2);+ bytes.append(3);+}++TEST_CASE(skips_trailing_bytes_too_short_for_a_box_at_the_end_of_a_sample_entry)+{+ for (auto append_configuration_box : { append_vp9_configuration_box_and_quicktime_terminator, append_vp9_configuration_box_and_three_trailing_bytes }) {+ ByteBuffer bytes;+ append_video_movie(bytes, "vp09", append_configuration_box, ColourBoxOrder::None);+ auto entry = parse_only_sample_entry(bytes);++ EXPECT(entry.parsed_codec.has_value());+ EXPECT_EQ(entry.parsed_codec->vp9_parameters()->profile, 2);+ }+}++TEST_CASE(leaves_the_streamer_at_the_parent_end_after_trailing_bytes_too_short_for_a_box)+{+ ByteBuffer bytes;+ auto parent = begin_box(bytes, "moof");+ auto child = begin_box(bytes, "free");+ finish_box(bytes, child);+ bytes.append(1);+ bytes.append(2);+ bytes.append(3);+ finish_box(bytes, parent);+ append_u32(bytes, 0); // Whatever follows the parent.++ auto streamer = streamer_for(bytes);+ auto header = MUST(Media::ISOBMFF::Reader::read_box_header(streamer));+ size_t child_count = 0;+ MUST(Media::ISOBMFF::Reader::parse_child_boxes(streamer, header, Media::ISOBMFF::IsTopLevel::No, [&](auto const&) -> Media::DecoderErrorOr<IterationDecision> {+ ++child_count;+ return IterationDecision::Continue;+ }));+ EXPECT_EQ(child_count, 1u);+ EXPECT_EQ(streamer.position(), header.end_position().value());+}+TEST_CASE(colour_information_box_overrides_the_vp_configuration_box_in_either_order){for (auto order : { ColourBoxOrder::BeforeConfiguration, ColourBoxOrder::AfterConfiguration }) {