Skip to content

Commit

Permalink
fixup! Fix issues reported by clang-tidy
Browse files Browse the repository at this point in the history
  • Loading branch information
Danielius1922 committed Jul 25, 2022
1 parent 3dd072b commit 467cfda
Show file tree
Hide file tree
Showing 9 changed files with 78 additions and 48 deletions.
6 changes: 3 additions & 3 deletions api/oc_buffer.c
Original file line number Diff line number Diff line change
Expand Up @@ -188,16 +188,16 @@ OC_PROCESS_THREAD(message_buffer_handler, ev, data)
data);
} else {
OC_DBG("Inbound network event: decrypted request");
oc_process_post(&coap_engine, oc_events[INBOUND_RI_EVENT], data);
oc_process_post(&g_coap_engine, oc_events[INBOUND_RI_EVENT], data);
}
#else /* OC_OSCORE */
OC_DBG("Inbound network event: decrypted request");
oc_process_post(&coap_engine, oc_events[INBOUND_RI_EVENT], data);
oc_process_post(&g_coap_engine, oc_events[INBOUND_RI_EVENT], data);
#endif /* OC_OSCORE */
}
#else /* OC_SECURITY */
OC_DBG("Inbound network event: decrypted request");
oc_process_post(&coap_engine, oc_events[INBOUND_RI_EVENT], data);
oc_process_post(&g_coap_engine, oc_events[INBOUND_RI_EVENT], data);
#endif /* !OC_SECURITY */
} else if (ev == oc_events[OUTBOUND_NETWORK_EVENT]) {
oc_message_t *message = (oc_message_t *)data;
Expand Down
7 changes: 4 additions & 3 deletions api/oc_helpers.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
#include "oc_helpers.h"
#include "port/oc_assert.h"
#include "port/oc_log.h"
#include <math.h>
#include <stdbool.h>

static bool mmem_initialized = false;
Expand Down Expand Up @@ -289,8 +288,10 @@ oc_conv_hex_string_to_byte_array(const char *hex_str, size_t hex_str_len,
return -1;
}

long len = lround((double)hex_str_len / 2.0);

long len = hex_str_len / 2;
if ((hex_str_len % 2) != 0) {
++len;
}
if (len < 0 || *array_len < (size_t)len) {
return -1;
}
Expand Down
4 changes: 2 additions & 2 deletions api/oc_ri.c
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,7 @@ start_processes(void)
allocate_events();
oc_process_start(&oc_etimer_process, NULL);
oc_process_start(&g_timed_callback_events, NULL);
oc_process_start(&coap_engine, NULL);
oc_process_start(&g_coap_engine, NULL);
oc_process_start(&message_buffer_handler, NULL);

#ifdef OC_SECURITY
Expand All @@ -388,7 +388,7 @@ stop_processes(void)
oc_process_exit(&oc_network_events);
oc_process_exit(&oc_etimer_process);
oc_process_exit(&g_timed_callback_events);
oc_process_exit(&coap_engine);
oc_process_exit(&g_coap_engine);

#ifdef OC_SECURITY
#ifdef OC_OSCORE
Expand Down
5 changes: 4 additions & 1 deletion messaging/coap/coap.c
Original file line number Diff line number Diff line change
Expand Up @@ -671,7 +671,10 @@ coap_oscore_parse_options(void *packet, uint8_t *data, uint32_t data_len,
if (!outer || !oscore) {
return BAD_OPTION_4_02;
}
coap_parse_oscore_option(coap_pkt, current_option, option_length);
if (coap_parse_oscore_option(coap_pkt, current_option, option_length) !=
0) {
return BAD_OPTION_4_02;
}
break;
#endif /* OC_OSCORE && OC_SECURITY */
case COAP_OPTION_CONTENT_FORMAT:
Expand Down
29 changes: 15 additions & 14 deletions messaging/coap/engine.c
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,6 @@
*/

#include "engine.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "api/oc_events.h"
#include "api/oc_main.h"
Expand All @@ -72,7 +69,11 @@
#include "coap_signal.h"
#endif

OC_PROCESS(coap_engine, "CoAP Engine");
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

OC_PROCESS(g_coap_engine, "CoAP Engine");

#ifdef OC_BLOCK_WISE
extern bool oc_ri_invoke_coap_entity_handler(
Expand All @@ -92,16 +93,16 @@ extern bool oc_ri_invoke_coap_entity_handler(void *request, void *response,
// match is found, the message is dropped as it must be
// a duplicate.
#define OC_REQUEST_HISTORY_SIZE (25)
static uint16_t history[OC_REQUEST_HISTORY_SIZE];
static uint8_t history_dev[OC_REQUEST_HISTORY_SIZE];
static uint8_t idx;
static uint16_t g_history[OC_REQUEST_HISTORY_SIZE];
static uint32_t g_history_dev[OC_REQUEST_HISTORY_SIZE];
static uint8_t g_idx = 0;

bool
oc_coap_check_if_duplicate(uint16_t mid, uint8_t device)
oc_coap_check_if_duplicate(uint16_t mid, uint32_t device)
{
size_t i;
for (i = 0; i < OC_REQUEST_HISTORY_SIZE; i++) {
if (history[i] == mid && history_dev[i] == device) {
if (g_history[i] == mid && g_history_dev[i] == device) {
OC_DBG("dropping duplicate request");
return true;
}
Expand Down Expand Up @@ -311,12 +312,12 @@ coap_receive(oc_message_t *msg)
} else {
#ifdef OC_REQUEST_HISTORY
if (oc_coap_check_if_duplicate(message->mid,
(uint8_t)msg->endpoint.device)) {
(uint32_t)msg->endpoint.device)) {
return 0;
}
history[idx] = message->mid;
history_dev[idx] = (uint8_t)msg->endpoint.device;
idx = (idx + 1) % OC_REQUEST_HISTORY_SIZE;
g_history[g_idx] = message->mid;
g_history_dev[g_idx] = (uint32_t)msg->endpoint.device;
g_idx = (g_idx + 1) % OC_REQUEST_HISTORY_SIZE;
#endif /* OC_REQUEST_HISTORY */
if (href_len == 7 && memcmp(href, "oic/res", 7) == 0) {
coap_udp_init_message(response, COAP_TYPE_CON, CONTENT_2_05,
Expand Down Expand Up @@ -948,7 +949,7 @@ coap_init_engine(void)
coap_register_as_transaction_handler();
}
/*---------------------------------------------------------------------------*/
OC_PROCESS_THREAD(coap_engine, ev, data)
OC_PROCESS_THREAD(g_coap_engine, ev, data)
{
OC_PROCESS_BEGIN();

Expand Down
5 changes: 3 additions & 2 deletions messaging/coap/engine.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,17 +52,18 @@
#include "observe.h"
#include "separate.h"
#include "transactions.h"
#include <stdint.h>

#ifdef __cplusplus
extern "C" {
#endif

OC_PROCESS_NAME(coap_engine);
OC_PROCESS_NAME(g_coap_engine);

void coap_init_engine(void);
/*---------------------------------------------------------------------------*/
int coap_receive(oc_message_t *message);
bool oc_coap_check_if_duplicate(uint16_t mid, uint8_t device);
bool oc_coap_check_if_duplicate(uint16_t mid, uint32_t device);

#ifdef __cplusplus
}
Expand Down
61 changes: 42 additions & 19 deletions messaging/coap/oscore.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include "coap.h"
#include "coap_signal.h"
#include "oc_ri.h"
#include <stdint.h>

void
oscore_send_error(const coap_packet_t *packet, uint8_t code,
Expand Down Expand Up @@ -234,49 +235,71 @@ coap_parse_oscore_option(coap_packet_t *packet, const uint8_t *current_option,
return 0;
}
/* Flags |0 0 0|h|k| n | */
packet->oscore_flags = *current_option;
uint8_t oscore_flags = *current_option;
current_option++;
option_length--;
OC_DBG("\tflags: %02x", packet->oscore_flags);
OC_DBG("\tflags: %02x", oscore_flags);

/* Partial IV length (n bytes) */
packet->piv_len = (packet->oscore_flags & OSCORE_FLAGS_PIVLEN_BITMASK);

if (packet->piv_len > 0) {
uint8_t piv[OSCORE_PIV_LEN];
uint8_t piv_len = (oscore_flags & OSCORE_FLAGS_PIVLEN_BITMASK);
if (piv_len > 0) {
/* Partial IV */
memcpy(packet->piv, current_option, packet->piv_len);
current_option += packet->piv_len;
option_length -= packet->piv_len;
memcpy(piv, current_option, piv_len);
current_option += piv_len;
option_length -= piv_len;

OC_DBG("\tPartial IV:");
OC_LOGbytes(packet->piv, packet->piv_len);
OC_LOGbytes(piv, piv_len);
}

/* kid context (if any) */
/* Check if 'h' flag bit is set */
if (packet->oscore_flags & OSCORE_FLAGS_KIDCTX_BITMASK) {
packet->kid_ctx_len = *current_option;
uint8_t kid_ctx[OSCORE_IDCTX_LEN];
uint8_t kid_ctx_len = 0;
if ((oscore_flags & OSCORE_FLAGS_KIDCTX_BITMASK) != 0) {
kid_ctx_len = *current_option;
current_option++;
option_length--;

/* Store kid context */
memcpy(packet->kid_ctx, current_option, packet->kid_ctx_len);
current_option += packet->kid_ctx_len;
option_length -= packet->kid_ctx_len;
memcpy(kid_ctx, current_option, kid_ctx_len);
current_option += kid_ctx_len;
option_length -= kid_ctx_len;

OC_DBG("\tkid context:");
OC_LOGbytes(packet->kid_ctx, packet->kid_ctx_len);
OC_LOGbytes(kid_ctx, kid_ctx_len);
}

/* kid (if any) */
/* Check if 'k' flag bit is set */
if (packet->oscore_flags & OSCORE_FLAGS_KID_BITMASK) {
uint8_t kid[OSCORE_CTXID_LEN];
uint8_t kid_len = 0;
if ((oscore_flags & OSCORE_FLAGS_KID_BITMASK) != 0) {
if (option_length > UINT8_MAX) {
OC_ERR("oscore: invalid option length for kid(%zu)", option_length);
return -1;
}
/* Remaining bytes in option: kid */
packet->kid_len = option_length;
memcpy(packet->kid, current_option, option_length);
kid_len = (uint8_t)option_length;
memcpy(kid, current_option, option_length);

OC_DBG("\tkid:");
OC_LOGbytes(packet->kid, packet->kid_len);
OC_LOGbytes(kid, kid_len);
}

packet->oscore_flags = oscore_flags;
packet->piv_len = piv_len;
if (piv_len > 0) {
memcpy(packet->piv, piv, piv_len);
}
packet->kid_ctx_len = kid_ctx_len;
if (kid_ctx_len > 0) {
memcpy(packet->kid_ctx, kid_ctx, kid_ctx_len);
}
packet->kid_len = kid_len;
if (kid_len > 0) {
memcpy(packet->kid, kid, kid_len);
}

return 0;
Expand Down
5 changes: 3 additions & 2 deletions security/oc_oscore_engine.c
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,8 @@ oscore_parse_message(oc_message_t *message)

if (oscore_pkt.transport_type == COAP_TRANSPORT_UDP &&
oscore_pkt.code <= OC_FETCH &&
oc_coap_check_if_duplicate(oscore_pkt.mid, message->endpoint.device)) {
oc_coap_check_if_duplicate(oscore_pkt.mid,
(uint32_t)message->endpoint.device)) {
OC_DBG("dropping duplicate request");
return false;
}
Expand Down Expand Up @@ -331,7 +332,7 @@ oc_oscore_recv_message(oc_message_t *message)
OC_DBG("#################################");

/* Dispatch oc_message_t to the CoAP layer */
if (oc_process_post(&coap_engine, oc_events[INBOUND_RI_EVENT], message) ==
if (oc_process_post(&g_coap_engine, oc_events[INBOUND_RI_EVENT], message) ==
OC_PROCESS_ERR_FULL) {
goto oscore_recv_error;
}
Expand Down
4 changes: 2 additions & 2 deletions security/oc_tls.c
Original file line number Diff line number Diff line change
Expand Up @@ -2279,7 +2279,7 @@ read_application_data_tcp(oc_tls_peer_t *peer)
(int)(total_length));
peer->processed_recv_message->encrypted = 0;
memcpy(peer->processed_recv_message->endpoint.di.id, peer->uuid.id, 16);
if (oc_process_post(&coap_engine, oc_events[INBOUND_RI_EVENT],
if (oc_process_post(&g_coap_engine, oc_events[INBOUND_RI_EVENT],
peer->processed_recv_message) ==
OC_PROCESS_ERR_FULL) {
oc_message_unref(peer->processed_recv_message);
Expand Down Expand Up @@ -2437,7 +2437,7 @@ read_application_data(oc_tls_peer_t *peer)
#endif /* !OC_INOUT_BUFFER_SIZE */
}
#else /* OC_OSCORE */
if (oc_process_post(&coap_engine, oc_events[INBOUND_RI_EVENT], msg) ==
if (oc_process_post(&g_coap_engine, oc_events[INBOUND_RI_EVENT], msg) ==
OC_PROCESS_ERR_FULL) {
#ifndef OC_INOUT_BUFFER_SIZE
oc_message_unref(msg);
Expand Down

0 comments on commit 467cfda

Please sign in to comment.