Skip to content

region_us_915_get_next_channel() division by zero crash #151

@chmorgan

Description

@chmorgan

This code:

    ...
    if( *active_channel_nb == 0 )
    {
        SMTC_MODEM_HAL_PANIC( "NO CHANNELS AVAILABLE\n" );
    }

    // Select a channel in array
    uint8_t temp = ( smtc_modem_hal_get_random_nb_in_range( 0, ( *active_channel_nb - 1 ) ) ) % *active_channel_nb;
    uint8_t channel_idx = active_channel_index[temp];
    if( channel_idx >= NUMBER_OF_TX_CHANNEL_US_915 )
    {
        SMTC_MODEM_HAL_TRACE_ERROR( "INVALID CHANNEL  active channel = %d and random channel = %d \n",
                                    *active_channel_nb, temp );
        return ERRORLORAWAN;
    }
   ...

If no channels are available, active_channel_nb is zero, and this line appears to be the cause of a division by zero I'm seeing on occasion:

    uint8_t temp = ( smtc_modem_hal_get_random_nb_in_range( 0, ( *active_channel_nb - 1 ) ) ) % *active_channel_nb;

It does look like this code in region_us_915_get_join_next_channel() is avoiding this issue but it's unclear if this is the desired behavior in regiion_us_915_get_next_channel() though. Here is the snippet from region_us_915_get_join_next_channel():

   ...
        bank_tmp_cnt++;
    } while( ( *active_channel_nb == 0 ) && ( bank_tmp_cnt < BANK_MAX_US915 ) );

    if( *active_channel_nb == 0 )
    {
        SMTC_MODEM_HAL_TRACE_WARNING( "NO CHANNELS AVAILABLE \n" );
        return ERRORLORAWAN;
    }

    uint8_t temp        = 0xFF;
    ...

I also checked 4.9.0 and it doesn't look like this code changed much (or any) and I don't see any other bug reports along the same lines.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions