Skip to content

Commit

Permalink
Fix ASAN warnings about overflows in bit shifts
Browse files Browse the repository at this point in the history
Gcc sanitizer (-fsanitize=shift) complains that the expression "1 << 31"
overflows. It is mostly a false warning since the result always is
used as a unsigned variable.

However, replacing the expression by "1U << 31" is more rigorous and is
sufficient to make ASAN happy.

Signed-off-by: Jérôme Pouiller <[email protected]>
  • Loading branch information
jerome-pouiller authored and Juha Heiskanen committed Aug 5, 2021
1 parent f998008 commit c808661
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 17 deletions.
14 changes: 7 additions & 7 deletions source/6LoWPAN/ws/ws_bootstrap.c
Original file line number Diff line number Diff line change
Expand Up @@ -553,7 +553,7 @@ static uint8_t ws_generate_exluded_channel_list_from_active_channels(ws_excluded
memset(excluded_data, 0, sizeof(ws_excluded_channel_data_t));

for (uint8_t i = 0; i < number_of_channels; i++) {
if (!(global_channel_mask[0 + (i / 32)] & (1 << (i % 32)))) {
if (!(global_channel_mask[0 + (i / 32)] & (1U << (i % 32)))) {
//Global exluded channel
if (active_range) {
//Mark range stop here
Expand All @@ -562,15 +562,15 @@ static uint8_t ws_generate_exluded_channel_list_from_active_channels(ws_excluded
continue;
}

if (selected_channel_mask[0 + (i / 32)] & (1 << (i % 32))) {
if (selected_channel_mask[0 + (i / 32)] & (1U << (i % 32))) {
if (active_range) {
//Mark range stop here
active_range = false;
}
} else {
//Mark excluded channel
//Swap Order already here
excluded_data->channel_mask[0 + (i / 32)] |= 1 << (31 - (i % 32));
excluded_data->channel_mask[ 0 + (i / 32)] |= 1U << (31 - (i % 32));
excluded_data->excluded_channel_count++;

if (excluded_data->excluded_range_length < WS_EXCLUDED_MAX_RANGE_TO_SEND) {
Expand Down Expand Up @@ -707,7 +707,7 @@ static int8_t ws_fhss_border_router_configure(protocol_interface_info_entry_t *c

static bool ws_channel_allowed(uint8_t channel, uint32_t *channel_mask)
{
if ((1 << (channel % 32)) & (channel_mask[channel / 32])) {
if ((1U << (channel % 32)) & (channel_mask[channel / 32])) {
return true;
}
return false;
Expand Down Expand Up @@ -1358,8 +1358,8 @@ static void ws_bootstrap_decode_exclude_range_to_mask_by_range(void *mask_buffer
//channel_index = 0;
}
if (channel >= range_start && channel <= range_stop) {
//mask_ptr[mask_index] |= 1 << (31 - channel_index);
mask_ptr[0 + (channel / 32)] |= 1 << (31 - (channel % 32));
//mask_ptr[mask_index] |= 1U << (31 - channel_index);
mask_ptr[0 + (channel / 32)] |= 1U << (31 - (channel % 32));
} else if (channel > range_stop) {
break;
}
Expand Down Expand Up @@ -3506,7 +3506,7 @@ static void ws_set_asynch_channel_list(protocol_interface_info_entry_t *cur, asy
if (cur->ws_info->cfg->fhss.fhss_uc_channel_function == WS_FIXED_CHANNEL) {
//SET 1 Channel only
uint16_t channel_number = cur->ws_info->cfg->fhss.fhss_uc_fixed_channel;
async_req->channel_list.channel_mask[0 + (channel_number / 32)] = (1 << (channel_number % 32));
async_req->channel_list.channel_mask[0 + (channel_number / 32)] = 1U << (channel_number % 32);
} else {
ws_generate_channel_list(async_req->channel_list.channel_mask, cur->ws_info->hopping_schdule.number_of_channels, cur->ws_info->hopping_schdule.regulatory_domain, cur->ws_info->hopping_schdule.operating_class, cur->ws_info->hopping_schdule.channel_plan_id);
}
Expand Down
6 changes: 3 additions & 3 deletions source/6LoWPAN/ws/ws_common.c
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ static int8_t ws_disable_channels_in_range(uint32_t *channel_mask, uint16_t numb
{
for (uint16_t i = 0; i < number_of_channels; i++) {
if (i >= range_start && i <= range_stop) {
channel_mask[0 + (i / 32)] &= ~(1 << (i % 32));
channel_mask[0 + (i / 32)] &= ~(1U << (i % 32));
}
}
return 0;
Expand All @@ -72,7 +72,7 @@ int8_t ws_generate_channel_list(uint32_t *channel_mask, uint16_t number_of_chann
}
// Enable all channels
for (uint16_t i = 0; i < number_of_channels; i++) {
channel_mask[0 + (i / 32)] |= (1 << (i % 32));
channel_mask[0 + (i / 32)] |= (1U << (i % 32));
}
// Disable unsupported channels per regional frequency bands
if (regulatory_domain == REG_DOMAIN_BZ) {
Expand Down Expand Up @@ -102,7 +102,7 @@ uint16_t ws_active_channel_count(uint32_t *channel_mask, uint16_t number_of_chan
uint16_t active_channels = 0;
// Set channel maks outside excluded channels
for (uint16_t i = 0; i < number_of_channels; i++) {
if (channel_mask[0 + (i / 32)] & (1 << (i % 32))) {
if (channel_mask[0 + (i / 32)] & (1U << (i % 32))) {
active_channels++;
}
}
Expand Down
4 changes: 2 additions & 2 deletions source/6LoWPAN/ws/ws_neighbor_class.c
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ static void ws_neighbour_excluded_mask_by_range(ws_channel_mask_t *channel_info,

if (channel >= range_start && channel <= range_stop) {
//Cut channel
compare_mask_bit = 1 << (channel % 32);
compare_mask_bit = 1U << (channel % 32);
mask_index = 0 + (channel / 32);

if (channel_info->channel_mask[mask_index] & compare_mask_bit) {
Expand All @@ -187,7 +187,7 @@ static uint32_t ws_reserve_order_32_bit(uint32_t value)
uint32_t ret_val = 0;
for (uint8_t i = 0; i < 32; i++) {
if ((value & (1 << i))) {
ret_val |= 1 << ((32 - 1) - i);
ret_val |= 1U << ((32 - 1) - i);
}
}
return ret_val;
Expand Down
6 changes: 3 additions & 3 deletions source/Service_Libs/fhss/channel_list.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ static bool channel_list_bit_test32(uint32_t word, int_fast8_t bit_number)
{
bool bitSet;

if (word & ((uint32_t) 1 << bit_number)) {
if (word & (1U << bit_number)) {
bitSet = true;
} else {
bitSet = false;
Expand Down Expand Up @@ -92,9 +92,9 @@ void channel_list_set_channel(uint32_t *list, int channel, bool active)
return;
}
if (active) {
list[channel / 32] |= (1 << channel % 32);
list[channel / 32] |= (1U << channel % 32);
} else {
list[channel / 32] &= ~(1 << channel % 32);
list[channel / 32] &= ~(1U << channel % 32);
}
return;
}
Expand Down
2 changes: 1 addition & 1 deletion source/Service_Libs/fhss/fhss_ws.c
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ static int32_t fhss_channel_index_from_mask(const uint32_t *channel_mask, int32_
int32_t active_channels = 0;
// Set channel maks outside excluded channels
for (int32_t i = 0; i < number_of_channels; i++) {
if (channel_mask[0 + (i / 32)] & (1 << (i % 32))) {
if (channel_mask[0 + (i / 32)] & (1U << (i % 32))) {
if (channel_index == active_channels) {
return i;
}
Expand Down
2 changes: 1 addition & 1 deletion source/libNET/src/ns_net.c
Original file line number Diff line number Diff line change
Expand Up @@ -768,7 +768,7 @@ static int arm_net_channel_bit_mask_to_number(const uint32_t *channel_mask)

for (j = 0; j < 8; j++) {
for (i = 0; i < 32; i++) {
if (channel_mask[j] & ((uint32_t)1 << i)) {
if (channel_mask[j] & (1U << i)) {
break;
}
}
Expand Down

0 comments on commit c808661

Please sign in to comment.