Repository: LadybirdBrowser/ladybird
File: Libraries/LibMedia/Containers/Matroska/Reader.cpp (C++)

Code before:
            constexpr uint final_micro_version = 100;

            auto major_version = split[0].to_number<uint>();
            if (!major_version.has_value() || major_version.value() > final_major_version)
                return false;
            if (major_version.value() < final_major_version)
                return true;

            auto minor_version = split[1].to_number<uint>();
            if (!minor_version.has_value() || minor_version.value() > final_minor_version)
                return false;
            if (minor_version.value() < final_minor_version)
                return true;

            auto micro_version = split[2].to_number<uint>();
            return micro_version.has_value() && micro_version.value() <= final_micro_version;
        }();
        if (!is_affected_version)
            return;

        u64 max_codec_delay = 0;
        for (auto& [id, track] : m_tracks) {
            auto delay = track->codec_delay();

            if (codec_id_from_matroska_id_string(track->codec_id()) == CodecID::Opus && track->audio_track().has_value())
                delay = delay * 48'000 / AK::clamp_to<u64>(track->audio_track()->sampling_frequency);

            max_codec_delay = max(max_codec_delay, delay);
        }

        auto timestamp_scale = segment_information.timestamp_scale();
        max_codec_delay = ((max_codec_delay + (timestamp_scale / 2)) / timestamp_scale) * timestamp_scale;

        for (auto& [id, track] : m_tracks) {
            if (track->codec_delay() != 0)
                continue;
            track->set_codec_delay(max_codec_delay);
        }

        auto duration = segment_information.duration_unscaled();

        if (duration.has_value()) {
            auto max_codec_delay_in_duration_units = static_cast<double>(max_codec_delay) / static_cast<double>(segment_information.timestamp_scale());
            segment_information.set_duration_unscaled(duration.value() - max_codec_delay_in_duration_units);
        }
    }
}

DecoderErrorOr<void> Reader::for_each_track(TrackEntryCallback callback)
{
    TRY(ensure_tracks_are_parsed());

Review comment (bug):
Unless I'm reading it incorrectly, `sampling_frequency` can still be a negative double at this point and we'll end up with a division by zero. Maybe we should detect this when reading the frequency from the data?

Code after:
            return;

        auto is_affected_version = [&] {
            constexpr uint final_major_version = 59;
            constexpr uint final_minor_version = 30;
            constexpr uint final_micro_version = 100;

            auto major_version = split[0].to_number<uint>();
            if (!major_version.has_value() || major_version.value() > final_major_version)
                return false;
            if (major_version.value() < final_major_version)
                return true;

            auto minor_version = split[1].to_number<uint>();
            if (!minor_version.has_value() || minor_version.value() > final_minor_version)
                return false;
            if (minor_version.value() < final_minor_version)
                return true;

            auto micro_version = split[2].to_number<uint>();
            return micro_version.has_value() && micro_version.value() <= final_micro_version;
        }();
        if (!is_affected_version)
            return;

        u64 max_codec_delay = 0;
        for (auto& [id, track] : m_tracks) {
            auto delay = track->codec_delay();

            if (codec_id_from_matroska_id_string(track->codec_id()) == CodecID::Opus && track->audio_track().has_value()) {
                auto sampling_frequency = AK::clamp_to<u64>(track->audio_track()->sampling_frequency);
                if (sampling_frequency == 0)
                    return;
                delay = delay * 48'000 / sampling_frequency;
            }

            max_codec_delay = max(max_codec_delay, delay);
        }

        auto timestamp_scale = segment_information.timestamp_scale();
        max_codec_delay = ((max_codec_delay + (timestamp_scale / 2)) / timestamp_scale) * timestamp_scale;

        for (auto& [id, track] : m_tracks) {
            if (track->codec_delay() != 0)
                continue;
            track->set_codec_delay(max_codec_delay);
        }

        auto duration = segment_information.duration_unscaled();

        if (duration.has_value()) {