From c80866183673f289cb1c7d289a20d010e99aa4f5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=B4me=20Pouiller?= Date: Fri, 11 Jun 2021 09:55:48 +0200 Subject: [PATCH] Fix ASAN warnings about overflows in bit shifts MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 --- source/6LoWPAN/ws/ws_bootstrap.c | 14 +++++++------- source/6LoWPAN/ws/ws_common.c | 6 +++--- source/6LoWPAN/ws/ws_neighbor_class.c | 4 ++-- source/Service_Libs/fhss/channel_list.c | 6 +++--- source/Service_Libs/fhss/fhss_ws.c | 2 +- source/libNET/src/ns_net.c | 2 +- 6 files changed, 17 insertions(+), 17 deletions(-) diff --git a/source/6LoWPAN/ws/ws_bootstrap.c b/source/6LoWPAN/ws/ws_bootstrap.c index f95d4c9b88b6..5240257a5c20 100644 --- a/source/6LoWPAN/ws/ws_bootstrap.c +++ b/source/6LoWPAN/ws/ws_bootstrap.c @@ -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 @@ -562,7 +562,7 @@ 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; @@ -570,7 +570,7 @@ static uint8_t ws_generate_exluded_channel_list_from_active_channels(ws_excluded } 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) { @@ -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; @@ -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; } @@ -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); } diff --git a/source/6LoWPAN/ws/ws_common.c b/source/6LoWPAN/ws/ws_common.c index 42a9ceb764b4..d402679e8440 100644 --- a/source/6LoWPAN/ws/ws_common.c +++ b/source/6LoWPAN/ws/ws_common.c @@ -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; @@ -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) { @@ -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++; } } diff --git a/source/6LoWPAN/ws/ws_neighbor_class.c b/source/6LoWPAN/ws/ws_neighbor_class.c index 20d29cbc4d23..a3ebb068666e 100644 --- a/source/6LoWPAN/ws/ws_neighbor_class.c +++ b/source/6LoWPAN/ws/ws_neighbor_class.c @@ -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) { @@ -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; diff --git a/source/Service_Libs/fhss/channel_list.c b/source/Service_Libs/fhss/channel_list.c index a25ffe12b416..9af436a7caeb 100644 --- a/source/Service_Libs/fhss/channel_list.c +++ b/source/Service_Libs/fhss/channel_list.c @@ -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; @@ -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; } diff --git a/source/Service_Libs/fhss/fhss_ws.c b/source/Service_Libs/fhss/fhss_ws.c index 090520570bfa..16d48495b315 100644 --- a/source/Service_Libs/fhss/fhss_ws.c +++ b/source/Service_Libs/fhss/fhss_ws.c @@ -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; } diff --git a/source/libNET/src/ns_net.c b/source/libNET/src/ns_net.c index dd3883c6efb9..4bcbe3ce9a23 100644 --- a/source/libNET/src/ns_net.c +++ b/source/libNET/src/ns_net.c @@ -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; } }