From 279d1aa5dc55599494ca55e1bea9c265c9ff86d9 Mon Sep 17 00:00:00 2001 From: Paul Davis Date: Tue, 29 Dec 2020 19:26:13 -0700 Subject: [PATCH] temporal: catch a special case when converting between superclock and beats --- libs/temporal/tempo.cc | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/libs/temporal/tempo.cc b/libs/temporal/tempo.cc index 27817ae428..e3bb175843 100644 --- a/libs/temporal/tempo.cc +++ b/libs/temporal/tempo.cc @@ -481,6 +481,22 @@ TempoPoint::superclocks_per_note_type_at (timepos_t const &pos) const Temporal::Beats TempoPoint::quarters_at_superclock (superclock_t sc) const { + /* catch a special case. The maximum superclock_t value cannot be + converted into a 32bit beat + 32 bit tick value for common tempos. + Obviously, values less than this can also cause overflow, but are + unlikely to be encountered. + + A longer term/big picture solution for this is likely required in + order to deal with longer sessions. Still, even at 300bpm, a 32 bit + integer should cover 165 days. The problem is that a 62 bit (int62_t) + superclock counter can cover 105064 days, so the theoretical + potential for errors here is real. + */ + + if (sc >= int62_t::max) { + return std::numeric_limits::max(); + } + if (!actually_ramped()) { /* convert sc into superbeats, given that sc represents some number of seconds */ const superclock_t whole_seconds = sc / superclock_ticks_per_second;