1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 07:37:46 +00:00

LibAudio: Perform seekpoint comparison without subtraction

For very large seekpoint indices, the casts necessary for the "simple"
subtraction comparison will yield wrong and overflowing results.
Therefore, we perform the seekpoint comparison manually instead.
This commit is contained in:
kleines Filmröllchen 2023-06-27 20:29:52 +02:00 committed by Jelle Raaijmakers
parent e82bee86dd
commit a75377d014

View file

@ -27,7 +27,12 @@ Optional<SeekPoint const&> SeekTable::seek_point_before(u64 sample_index) const
return {}; return {};
size_t nearby_seek_point_index = 0; size_t nearby_seek_point_index = 0;
AK::binary_search(m_seek_points, sample_index, &nearby_seek_point_index, [](auto const& sample_index, auto const& seekpoint_candidate) { AK::binary_search(m_seek_points, sample_index, &nearby_seek_point_index, [](auto const& sample_index, auto const& seekpoint_candidate) {
return static_cast<i64>(sample_index) - static_cast<i64>(seekpoint_candidate.sample_index); // Subtraction with i64 cast may cause overflow.
if (sample_index > seekpoint_candidate.sample_index)
return 1;
if (sample_index == seekpoint_candidate.sample_index)
return 0;
return -1;
}); });
// Binary search will always give us a close index, but it may be too large or too small. // Binary search will always give us a close index, but it may be too large or too small.
// By doing the index adjustment in this order, we will always find a seek point before the given sample. // By doing the index adjustment in this order, we will always find a seek point before the given sample.