Skip to content

Commit

Permalink
Fix issues reported by clang-tidy
Browse files Browse the repository at this point in the history
Fixed issues:
- clang-analyzer-deadcode.DeadStores
- bugprone-branch-clone
- some of bugprone-narrowing-conversions, 
  bugprone-implicit-widening-of-multiplication-result,
  bugprone-signed-char-misuse,
  clang-analyzer-security.insecureAPI.*
  • Loading branch information
Danielius1922 authored Jun 15, 2023
1 parent 9ad771b commit b2b66ef
Show file tree
Hide file tree
Showing 106 changed files with 1,228 additions and 764 deletions.
4 changes: 2 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -381,8 +381,8 @@ if(MINGW)
endif()

# force the use mingw_printf because the default doesn't support '%zu'
list(APPEND PRIVATE_COMPILE_DEFINITIONS "__USE_MINGW_ANSI_STDIO=1")
list(APPEND TEST_COMPILE_DEFINITIONS "__USE_MINGW_ANSI_STDIO=1")
list(APPEND PRIVATE_COMPILE_DEFINITIONS "__USE_MINGW_ANSI_STDIO=(1)")
list(APPEND TEST_COMPILE_DEFINITIONS "__USE_MINGW_ANSI_STDIO=(1)")
endif()

if(MSVC)
Expand Down
30 changes: 13 additions & 17 deletions api/c-timestamp/timestamp_format.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#include "timestamp.h"
#include "util/oc_compiler.h"
#include <assert.h>
#include <stddef.h>

Expand Down Expand Up @@ -64,12 +66,6 @@ static const uint32_t Pow10[10] = { 1, 10, 100, 1000,
10000, 100000, 1000000, 10000000,
100000000, 1000000000 };

#if defined(__clang__) || defined(__GNUC__)
#define FALLTHROUGH __attribute__((fallthrough))
#else
#define FALLTHROUGH
#endif

static size_t
timestamp_format_internal(char *dst, size_t len, const timestamp_t *tsp,
const int precision)
Expand All @@ -87,8 +83,8 @@ timestamp_format_internal(char *dst, size_t len, const timestamp_t *tsp,
return 0;
}

uint64_t sec = tsp->sec + tsp->offset * 60 + EPOCH;
uint64_t rdn = sec / 86400;
int64_t sec = tsp->sec + tsp->offset * 60L + EPOCH;
int64_t rdn = sec / 86400;
assert(rdn < UINT32_MAX);
uint16_t y;
uint16_t m;
Expand Down Expand Up @@ -139,38 +135,38 @@ timestamp_format_internal(char *dst, size_t len, const timestamp_t *tsp,
case 9:
p[9] = '0' + (v % 10);
v /= 10;
FALLTHROUGH;
OC_FALLTHROUGH;
case 8:
p[8] = '0' + (v % 10);
v /= 10;
FALLTHROUGH;
OC_FALLTHROUGH;
case 7:
p[7] = '0' + (v % 10);
v /= 10;
FALLTHROUGH;
OC_FALLTHROUGH;
case 6:
p[6] = '0' + (v % 10);
v /= 10;
FALLTHROUGH;
OC_FALLTHROUGH;
case 5:
p[5] = '0' + (v % 10);
v /= 10;
FALLTHROUGH;
OC_FALLTHROUGH;
case 4:
p[4] = '0' + (v % 10);
v /= 10;
FALLTHROUGH;
OC_FALLTHROUGH;
case 3:
p[3] = '0' + (v % 10);
v /= 10;
FALLTHROUGH;
OC_FALLTHROUGH;
case 2:
p[2] = '0' + (v % 10);
v /= 10;
FALLTHROUGH;
OC_FALLTHROUGH;
case 1:
p[1] = '0' + (v % 10);
FALLTHROUGH;
OC_FALLTHROUGH;
default:
break;
}
Expand Down
10 changes: 5 additions & 5 deletions api/c-timestamp/timestamp_parse.c
Original file line number Diff line number Diff line change
Expand Up @@ -151,18 +151,18 @@ parse_time(const unsigned char *str, uint16_t *hour, uint16_t *min,
static bool
parse_offset(const unsigned char *str, bool minus, int16_t *offset)
{
uint16_t hour;
uint16_t min;
if (str[2] != ':') {
return false;
}

uint16_t hour;
uint16_t min;
if (parse_2d(str, 0, &hour) || hour > 23 || parse_2d(str, 3, &min) ||
min > 59) {
return false;
}

int16_t o = hour * 60 + min;
int16_t o = (int16_t)(hour * 60 + min);
if (minus) {
o *= -1;
}
Expand Down Expand Up @@ -243,8 +243,8 @@ timestamp_parse(const char *str, size_t len, timestamp_t *tsp)
return 1;
}

tsp->sec = ((int64_t)rdn - 719163) * 86400 + sod - offset * 60;
tsp->nsec = nsec;
tsp->sec = ((int64_t)rdn - 719163) * 86400 + sod - (offset * 60L);
tsp->nsec = (int32_t)nsec;
tsp->offset = offset;
return 0;
}
2 changes: 1 addition & 1 deletion api/c-timestamp/timestamp_tm.c
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ timestamp_to_tm(const timestamp_t *tsp, struct tm *tmp, const bool local)

uint64_t sec = tsp->sec + RDN_OFFSET;
if (local) {
sec += tsp->offset * 60;
sec += tsp->offset * 60L;
}
assert((sec / 86400) <= UINT32_MAX);
uint32_t rdn = (uint32_t)(sec / 86400);
Expand Down
5 changes: 3 additions & 2 deletions api/c-timestamp/timestamp_valid.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,10 @@
bool
timestamp_valid(const timestamp_t *tsp)
{
const int64_t sec = tsp->sec + tsp->offset * 60;
const int64_t sec = tsp->sec + tsp->offset * 60L;
if (sec < MIN_SEC || sec > MAX_SEC || tsp->nsec < 0 ||
tsp->nsec > 999999999 || tsp->offset < -1439 || tsp->offset > 1439)
tsp->nsec > 999999999 || tsp->offset < -1439 || tsp->offset > 1439) {
return false;
}
return true;
}
25 changes: 14 additions & 11 deletions api/client/oc_client_cb.c
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ oc_ri_alloc_client_cb(const char *uri, const oc_endpoint_t *endpoint,
oc_random_buffer(cb->token, cb->token_len);
cb->discovery = false;
cb->timestamp = oc_clock_time();
cb->observe_seq = -1;
cb->observe_seq = OC_COAP_OPTION_OBSERVE_NOT_SET;
if (endpoint != NULL) {
oc_endpoint_copy(&cb->endpoint, endpoint);
}
Expand Down Expand Up @@ -237,7 +237,7 @@ client_cb_notify_with_code(oc_client_cb_t *cb, oc_status_t code)
memset(&client_response, 0, sizeof(oc_client_response_t));
client_response.client_cb = cb;
client_response.endpoint = &cb->endpoint;
client_response.observe_option = -1;
client_response.observe_option = OC_COAP_OPTION_OBSERVE_NOT_SET;
client_response.user_data = cb->user_data;
client_response.code = code;

Expand Down Expand Up @@ -332,7 +332,7 @@ ri_prepare_client_response(const coap_packet_t *packet,
memset(&client_response, 0, sizeof(oc_client_response_t));
client_response.client_cb = cb;
client_response.endpoint = endpoint;
client_response.observe_option = -1;
client_response.observe_option = OC_COAP_OPTION_OBSERVE_NOT_SET;
client_response.content_format = cf;
client_response.user_data = cb->user_data;

Expand All @@ -351,7 +351,7 @@ ri_prepare_client_response(const coap_packet_t *packet,
}
#else /* !OC_BLOCK_WISE */
(void)response_state;
coap_get_header_observe(packet, (uint32_t *)&client_response.observe_option);
coap_get_header_observe(packet, &client_response.observe_option);
#endif /* OC_BLOCK_WISE */

return client_response;
Expand All @@ -364,10 +364,11 @@ ri_client_cb_set_observe_seq(oc_client_cb_t *cb, int observe_seq,
cb->observe_seq = observe_seq;

// Drop old observe callback and keep the last one.
if (cb->observe_seq == 0) {
if (cb->observe_seq == OC_COAP_OPTION_OBSERVE_REGISTER) {
oc_client_cb_t *dup_cb = (oc_client_cb_t *)oc_list_head(g_client_cbs);
while (dup_cb != NULL) {
if (dup_cb != cb && dup_cb->observe_seq != -1 &&
if (dup_cb != cb &&
dup_cb->observe_seq != OC_COAP_OPTION_OBSERVE_NOT_SET &&
dup_cb->token_len == cb->token_len &&
memcmp(dup_cb->token, cb->token, cb->token_len) == 0 &&
oc_string_is_equal(&dup_cb->uri, &cb->uri) &&
Expand Down Expand Up @@ -408,7 +409,8 @@ oc_client_cb_invoke(const coap_packet_t *response, oc_client_cb_t *cb,
#endif /* OC_BLOCK_WISE */

#if defined(OC_OSCORE) && defined(OC_SECURITY)
if (client_response.observe_option > 1) {
if (client_response.observe_option >=
OC_COAP_OPTION_OBSERVE_SEQUENCE_START_VALUE) {
uint64_t notification_num = 0;
oscore_read_piv(endpoint->piv, endpoint->piv_len, &notification_num);
if (notification_num < cb->notification_num) {
Expand All @@ -419,17 +421,17 @@ oc_client_cb_invoke(const coap_packet_t *response, oc_client_cb_t *cb,
#endif /* OC_OSCORE && OC_SECURITY */

const uint8_t *payload = NULL;
int payload_len = 0;
size_t payload_len = 0;
#ifdef OC_BLOCK_WISE
if (*response_state != NULL) {
payload = (*response_state)->buffer;
payload_len = (int)(*response_state)->payload_size;
payload_len = (*response_state)->payload_size;
}
#else /* OC_BLOCK_WISE */
payload_len = coap_get_payload(response, (const uint8_t **)&payload);
#endif /* !OC_BLOCK_WISE */
client_response._payload = payload;
client_response._payload_len = (size_t)payload_len;
client_response._payload_len = payload_len;

bool separate = false;
if (payload_len) {
Expand Down Expand Up @@ -491,7 +493,8 @@ oc_client_cb_invoke(const coap_packet_t *response, oc_client_cb_t *cb,

cb->ref_count = 0;

if (client_response.observe_option == -1 && !separate && !cb->discovery) {
if (client_response.observe_option == OC_COAP_OPTION_OBSERVE_NOT_SET &&
!separate && !cb->discovery) {
if (cb->multicast) {
if (cb->stop_multicast_receive) {
uint16_t mid = cb->mid;
Expand Down
57 changes: 33 additions & 24 deletions api/cloud/oc_cloud_manager.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@
#include <assert.h>
#include <stdint.h>

#define MILLISECONDS_PER_SECOND (1000)
#define MILLISECONDS_PER_MINUTE (60 * MILLISECONDS_PER_SECOND)
#define MILLISECONDS_PER_HOUR (60 * MILLISECONDS_PER_MINUTE)

static void cloud_start_process(oc_cloud_context_t *ctx);
static oc_event_callback_retval_t cloud_manager_reconnect_async(void *data);
static oc_event_callback_retval_t cloud_manager_register_async(void *data);
Expand Down Expand Up @@ -186,23 +190,26 @@ cloud_start_process(oc_cloud_context_t *ctx)
_oc_signal_event_loop();
}

static uint16_t
check_expires_in(int64_t expires_in)
static uint64_t
refresh_token_expires_in_ms(int64_t expires_in_ms)
{
if (expires_in <= 0) {
if (expires_in_ms <= 0) {
return 0;
}
if (expires_in > 60 * 60) {
// if time is more than 1h then set expires to (expires_in - 10min).
expires_in = expires_in - 10 * 60;
} else if (expires_in > 4 * 60) {
// if time is more than 240sec then set expires to (expires_in - 2min).
expires_in = expires_in - 2 * 60;
} else if (expires_in > 20) {
// if time is more than 20sec then set expires to (expires_in - 10sec).
expires_in = expires_in - 10;

if (expires_in_ms > (int64_t)MILLISECONDS_PER_HOUR) {
// if time is more than 1h then set expires to (expires_in_ms - 10min).
return (uint64_t)(expires_in_ms - (int64_t)(10 * MILLISECONDS_PER_MINUTE));
}
if (expires_in_ms > (int64_t)(4 * MILLISECONDS_PER_MINUTE)) {
// if time is more than 240sec then set expires to (expires_in_ms - 2min).
return (uint64_t)(expires_in_ms - (int64_t)(2 * MILLISECONDS_PER_MINUTE));
}
if (expires_in_ms > (int64_t)(20 * MILLISECONDS_PER_SECOND)) {
// if time is more than 20sec then set expires to (expires_in_ms - 10sec).
return (uint64_t)(expires_in_ms - (int64_t)(10 * MILLISECONDS_PER_SECOND));
}
return expires_in > UINT16_MAX ? UINT16_MAX : (uint16_t)expires_in;
return (uint64_t)expires_in_ms;
}

static bool
Expand Down Expand Up @@ -530,14 +537,14 @@ on_keepalive_response_default(oc_cloud_context_t *ctx, bool response_received,
uint64_t *next_ping)
{
if (response_received) {
*next_ping = 20 * 1000;
*next_ping = 20UL * MILLISECONDS_PER_SECOND;
ctx->retry_count = 0;
} else {
*next_ping = 4 * 1000;
*next_ping = 4UL * MILLISECONDS_PER_SECOND;
uint64_t keepalive_ping_timeout_ms =
((uint64_t)(ctx->keepalive.ping_timeout)) * 1000;
((uint64_t)(ctx->keepalive.ping_timeout)) * MILLISECONDS_PER_SECOND;
// we don't want to ping more often than once per second
if (keepalive_ping_timeout_ms >= (*next_ping + 1000)) {
if (keepalive_ping_timeout_ms >= (*next_ping + MILLISECONDS_PER_SECOND)) {
*next_ping = (keepalive_ping_timeout_ms - *next_ping);
}
++ctx->retry_count;
Expand All @@ -559,12 +566,12 @@ on_keepalive_response(oc_cloud_context_t *ctx, bool response_received,
}
if (!ok) {
OC_ERR("[CM] keepalive failed");
} else {
OC_DBG("[CM] keepalive sends the next ping in %llu milliseconds with %u "
"seconds timeout",
(long long unsigned)*next_ping, ctx->keepalive.ping_timeout);
return false;
}
return ok;
OC_DBG("[CM] keepalive sends the next ping in %llu milliseconds with %u "
"seconds timeout",
(long long unsigned)*next_ping, ctx->keepalive.ping_timeout);
return true;
}

static void
Expand All @@ -583,8 +590,10 @@ cloud_manager_login_handler(oc_client_response_t *data)
on_keepalive_response(ctx, true, &next_ping);
oc_reset_delayed_callback_ms(ctx, cloud_manager_send_ping_async, next_ping);
if (ctx->store.expires_in > 0) {
oc_reset_delayed_callback(ctx, cloud_manager_refresh_token_async,
check_expires_in(ctx->store.expires_in));
oc_reset_delayed_callback_ms(
ctx, cloud_manager_refresh_token_async,
refresh_token_expires_in_ms(ctx->store.expires_in *
MILLISECONDS_PER_SECOND));
}
goto finish;
}
Expand Down
4 changes: 2 additions & 2 deletions api/cloud/oc_cloud_store.c
Original file line number Diff line number Diff line change
Expand Up @@ -133,10 +133,10 @@ cloud_store_dump_internal(const char *store_name, const oc_cloud_store_t *store)
uint8_t *buf = malloc(OC_MIN_APP_DATA_SIZE);
if (!buf)
return -1;
oc_rep_new_realloc(&buf, OC_MIN_APP_DATA_SIZE, OC_MAX_APP_DATA_SIZE);
oc_rep_new_realloc_v1(&buf, OC_MIN_APP_DATA_SIZE, OC_MAX_APP_DATA_SIZE);
#else /* OC_DYNAMIC_ALLOCATION */
uint8_t buf[OC_MIN_APP_DATA_SIZE];
oc_rep_new(buf, OC_MIN_APP_DATA_SIZE);
oc_rep_new_v1(buf, sizeof(buf));
#endif /* !OC_DYNAMIC_ALLOCATION */

// Dumping cloud and accesspoint information.
Expand Down
Loading

0 comments on commit b2b66ef

Please sign in to comment.