Skip to content

Commit d17d091

Browse files
committed
clamp track duration to bar range to avoid any possible overflow
1 parent ad2aa15 commit d17d091

File tree

1 file changed

+5
-7
lines changed

1 file changed

+5
-7
lines changed

src/displayapp/screens/Music.cpp

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -236,23 +236,21 @@ void Music::RefreshTrackInfo() {
236236

237237
void Music::UpdateLength() {
238238
int remaining = std::max(totalLength - currentPosition, 0);
239+
int range = std::min(totalLength > 0 ? totalLength : 1, static_cast<int>(std::numeric_limits<int16_t>::max()));
239240

240241
if (totalLength > (99 * 60 * 60)) {
241242
lv_label_set_text_static(txtCurrentPosition, "Inf");
242243
lv_label_set_text_static(txtTrackDuration, "Inf");
243244
} else if (totalLength > (99 * 60)) {
244245
lv_label_set_text_fmt(txtCurrentPosition, "%d:%02d", (currentPosition / (60 * 60)) % 100, ((currentPosition % (60 * 60)) / 60) % 100);
245246
lv_label_set_text_fmt(txtTrackDuration, "-%d:%02d", (remaining / (60 * 60)) % 100, ((remaining % (60 * 60)) / 60) % 100);
246-
// These conversions are narrowing: lv_bar_set_range accepts int16_t args
247-
// resolve by normalising?
248-
// same for else branch below
249-
lv_bar_set_range(barTrackDuration, 0, totalLength > 0 ? totalLength : 1);
250-
lv_bar_set_value(barTrackDuration, currentPosition, LV_ANIM_OFF);
247+
lv_bar_set_range(barTrackDuration, 0, range);
248+
lv_bar_set_value(barTrackDuration, std::min(currentPosition, range), LV_ANIM_OFF);
251249
} else {
252250
lv_label_set_text_fmt(txtCurrentPosition, "%d:%02d", (currentPosition / 60) % 100, (currentPosition % 60) % 100);
253251
lv_label_set_text_fmt(txtTrackDuration, "-%d:%02d", (remaining / 60) % 100, (remaining % 60) % 100);
254-
lv_bar_set_range(barTrackDuration, 0, totalLength > 0 ? totalLength : 1);
255-
lv_bar_set_value(barTrackDuration, currentPosition, LV_ANIM_OFF);
252+
lv_bar_set_range(barTrackDuration, 0, range);
253+
lv_bar_set_value(barTrackDuration, std::min(currentPosition, range), LV_ANIM_OFF);
256254
}
257255
}
258256

0 commit comments

Comments
 (0)