f4exb / dsdcc

Digital Speech Decoder (DSD) rewritten as a C++ library
286 stars 60 forks source link

Race condition in DSDSymbol #24

Closed f4exb closed 4 years ago

f4exb commented 5 years ago

This has been discovered in SDRangel because the decoder parameter settings and the sample feed run in different threads. It is reported here: https://github.com/f4exb/sdrangel/issues/406

This becomes an issue with this line of code: https://github.com/f4exb/dsdcc/blob/master/dsd_symbol.cpp#L265

This can cause a divide by zero if m_count is reset in a different thread. It could be possible to use mutexes but a simple guard code like this could fix the issue:

        if (m_count == 0) // out of sync for example because of race condition
        {
            m_sampleIndex = 0; // return to the beginning of a symbol
            return false;      // and wait for next sample
        }

In fact if m_count is 0 it means we should restart symbol search from the beginning and that no symbol was discovered.

f4exb commented 5 years ago

There is a second issue with DSDDecoder::setDecodeMode because it does not return to initial state after having changed the sync lookup settings. The same code used in the constructor should be used to reset initial state:

    resetFrameSync();
    noCarrier();
    m_squelchTimeoutCount = 0;
    m_nxdnInterSyncCount = -1; // reset to quiet state
f4exb commented 4 years ago

Fixed in v1.8.6