TheSpectacularSyncEngine icon indicating copy to clipboard operation
TheSpectacularSyncEngine copied to clipboard

if tempo negative, crash..

Open designerfuzzi opened this issue 2 years ago • 0 comments

spotted some flaw.. https://github.com/michaeltyson/TheSpectacularSyncEngine/blob/61568ac9c045dc34ad5378c0bf9b4ebb3ddbb024/Syncy/SEMetronome.m#L115-L121

Line: 120: runtime error: -1.84462e+19 is outside the range of representable values of type 'unsigned long long'

when the enclosing if statement checks for _timeBase it should never happen, but it does.. so another value became negative So either tempo is or can become negative here or mach_absolute_time() is able to report negative values which is extremely unlikely. but still uint64_t types can not hold negative results, it must raise the error. It also causes the midi receiver to have wrong alignment of packets, because the timestamps become malicious.

to address it i came up with this, it should never ever result in any negative _timeBase

if ( _timeBase ) {
    // Scale time base to new tempo, so our relative timeline position remains the same (as it is dependent on tempo)
    double ratio = _tempo / tempo;
    uint64_t now = SECurrentTimeInHostTicks();
    uint64_t subtract = (now - _timeBase) * fabs(ratio); //fabs() so -_tempo or -tempo don't matter
    _timeBase = now - (subtract >= now ? 0 : subtract); //in case of subtracting more then passed time, just don't subtract
}

designerfuzzi avatar Dec 08 '21 00:12 designerfuzzi