From 9305033e0290a73b79a29900c5b9f35f77d880b1 Mon Sep 17 00:00:00 2001 From: Matthias Ringwald Date: Mon, 1 Feb 2021 14:49:27 +0100 Subject: [PATCH] MISRAC2012-Rule-14.4_c: compare against 0 or NULL --- src/ble/att_db.c | 18 +++++++------- src/ble/att_server.c | 14 +++++------ src/ble/gatt-service/battery_service_server.c | 2 +- .../gatt-service/nordic_spp_service_server.c | 4 ++-- .../gatt-service/ublox_spp_service_server.c | 4 ++-- src/ble/gatt_client.c | 2 +- src/ble/le_device_db_tlv.c | 24 +++++++++---------- src/ble/sm.c | 16 ++++++------- src/btstack_crypto.c | 2 +- src/btstack_linked_list.c | 2 +- src/btstack_ring_buffer.c | 4 ++-- src/btstack_slip.c | 2 +- src/btstack_util.c | 2 +- src/hci_dump.c | 4 ++-- src/hci_transport_h4.c | 2 +- src/l2cap.c | 2 +- 16 files changed, 52 insertions(+), 52 deletions(-) diff --git a/src/ble/att_db.c b/src/ble/att_db.c index 0cd5ba4de..395c4d26f 100644 --- a/src/ble/att_db.c +++ b/src/ble/att_db.c @@ -675,7 +675,7 @@ static uint16_t handle_read_request2(att_connection_t * att_connection, uint8_t // check security requirements uint8_t error_code = att_validate_security(att_connection, ATT_READ, &it); - if (error_code) { + if (error_code != 0) { return setup_error(response_buffer, request_type, handle, error_code); } @@ -723,7 +723,7 @@ static uint16_t handle_read_blob_request2(att_connection_t * att_connection, uin // check security requirements uint8_t error_code = att_validate_security(att_connection, ATT_READ, &it); - if (error_code) { + if (error_code != 0) { return setup_error(response_buffer, request_type, handle, error_code); } @@ -798,7 +798,7 @@ static uint16_t handle_read_multiple_request2(att_connection_t * att_connection, // check security requirements error_code = att_validate_security(att_connection, ATT_READ, &it); - if (error_code) break; + if (error_code != 0) break; att_update_value_len(&it, att_connection->con_handle); @@ -814,7 +814,7 @@ static uint16_t handle_read_multiple_request2(att_connection_t * att_connection, offset += bytes_copied; } - if (error_code){ + if (error_code != 0){ return setup_error(response_buffer, request_type, handle, error_code); } @@ -989,7 +989,7 @@ static uint16_t handle_write_request(att_connection_t * att_connection, uint8_t } // check security requirements int error_code = att_validate_security(att_connection, ATT_WRITE, &it); - if (error_code) { + if (error_code != 0) { return setup_error(response_buffer, request_type, handle, error_code); } att_persistent_ccc_cache(&it); @@ -999,7 +999,7 @@ static uint16_t handle_write_request(att_connection_t * att_connection, uint8_t if (error_code == ATT_ERROR_WRITE_RESPONSE_PENDING) return ATT_INTERNAL_WRITE_RESPONSE_PENDING; #endif - if (error_code) { + if (error_code != 0) { return setup_error(response_buffer, request_type, handle, error_code); } response_buffer[0] = ATT_WRITE_RESPONSE; @@ -1032,7 +1032,7 @@ static uint16_t handle_prepare_write_request(att_connection_t * att_connection, } // check security requirements int error_code = att_validate_security(att_connection, ATT_WRITE, &it); - if (error_code) { + if (error_code != 0) { return setup_error(response_buffer, request_type, handle, error_code); } @@ -1091,7 +1091,7 @@ static uint16_t handle_execute_write_request(att_connection_t * att_connection, if (att_prepare_write_error_code == ATT_ERROR_WRITE_RESPONSE_PENDING) return ATT_INTERNAL_WRITE_RESPONSE_PENDING; #endif // deliver queued errors - if (att_prepare_write_error_code){ + if (att_prepare_write_error_code != 0){ att_clear_transaction_queue(att_connection); uint8_t error_code = att_prepare_write_error_code; uint16_t handle = att_prepare_write_error_handle; @@ -1408,7 +1408,7 @@ bool att_is_persistent_ccc(uint16_t handle){ uint16_t att_read_callback_handle_blob(const uint8_t * blob, uint16_t blob_size, uint16_t offset, uint8_t * buffer, uint16_t buffer_size){ btstack_assert(blob != NULL); - if (buffer){ + if (buffer != NULL){ uint16_t bytes_to_copy = 0; if (blob_size >= offset){ bytes_to_copy = btstack_min(blob_size - offset, buffer_size); diff --git a/src/ble/att_server.c b/src/ble/att_server.c index 5a48a3c02..64ddf8e6c 100644 --- a/src/ble/att_server.c +++ b/src/ble/att_server.c @@ -159,7 +159,7 @@ static void att_handle_value_indication_notify_client(uint8_t status, uint16_t c static void att_emit_event_to_all(const uint8_t * event, uint16_t size){ // dispatch to app level handler - if (att_client_packet_handler){ + if (att_client_packet_handler != NULL){ (*att_client_packet_handler)(HCI_EVENT_PACKET, 0, (uint8_t*) event, size); } @@ -923,7 +923,7 @@ static void att_server_persistent_ccc_write(hci_con_handle_t con_handle, uint16_ if (entry.att_handle != att_handle) continue; // found matching entry - if (value){ + if (value != 0){ // update if (entry.value == value) { log_info("CCC Index %u: Up-to-date", index); @@ -952,7 +952,7 @@ static void att_server_persistent_ccc_write(hci_con_handle_t con_handle, uint16_ } uint32_t tag_to_use = 0; - if (tag_for_empty){ + if (tag_for_empty != 0){ tag_to_use = tag_for_empty; } else if (tag_for_lowest_seq_nr){ tag_to_use = tag_for_lowest_seq_nr; @@ -1048,19 +1048,19 @@ static att_service_handler_t * att_service_handler_for_handle(uint16_t handle){ } static att_read_callback_t att_server_read_callback_for_handle(uint16_t handle){ att_service_handler_t * handler = att_service_handler_for_handle(handle); - if (handler) return handler->read_callback; + if (handler != NULL) return handler->read_callback; return att_server_client_read_callback; } static att_write_callback_t att_server_write_callback_for_handle(uint16_t handle){ att_service_handler_t * handler = att_service_handler_for_handle(handle); - if (handler) return handler->write_callback; + if (handler != NULL) return handler->write_callback; return att_server_client_write_callback; } static btstack_packet_handler_t att_server_packet_handler_for_handle(uint16_t handle){ att_service_handler_t * handler = att_service_handler_for_handle(handle); - if (handler) return handler->packet_handler; + if (handler != NULL) return handler->packet_handler; return att_client_packet_handler; } @@ -1085,7 +1085,7 @@ static uint8_t att_validate_prepared_write(hci_con_handle_t con_handle){ att_service_handler_t * handler = (att_service_handler_t*) btstack_linked_list_iterator_next(&it); if (!handler->write_callback) continue; uint8_t error_code = (*handler->write_callback)(con_handle, 0, ATT_TRANSACTION_MODE_VALIDATE, 0, NULL, 0); - if (error_code) return error_code; + if (error_code != 0) return error_code; } if (!att_server_client_write_callback) return 0; return (*att_server_client_write_callback)(con_handle, 0, ATT_TRANSACTION_MODE_VALIDATE, 0, NULL, 0); diff --git a/src/ble/gatt-service/battery_service_server.c b/src/ble/gatt-service/battery_service_server.c index af6512a01..61e80720c 100644 --- a/src/ble/gatt-service/battery_service_server.c +++ b/src/ble/gatt-service/battery_service_server.c @@ -117,7 +117,7 @@ void battery_service_server_init(uint8_t value){ void battery_service_server_set_battery_value(uint8_t value){ battery_value = value; - if (battery_value_client_configuration){ + if (battery_value_client_configuration != 0){ battery_callback.callback = &battery_service_can_send_now; battery_callback.context = (void*) (uintptr_t) battery_value_client_configuration_connection; att_server_register_can_send_now_callback(&battery_callback, battery_value_client_configuration_connection); diff --git a/src/ble/gatt-service/nordic_spp_service_server.c b/src/ble/gatt-service/nordic_spp_service_server.c index a08016430..ffbcfff9f 100644 --- a/src/ble/gatt-service/nordic_spp_service_server.c +++ b/src/ble/gatt-service/nordic_spp_service_server.c @@ -71,7 +71,7 @@ static uint16_t nordic_spp_service_read_callback(hci_con_handle_t con_handle, ui UNUSED(buffer_size); if (attribute_handle == nordic_spp_tx_client_configuration_handle){ - if (buffer){ + if (buffer != NULL){ little_endian_store_16(buffer, 0, nordic_spp_tx_client_configuration_value); } return 2; @@ -89,7 +89,7 @@ static int nordic_spp_service_write_callback(hci_con_handle_t con_handle, uint16 } if (attribute_handle == nordic_spp_tx_client_configuration_handle){ nordic_spp_tx_client_configuration_value = little_endian_read_16(buffer, 0); - if (nordic_spp_tx_client_configuration_value){ + if (nordic_spp_tx_client_configuration_value != 0){ client_callback(con_handle, NULL, 0); } } diff --git a/src/ble/gatt-service/ublox_spp_service_server.c b/src/ble/gatt-service/ublox_spp_service_server.c index 017316aa3..c4417287b 100644 --- a/src/ble/gatt-service/ublox_spp_service_server.c +++ b/src/ble/gatt-service/ublox_spp_service_server.c @@ -117,14 +117,14 @@ static uint16_t ublox_spp_service_read_callback(hci_con_handle_t con_handle, uin if (!instance) return 0; if (attribute_handle == instance->fifo_client_configuration_descriptor_handle){ - if (buffer){ + if (buffer != NULL){ little_endian_store_16(buffer, 0, instance->fifo_client_configuration_descriptor_value); } return 2; } if (attribute_handle == instance->credits_client_configuration_descriptor_handle){ - if (buffer){ + if (buffer != NULL){ little_endian_store_16(buffer, 0, instance->credits_client_configuration_descriptor_value); } return 2; diff --git a/src/ble/gatt_client.c b/src/ble/gatt_client.c index 7529e9797..b9e10459a 100644 --- a/src/ble/gatt_client.c +++ b/src/ble/gatt_client.c @@ -159,7 +159,7 @@ static gatt_client_t * gatt_client_get_context_for_handle(uint16_t handle){ // returns existing one, or tries to setup new one static gatt_client_t * gatt_client_provide_context_for_handle(hci_con_handle_t con_handle){ gatt_client_t * gatt_client = gatt_client_get_context_for_handle(con_handle); - if (gatt_client) return gatt_client; + if (gatt_client != NULL) return gatt_client; // bail if no such hci connection hci_connection_t * hci_connection = hci_connection_for_handle(con_handle); diff --git a/src/ble/le_device_db_tlv.c b/src/ble/le_device_db_tlv.c index a37b4ff23..8f8ae2c62 100644 --- a/src/ble/le_device_db_tlv.c +++ b/src/ble/le_device_db_tlv.c @@ -291,9 +291,9 @@ void le_device_db_info(int index, int * addr_type, bd_addr_t addr, sm_key_t irk) } // setup return values - if (addr_type) *addr_type = entry.addr_type; - if (addr) (void)memcpy(addr, entry.addr, 6); - if (irk) (void)memcpy(irk, entry.irk, 16); + if (addr_type != NULL) *addr_type = entry.addr_type; + if (addr != NULL) (void)memcpy(addr, entry.addr, 6); + if (irk != NULL) (void)memcpy(irk, entry.irk, 16); } void le_device_db_encryption_set(int index, uint16_t ediv, uint8_t rand[8], sm_key_t ltk, int key_size, int authenticated, int authorized, int secure_connection){ @@ -307,8 +307,8 @@ void le_device_db_encryption_set(int index, uint16_t ediv, uint8_t rand[8], sm_k log_info("LE Device DB set encryption for %u, ediv x%04x, key size %u, authenticated %u, authorized %u, secure connection %u", index, ediv, key_size, authenticated, authorized, secure_connection); entry.ediv = ediv; - if (rand) (void)memcpy(entry.rand, rand, 8); - if (ltk) (void)memcpy(entry.ltk, ltk, 16); + if (rand != 0) (void)memcpy(entry.rand, rand, 8); + if (ltk != 0) (void)memcpy(entry.ltk, ltk, 16); entry.key_size = key_size; entry.authenticated = authenticated; entry.authorized = authorized; @@ -331,13 +331,13 @@ void le_device_db_encryption_get(int index, uint16_t * ediv, uint8_t rand[8], sm // update user fields log_info("LE Device DB encryption for %u, ediv x%04x, keysize %u, authenticated %u, authorized %u, secure connection %u", index, entry.ediv, entry.key_size, entry.authenticated, entry.authorized, entry.secure_connection); - if (ediv) *ediv = entry.ediv; - if (rand) (void)memcpy(rand, entry.rand, 8); - if (ltk) (void)memcpy(ltk, entry.ltk, 16); - if (key_size) *key_size = entry.key_size; - if (authenticated) *authenticated = entry.authenticated; - if (authorized) *authorized = entry.authorized; - if (secure_connection) *secure_connection = entry.secure_connection; + if (ediv != NULL) *ediv = entry.ediv; + if (rand != NULL) (void)memcpy(rand, entry.rand, 8); + if (ltk != NULL) (void)memcpy(ltk, entry.ltk, 16); + if (key_size != NULL) *key_size = entry.key_size; + if (authenticated != NULL) *authenticated = entry.authenticated; + if (authorized != NULL) *authorized = entry.authorized; + if (secure_connection != NULL) *secure_connection = entry.secure_connection; } #ifdef ENABLE_LE_SIGNED_WRITE diff --git a/src/ble/sm.c b/src/ble/sm.c index 22be3e93b..ba1ac42c7 100644 --- a/src/ble/sm.c +++ b/src/ble/sm.c @@ -1137,7 +1137,7 @@ static void sm_init_setup(sm_connection_t * sm_conn){ // query client for Legacy Pairing OOB data setup->sm_have_oob_data = 0; - if (sm_get_oob_data) { + if (sm_get_oob_data != NULL) { setup->sm_have_oob_data = (*sm_get_oob_data)(sm_conn->sm_peer_addr_type, sm_conn->sm_peer_address, setup->sm_tk); } @@ -1146,7 +1146,7 @@ static void sm_init_setup(sm_connection_t * sm_conn){ memset(setup->sm_ra, 0, 16); memset(setup->sm_rb, 0, 16); if (setup->sm_have_oob_data && (sm_auth_req & SM_AUTHREQ_SECURE_CONNECTION)){ - if (sm_get_sc_oob_data){ + if (sm_get_sc_oob_data != NULL){ if (IS_RESPONDER(sm_conn->sm_role)){ setup->sm_have_oob_data = (*sm_get_sc_oob_data)( sm_conn->sm_peer_addr_type, @@ -2690,7 +2690,7 @@ static void sm_run(void){ err = test_pairing_failure; } #endif - if (err){ + if (err != 0){ setup->sm_pairing_failed_reason = err; connection->sm_engine_state = SM_GENERAL_SEND_PAIRING_FAILED; sm_trigger_run(); @@ -3360,7 +3360,7 @@ static void sm_validate_er_ir(void){ static void sm_handle_random_result_ir(void *arg){ sm_persistent_keys_random_active = 0; - if (arg){ + if (arg != NULL){ // key generated, store in tlv int status = sm_tlv_impl->store_tag(sm_tlv_context, BTSTACK_TAG32('S','M','I','R'), sm_persistent_ir, 16u); log_info("Generated IR key. Store in TLV status: %d", status); @@ -3379,7 +3379,7 @@ static void sm_handle_random_result_ir(void *arg){ static void sm_handle_random_result_er(void *arg){ sm_persistent_keys_random_active = 0; - if (arg){ + if (arg != 0){ // key generated, store in tlv int status = sm_tlv_impl->store_tag(sm_tlv_context, BTSTACK_TAG32('S','M','E','R'), sm_persistent_er, 16u); log_info("Generated ER key. Store in TLV status: %d", status); @@ -3420,7 +3420,7 @@ static void sm_event_packet_handler (uint8_t packet_type, uint16_t channel, uint // setup IR/ER with TLV btstack_tlv_get_instance(&sm_tlv_impl, &sm_tlv_context); - if (sm_tlv_impl){ + if (sm_tlv_impl != NULL){ int key_size = sm_tlv_impl->get_tag(sm_tlv_context, BTSTACK_TAG32('S','M','E','R'), sm_persistent_er, 16u); if (key_size == 16){ // ok, let's continue @@ -3874,7 +3874,7 @@ static void sm_pdu_handler(uint8_t packet_type, hci_con_handle_t con_handle, uin } #endif - if (err){ + if (err != 0){ setup->sm_pairing_failed_reason = err; sm_conn->sm_engine_state = SM_GENERAL_SEND_PAIRING_FAILED; break; @@ -3984,7 +3984,7 @@ static void sm_pdu_handler(uint8_t packet_type, hci_con_handle_t con_handle, uin // validate public key err = btstack_crypto_ecc_p256_validate_public_key(setup->sm_peer_q); - if (err){ + if (err != 0){ log_error("sm: peer public key invalid %x", err); sm_pairing_error(sm_conn, SM_REASON_DHKEY_CHECK_FAILED); break; diff --git a/src/btstack_crypto.c b/src/btstack_crypto.c index 49cbf3cf4..c0c6f0daf 100644 --- a/src/btstack_crypto.c +++ b/src/btstack_crypto.c @@ -1282,7 +1282,7 @@ int btstack_crypto_ecc_p256_validate_public_key(const uint8_t * public_key){ mbedtls_ecp_point_free( & Q); #endif - if (err){ + if (err != 0){ log_error("public key invalid %x", err); } return err; diff --git a/src/btstack_linked_list.c b/src/btstack_linked_list.c index 453c38905..39e5f4c85 100644 --- a/src/btstack_linked_list.c +++ b/src/btstack_linked_list.c @@ -60,7 +60,7 @@ btstack_linked_item_t * btstack_linked_list_get_last_item(btstack_linked_list_t btstack_linked_item_t *lastItem = NULL; btstack_linked_item_t *it; for (it = *list; it != NULL; it = it->next){ - if (it) { + if (it != NULL) { lastItem = it; } } diff --git a/src/btstack_ring_buffer.c b/src/btstack_ring_buffer.c index f4b869b4a..826019a3e 100644 --- a/src/btstack_ring_buffer.c +++ b/src/btstack_ring_buffer.c @@ -104,7 +104,7 @@ int btstack_ring_buffer_write(btstack_ring_buffer_t * ring_buffer, uint8_t * dat } // copy second chunk - if (data_length) { + if (data_length != 0) { (void)memcpy(&ring_buffer->storage[0], data, data_length); ring_buffer->last_written_index += data_length; } @@ -140,7 +140,7 @@ void btstack_ring_buffer_read(btstack_ring_buffer_t * ring_buffer, uint8_t * dat } // copy second chunk - if (data_length) { + if (data_length != 0) { (void)memcpy(data, &ring_buffer->storage[0], data_length); ring_buffer->last_read_index += data_length; } diff --git a/src/btstack_slip.c b/src/btstack_slip.c index 5fd5113ef..ba88470dd 100644 --- a/src/btstack_slip.c +++ b/src/btstack_slip.c @@ -201,7 +201,7 @@ void btstack_slip_decoder_process(uint8_t input){ case SLIP_DECODER_ACTIVE: switch(input){ case BTSTACK_SLIP_SOF: - if (decoder_pos){ + if (decoder_pos != 0){ decoder_state = SLIP_DECODER_COMPLETE; } else { btstack_slip_decoder_reset(); diff --git a/src/btstack_util.c b/src/btstack_util.c index eb4ecfc72..94aa11377 100644 --- a/src/btstack_util.c +++ b/src/btstack_util.c @@ -383,7 +383,7 @@ int sscanf_bd_addr(const char * addr_string, bd_addr_t addr){ } } - if (result){ + if (result != 0){ bd_addr_copy(addr, buffer); } return result; diff --git a/src/hci_dump.c b/src/hci_dump.c index 66098a41c..3a4ecb3d8 100644 --- a/src/hci_dump.c +++ b/src/hci_dump.c @@ -211,14 +211,14 @@ static void printf_packet(uint8_t packet_type, uint8_t in, uint8_t * packet, uin printf("EVT <= "); break; case HCI_ACL_DATA_PACKET: - if (in) { + if (in != 0) { printf("ACL <= "); } else { printf("ACL => "); } break; case HCI_SCO_DATA_PACKET: - if (in) { + if (in != 0) { printf("SCO <= "); } else { printf("SCO => "); diff --git a/src/hci_transport_h4.c b/src/hci_transport_h4.c index 83d9a24d1..795f77d8b 100644 --- a/src/hci_transport_h4.c +++ b/src/hci_transport_h4.c @@ -425,7 +425,7 @@ static void hci_transport_h4_init(const void * transport_config){ static int hci_transport_h4_open(void){ // open uart driver int res = btstack_uart->open(); - if (res){ + if (res != 0){ return res; } diff --git a/src/l2cap.c b/src/l2cap.c index 1d469a78c..a374dfc8b 100644 --- a/src/l2cap.c +++ b/src/l2cap.c @@ -3091,7 +3091,7 @@ static int l2cap_le_signaling_handler_dispatch(hci_con_handle_t handle, uint8_t // check size if (len < 8u) return 0u; connection = hci_connection_for_handle(handle); - if (connection){ + if (connection != NULL){ if (connection->role != HCI_ROLE_MASTER){ // reject command without notifying upper layer when not in master role return 0;