From a75377d014ad2f4703e35a3720ed04716daa47f0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?kleines=20Filmr=C3=B6llchen?= Date: Tue, 27 Jun 2023 20:29:52 +0200 Subject: [PATCH] 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. --- Userland/Libraries/LibAudio/GenericTypes.cpp | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/Userland/Libraries/LibAudio/GenericTypes.cpp b/Userland/Libraries/LibAudio/GenericTypes.cpp index 4c6225b1ef..d7fdc27a2d 100644 --- a/Userland/Libraries/LibAudio/GenericTypes.cpp +++ b/Userland/Libraries/LibAudio/GenericTypes.cpp @@ -27,7 +27,12 @@ Optional SeekTable::seek_point_before(u64 sample_index) const return {}; 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) { - return static_cast(sample_index) - static_cast(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. // By doing the index adjustment in this order, we will always find a seek point before the given sample.