From 4a94cc7c80e109942d8c91e231c8d5f2dfe527c3 Mon Sep 17 00:00:00 2001 From: Jon Shallow Date: Wed, 24 Apr 2024 11:00:25 +0100 Subject: [PATCH] scan-build: Fix warnings --- examples/coap-client.c | 98 ++++++++++++++--------------- include/coap3/coap_mutex_internal.h | 4 +- man/examples-code-check.c | 8 +-- src/coap_block.c | 14 ++--- src/coap_mbedtls.c | 5 +- src/coap_openssl.c | 6 +- src/coap_oscore.c | 9 +-- src/coap_pdu.c | 4 +- src/coap_resource.c | 8 +-- src/coap_session.c | 5 +- src/coap_wolfssl.c | 7 ++- src/oscore/oscore_cbor.c | 2 +- tests/test_error_response.c | 20 +++--- tests/test_pdu.c | 2 +- tests/test_sendqueue.c | 47 +++++++------- tests/test_session.c | 8 ++- tests/test_tls.c | 6 +- tests/test_wellknown.c | 6 +- 18 files changed, 137 insertions(+), 122 deletions(-) diff --git a/examples/coap-client.c b/examples/coap-client.c index 7b113a7d..7d3af2fe 100644 --- a/examples/coap-client.c +++ b/examples/coap-client.c @@ -1550,62 +1550,60 @@ get_session(coap_context_t *ctx, coap_session_t *session = NULL; is_mcast = coap_is_mcast(dst); - if (local_addr || coap_is_af_unix(dst)) { - if (coap_is_af_unix(dst)) { - coap_address_t bind_addr; + if (coap_is_af_unix(dst)) { + coap_address_t bind_addr; - if (local_addr) { - if (!coap_address_set_unix_domain(&bind_addr, - (const uint8_t *)local_addr, - strlen(local_addr))) { - fprintf(stderr, "coap_address_set_unix_domain: %s: failed\n", - local_addr); - return NULL; - } - } else { - char buf[COAP_UNIX_PATH_MAX]; - - /* Need a unique address */ - snprintf(buf, COAP_UNIX_PATH_MAX, - "/tmp/coap-client.%d", (int)getpid()); - if (!coap_address_set_unix_domain(&bind_addr, (const uint8_t *)buf, - strlen(buf))) { - fprintf(stderr, "coap_address_set_unix_domain: %s: failed\n", - buf); - remove(buf); - return NULL; - } - (void)remove(buf); - } - session = open_session(ctx, proto, &bind_addr, dst, - identity, identity_len, key, key_len); - } else { - coap_addr_info_t *info_list = NULL; - coap_addr_info_t *info; - coap_str_const_t local; - uint16_t port = local_port ? atoi(local_port) : 0; - - local.s = (const uint8_t *)local_addr; - local.length = strlen(local_addr); - /* resolve local address where data should be sent from */ - info_list = coap_resolve_address_info(&local, port, port, port, port, - AI_PASSIVE | AI_NUMERICHOST | AI_NUMERICSERV | AI_ALL, - 1 << scheme, - COAP_RESOLVE_TYPE_LOCAL); - if (!info_list) { - fprintf(stderr, "coap_resolve_address_info: %s: failed\n", local_addr); + if (local_addr) { + if (!coap_address_set_unix_domain(&bind_addr, + (const uint8_t *)local_addr, + strlen(local_addr))) { + fprintf(stderr, "coap_address_set_unix_domain: %s: failed\n", + local_addr); return NULL; } + } else { + char buf[COAP_UNIX_PATH_MAX]; - /* iterate through results until success */ - for (info = info_list; info != NULL; info = info->next) { - session = open_session(ctx, proto, &info->addr, dst, - identity, identity_len, key, key_len); - if (session) - break; + /* Need a unique address */ + snprintf(buf, COAP_UNIX_PATH_MAX, + "/tmp/coap-client.%d", (int)getpid()); + if (!coap_address_set_unix_domain(&bind_addr, (const uint8_t *)buf, + strlen(buf))) { + fprintf(stderr, "coap_address_set_unix_domain: %s: failed\n", + buf); + remove(buf); + return NULL; } - coap_free_address_info(info_list); + (void)remove(buf); } + session = open_session(ctx, proto, &bind_addr, dst, + identity, identity_len, key, key_len); + } else if (local_addr) { + coap_addr_info_t *info_list = NULL; + coap_addr_info_t *info; + coap_str_const_t local; + uint16_t port = local_port ? atoi(local_port) : 0; + + local.s = (const uint8_t *)local_addr; + local.length = strlen(local_addr); + /* resolve local address where data should be sent from */ + info_list = coap_resolve_address_info(&local, port, port, port, port, + AI_PASSIVE | AI_NUMERICHOST | AI_NUMERICSERV | AI_ALL, + 1 << scheme, + COAP_RESOLVE_TYPE_LOCAL); + if (!info_list) { + fprintf(stderr, "coap_resolve_address_info: %s: failed\n", local_addr); + return NULL; + } + + /* iterate through results until success */ + for (info = info_list; info != NULL; info = info->next) { + session = open_session(ctx, proto, &info->addr, dst, + identity, identity_len, key, key_len); + if (session) + break; + } + coap_free_address_info(info_list); } else if (local_port) { coap_address_t bind_addr; diff --git a/include/coap3/coap_mutex_internal.h b/include/coap3/coap_mutex_internal.h index 31fb2545..bdef378b 100644 --- a/include/coap3/coap_mutex_internal.h +++ b/include/coap3/coap_mutex_internal.h @@ -342,7 +342,9 @@ int coap_lock_lock_func(coap_lock_t *lock); } while (0) #define coap_lock_check_locked(s) do { \ - assert ((s) && (s)->lock.being_freed ? coap_thread_pid == (s)->lock.freeing_pid: coap_thread_pid == (s)->lock.pid); \ + assert((s) && \ + coap_thread_pid == ((s)->lock.being_freed ? (s)->lock.freeing_pid : \ + (s)->lock.pid)); \ } while (0) #define coap_lock_invert(s,func,f) do { \ diff --git a/man/examples-code-check.c b/man/examples-code-check.c index fe339a90..1c7b662f 100644 --- a/man/examples-code-check.c +++ b/man/examples-code-check.c @@ -187,6 +187,9 @@ check_synopsis(const char *file) { exit_code = WEXITSTATUS(status); snprintf(buffer, sizeof(buffer), "echo %sc ; cat -n %sc", file_name, file_name); status = system(buffer); + if (WEXITSTATUS(status)) { + printf("Issues with system() call\n"); + } snprintf(buffer, sizeof(buffer), "echo tmp/%s.h ; cat -n tmp/%s.h", file, file); status = system(buffer); if (WEXITSTATUS(status)) { @@ -573,13 +576,8 @@ main(int argc, char *argv[]) { fpheader = NULL; check_synopsis(pdir_ent->d_name); } - in_name = 0; - in_synopsis = 0; - in_return = 0; dump_name_synopsis_mismatch(); dump_return_synopsis_mismatch(); - in_functions = 0; - in_examples = 1; break; } diff --git a/src/coap_block.c b/src/coap_block.c index 46e04d98..0803ced5 100644 --- a/src/coap_block.c +++ b/src/coap_block.c @@ -1653,7 +1653,6 @@ coap_block_check_lg_srcv_timeouts(coap_session_t *session, coap_tick_t now, if (final_block > last_payload_block) { final_block = last_payload_block; } - no_blocks += final_block - block; } /* Ask for the missing blocks */ block = -1; @@ -1755,8 +1754,7 @@ coap_send_q_blocks(coap_session_t *session, coap_pdu_t *block_pdu = NULL; coap_opt_filter_t drop_options; coap_mid_t mid = COAP_INVALID_MID; - uint64_t token = coap_decode_var_bytes8(pdu->actual_token.s, - pdu->actual_token.length); + uint64_t token; const uint8_t *ptoken; uint8_t ltoken[8]; size_t ltoken_length; @@ -2189,6 +2187,7 @@ coap_block_new_lg_crcv(coap_session_t *session, coap_pdu_t *pdu, memcpy(lg_crcv->pdu.token, pdu->token, token_options); if (lg_crcv->pdu.data) { lg_crcv->pdu.data = lg_crcv->pdu.token + token_options; + assert(pdu->data); memcpy(lg_crcv->pdu.data, lg_xmit ? lg_xmit->data : pdu->data, data_len); } @@ -3121,9 +3120,9 @@ derive_cbor_value(const uint8_t **bp, size_t rem_len) { return (uint32_t)-1; value = **bp << 24; (*bp)++; - value = **bp << 16; + value |= **bp << 16; (*bp)++; - value = **bp << 8; + value |= **bp << 8; (*bp)++; value |= **bp; (*bp)++; @@ -3424,8 +3423,7 @@ coap_handle_response_send_block(coap_session_t *session, coap_pdu_t *sent, uint32_t i; uint8_t buf[8]; coap_pdu_t *pdu; - uint64_t token = coap_decode_var_bytes8(rcvd->actual_token.s, - rcvd->actual_token.length); + uint64_t token; uint8_t ltoken[8]; size_t ltoken_length; @@ -4049,7 +4047,6 @@ fail_resp: } #endif /* COAP_Q_BLOCK_SUPPORT */ have_block = 1; - block_opt = COAP_OPTION_BLOCK2; if (block.num != 0) { /* Assume random access and just give the single response to app */ size_t length; @@ -4065,7 +4062,6 @@ fail_resp: #if COAP_Q_BLOCK_SUPPORT else if (coap_get_block_b(session, rcvd, COAP_OPTION_Q_BLOCK2, &block)) { have_block = 1; - block_opt = COAP_OPTION_Q_BLOCK2; /* server indicating that it supports Q_BLOCK2 */ if (!(session->block_mode & COAP_BLOCK_HAS_Q_BLOCK)) { set_block_mode_has_q(session->block_mode); diff --git a/src/coap_mbedtls.c b/src/coap_mbedtls.c index 22558cf9..5ac8bbd0 100644 --- a/src/coap_mbedtls.c +++ b/src/coap_mbedtls.c @@ -1599,7 +1599,7 @@ coap_dtls_new_mbedtls_env(coap_session_t *c_session, MBEDTLS_SSL_MINOR_VERSION_3); #endif /* MBEDTLS_VERSION_NUMBER >= 0x03020000 */ - if ((ret = mbedtls_ssl_setup(&m_env->ssl, &m_env->conf)) != 0) { + if (mbedtls_ssl_setup(&m_env->ssl, &m_env->conf) != 0) { goto fail; } if (proto == COAP_PROTO_DTLS) { @@ -2181,8 +2181,6 @@ coap_dtls_receive(coap_session_t *c_session, if (ret == 1) { /* Just connected, so send the data */ coap_session_connected(c_session); - } else { - ret = -1; } } ret = -1; @@ -2625,6 +2623,7 @@ coap_digest_setup(void) { #else if (mbedtls_sha256_starts(digest_ctx, 0) != 0) { #endif /* MBEDTLS_2_X_COMPAT */ + coap_digest_free(digest_ctx); return NULL; } } diff --git a/src/coap_openssl.c b/src/coap_openssl.c index 07732526..03c9d91f 100644 --- a/src/coap_openssl.c +++ b/src/coap_openssl.c @@ -1334,7 +1334,7 @@ add_ca_to_cert_store(X509_STORE *st, X509 *x509) { long e; /* Flush out existing errors */ - while ((e = ERR_get_error()) != 0) { + while (ERR_get_error() != 0) { } if (!X509_STORE_add_cert(st, x509)) { @@ -3459,7 +3459,7 @@ coap_dtls_free_session(coap_session_t *session) { if (!SSL_in_init(ssl) && !(SSL_get_shutdown(ssl) & SSL_SENT_SHUTDOWN)) { int r = SSL_shutdown(ssl); if (r == 0) - r = SSL_shutdown(ssl); + SSL_shutdown(ssl); } SSL_free(ssl); session->tls = NULL; @@ -3864,7 +3864,7 @@ coap_tls_free_session(coap_session_t *session) { if (!SSL_in_init(ssl) && !(SSL_get_shutdown(ssl) & SSL_SENT_SHUTDOWN)) { int r = SSL_shutdown(ssl); if (r == 0) - r = SSL_shutdown(ssl); + SSL_shutdown(ssl); } SSL_free(ssl); session->tls = NULL; diff --git a/src/coap_oscore.c b/src/coap_oscore.c index e0fb2294..322c0e48 100644 --- a/src/coap_oscore.c +++ b/src/coap_oscore.c @@ -294,15 +294,15 @@ coap_oscore_new_pdu_encrypted(coap_session_t *session, uint8_t nonce_buffer[13]; coap_bin_const_t aad; coap_bin_const_t nonce; - oscore_recipient_ctx_t *rcp_ctx = session->recipient_ctx; - oscore_ctx_t *osc_ctx = rcp_ctx ? rcp_ctx->osc_ctx : NULL; + oscore_recipient_ctx_t *rcp_ctx; + oscore_ctx_t *osc_ctx; cose_encrypt0_t cose[1]; uint8_t group_flag = 0; int show_pdu = 0; int doing_observe = 0; uint32_t observe_value = 0; oscore_association_t *association = NULL; - oscore_sender_ctx_t *snd_ctx = osc_ctx ? osc_ctx->sender_context : NULL; + oscore_sender_ctx_t *snd_ctx; uint8_t external_aad_buffer[200]; coap_bin_const_t external_aad; uint8_t oscore_option[48]; @@ -337,6 +337,7 @@ coap_oscore_new_pdu_encrypted(coap_session_t *session, if (rcp_ctx == NULL) goto error; osc_ctx = rcp_ctx->osc_ctx; + assert(osc_ctx); snd_ctx = osc_ctx->sender_context; } else { /* @@ -1389,7 +1390,7 @@ coap_oscore_decrypt_pdu(coap_session_t *session, } /* Skip the options */ coap_option_iterator_init(plain_pdu, &opt_iter, COAP_OPT_ALL); - while ((opt = coap_option_next(&opt_iter))) { + while (coap_option_next(&opt_iter)) { } if (opt_iter.length > 0 && opt_iter.next_option && opt_iter.next_option[0] == COAP_PAYLOAD_START) { diff --git a/src/coap_pdu.c b/src/coap_pdu.c index 49a74730..3b056ef5 100644 --- a/src/coap_pdu.c +++ b/src/coap_pdu.c @@ -1350,7 +1350,7 @@ coap_pdu_parse_opt(coap_pdu_t *pdu) { outbuflen = sizeof(outbuf); obp = outbuf; ok = write_prefix(&obp, &outbuflen, "O: ", 3); - while (length > 0 && *opt != COAP_PAYLOAD_START) { + while (ok && length > 0 && *opt != COAP_PAYLOAD_START) { coap_opt_t *opt_last = opt; size_t optsize = next_option_safe(&opt, &length, &pdu->max_opt); const uint32_t len = @@ -1374,7 +1374,7 @@ coap_pdu_parse_opt(coap_pdu_t *pdu) { } } if (length && *opt == COAP_PAYLOAD_START) { - ok = ok && write_char(&obp, &outbuflen, *opt, i); + write_char(&obp, &outbuflen, *opt, i); } /* write_*() always leaves a spare byte to null terminate */ *obp = '\000'; diff --git a/src/coap_resource.c b/src/coap_resource.c index 9c6ec8dc..59d38a15 100644 --- a/src/coap_resource.c +++ b/src/coap_resource.c @@ -65,13 +65,12 @@ } static int -match(const coap_str_const_t *text, const coap_str_const_t *pattern, int match_prefix, - int match_substring - ) { +match(const coap_str_const_t *text, const coap_str_const_t *pattern, + int match_prefix, int match_substring) { assert(text); assert(pattern); - if (text->length < pattern->length) + if (text->length < pattern->length || !pattern->s) return 0; if (match_substring) { @@ -977,6 +976,7 @@ coap_delete_observers(coap_context_t *context, coap_session_t *session) { if (s->session == session) { if (context->observe_deleted) context->observe_deleted(session, s, context->observe_user_data); + assert(resource->subscribers); LL_DELETE(resource->subscribers, s); coap_session_release(session); coap_delete_pdu(s->pdu); diff --git a/src/coap_session.c b/src/coap_session.c index 0a6e743f..d105617e 100644 --- a/src/coap_session.c +++ b/src/coap_session.c @@ -809,7 +809,7 @@ coap_session_connected(coap_session_t *session) { coap_delete_node(q); if (bytes_written < 0) break; - } else { + } else if (q) { if (bytes_written <= 0 || (size_t)bytes_written < q->pdu->used_size + q->pdu->hdr_size) { q->next = session->delayqueue; session->delayqueue = q; @@ -1944,13 +1944,10 @@ coap_endpoint_str(const coap_endpoint_t *endpoint) { if (p + 6 < end) { if (endpoint->proto == COAP_PROTO_UDP) { strcpy(p, " UDP"); - p += 4; } else if (endpoint->proto == COAP_PROTO_DTLS) { strcpy(p, " DTLS"); - p += 5; } else { strcpy(p, " NONE"); - p += 5; } } diff --git a/src/coap_wolfssl.c b/src/coap_wolfssl.c index 1168b654..8d704bb9 100644 --- a/src/coap_wolfssl.c +++ b/src/coap_wolfssl.c @@ -513,6 +513,7 @@ coap_dgram_write(WOLFSSL *ssl, char *in, int inl, void *ctx) { coap_tick_t now; (void)ssl; + assert(data); if (data->session) { if (!coap_netif_available(data->session) #if COAP_SERVER_SUPPORT @@ -785,6 +786,7 @@ coap_sock_read(WOLFSSL *ssl, char *out, int outl, void *ctx) { coap_session_t *session = w_env ? w_env->data.session : NULL; (void)ssl; + assert(session); if (w_env && !w_env->done_psk_check && w_env->ssl && w_env->role == COAP_DTLS_ROLE_SERVER) { if (wolfSSL_SSL_in_init(w_env->ssl)) { @@ -823,6 +825,7 @@ coap_sock_write(WOLFSSL *ssl, char *in, int inl, void *ctx) { coap_session_t *session = w_env ? w_env->data.session : NULL; (void)ssl; + assert(session); ret = (int)session->sock.lfunc[COAP_LAYER_TLS].l_write(session, (const uint8_t *)in, inl); @@ -2076,7 +2079,7 @@ coap_dtls_free_session(coap_session_t *session) { if (!wolfSSL_SSL_in_init(ssl) && !(wolfSSL_get_shutdown(ssl) & WOLFSSL_SENT_SHUTDOWN)) { int r = wolfSSL_shutdown(ssl); if (r == 0) - r = wolfSSL_shutdown(ssl); + wolfSSL_shutdown(ssl); } w_env->ssl = NULL; wolfSSL_free(ssl); @@ -2558,7 +2561,7 @@ coap_tls_free_session(coap_session_t *session) { if (!wolfSSL_SSL_in_init(ssl) && !(wolfSSL_get_shutdown(ssl) & WOLFSSL_SENT_SHUTDOWN)) { int r = wolfSSL_shutdown(ssl); if (r == 0) - r = wolfSSL_shutdown(ssl); + wolfSSL_shutdown(ssl); } wolfSSL_free(ssl); w_env->ssl = NULL; diff --git a/src/oscore/oscore_cbor.c b/src/oscore/oscore_cbor.c index 4b31a567..5bc9ce59 100644 --- a/src/oscore/oscore_cbor.c +++ b/src/oscore/oscore_cbor.c @@ -239,7 +239,7 @@ oscore_cbor_get_next_element(const uint8_t **buffer, size_t *buf_len) { size_t oscore_cbor_get_element_size(const uint8_t **buffer, size_t *buf_len) { uint8_t control = get_byte(buffer, buf_len) & 0x1f; - size_t size = get_byte_inc(buffer, buf_len); + size_t size; if (control < 0x18) { size = (uint64_t)control; diff --git a/tests/test_error_response.c b/tests/test_error_response.c index aaa150fb..81b58318 100644 --- a/tests/test_error_response.c +++ b/tests/test_error_response.c @@ -19,6 +19,10 @@ static coap_pdu_t *pdu; /* Holds the request PDU for most tests */ static coap_opt_filter_t opts; /* option filter used for generating responses */ +#define ReturnIf_CU_ASSERT_PTR_NOT_NULL(value) \ + CU_ASSERT_PTR_NOT_NULL(value); \ + if ((void*)value == NULL) return; + /************************************************************************ ** PDU decoder ************************************************************************/ @@ -41,7 +45,7 @@ t_error_response1(void) { coap_option_filter_clear(&opts); response = coap_new_error_response(pdu, COAP_RESPONSE_CODE(400), &opts); - CU_ASSERT_PTR_NOT_NULL(response); + ReturnIf_CU_ASSERT_PTR_NOT_NULL(response); CU_ASSERT(response->used_size == sizeof(teststr) - 4); CU_ASSERT(response->type == COAP_MESSAGE_ACK); @@ -71,7 +75,7 @@ t_error_response2(void) { coap_option_filter_clear(&opts); response = coap_new_error_response(pdu, COAP_RESPONSE_CODE(404), &opts); - CU_ASSERT_PTR_NOT_NULL(response); + ReturnIf_CU_ASSERT_PTR_NOT_NULL(response); CU_ASSERT(response->used_size == sizeof(teststr) - 4); CU_ASSERT(response->type == COAP_MESSAGE_NON); @@ -104,7 +108,7 @@ t_error_response3(void) { coap_option_filter_set(&opts, 25); response = coap_new_error_response(pdu, code, &opts); - CU_ASSERT_PTR_NOT_NULL(response); + ReturnIf_CU_ASSERT_PTR_NOT_NULL(response); CU_ASSERT(response->used_size == sizeof(teststr) - 4); CU_ASSERT(response->type == COAP_MESSAGE_ACK); @@ -143,7 +147,7 @@ t_error_response4(void) { coap_option_filter_set(&opts, 25); response = coap_new_error_response(pdu, code, &opts); - CU_ASSERT_PTR_NOT_NULL(response); + ReturnIf_CU_ASSERT_PTR_NOT_NULL(response); CU_ASSERT(response->used_size == sizeof(teststr) - 4); CU_ASSERT(response->type == COAP_MESSAGE_ACK); @@ -184,7 +188,7 @@ t_error_response5(void) { coap_option_filter_set(&opts, 25); response = coap_new_error_response(pdu, code, &opts); - CU_ASSERT_PTR_NOT_NULL(response); + ReturnIf_CU_ASSERT_PTR_NOT_NULL(response); CU_ASSERT(response->used_size == sizeof(teststr) - 4); CU_ASSERT(response->type == COAP_MESSAGE_ACK); @@ -225,7 +229,7 @@ t_error_response6(void) { coap_option_filter_set(&opts, 23); response = coap_new_error_response(pdu, code, &opts); - CU_ASSERT_PTR_NOT_NULL(response); + ReturnIf_CU_ASSERT_PTR_NOT_NULL(response); CU_ASSERT(response->used_size == sizeof(teststr) - 4); CU_ASSERT(response->type == COAP_MESSAGE_ACK); @@ -267,7 +271,7 @@ t_error_response7(void) { coap_option_filter_set(&opts, 23); response = coap_new_error_response(pdu, code, &opts); - CU_ASSERT_PTR_NOT_NULL(response); + ReturnIf_CU_ASSERT_PTR_NOT_NULL(response); CU_ASSERT(response->used_size == sizeof(teststr) - 4); CU_ASSERT(response->type == COAP_MESSAGE_ACK); @@ -308,7 +312,7 @@ t_error_response8(void) { coap_option_filter_set(&opts, 1014); response = coap_new_error_response(pdu, code, &opts); - CU_ASSERT_PTR_NOT_NULL(response); + ReturnIf_CU_ASSERT_PTR_NOT_NULL(response); CU_ASSERT(response->used_size == sizeof(teststr) - 4); CU_ASSERT(response->type == COAP_MESSAGE_ACK); diff --git a/tests/test_pdu.c b/tests/test_pdu.c index 6656b39e..57d24e85 100644 --- a/tests/test_pdu.c +++ b/tests/test_pdu.c @@ -458,7 +458,7 @@ t_encode_pdu5(void) { CU_ASSERT(pdu->used_size == 0); - result = coap_add_token(pdu, 8, (const uint8_t *)"12345678"); + coap_add_token(pdu, 8, (const uint8_t *)"12345678"); CU_ASSERT(pdu->used_size == 8); diff --git a/tests/test_sendqueue.c b/tests/test_sendqueue.c index c4169c2c..3c4555b7 100644 --- a/tests/test_sendqueue.c +++ b/tests/test_sendqueue.c @@ -14,6 +14,11 @@ #include #if COAP_CLIENT_SUPPORT + +#define ReturnIf_CU_ASSERT_PTR_NOT_NULL(value) \ + CU_ASSERT_PTR_NOT_NULL(value); \ + if ((void*)value == NULL) return; + static coap_context_t *ctx; /* Holds the coap context for most tests */ static coap_session_t *session; /* Holds a reference-counted session object */ @@ -44,7 +49,7 @@ t_sendqueue1(void) { int result = coap_insert_node(&ctx->sendqueue, node[1]); CU_ASSERT(result > 0); - CU_ASSERT_PTR_NOT_NULL(ctx->sendqueue); + ReturnIf_CU_ASSERT_PTR_NOT_NULL(ctx->sendqueue); CU_ASSERT_PTR_EQUAL(ctx->sendqueue, node[1]); CU_ASSERT(node[1]->t == timestamp[1]); } @@ -74,8 +79,8 @@ t_sendqueue3(void) { CU_ASSERT_PTR_EQUAL(ctx->sendqueue, node[3]); CU_ASSERT(node[3]->t == timestamp[3]); - CU_ASSERT_PTR_NOT_NULL(ctx->sendqueue->next); - CU_ASSERT_PTR_NOT_NULL(ctx->sendqueue->next->next); + ReturnIf_CU_ASSERT_PTR_NOT_NULL(ctx->sendqueue->next); + ReturnIf_CU_ASSERT_PTR_NOT_NULL(ctx->sendqueue->next->next); CU_ASSERT(ctx->sendqueue->next->t == timestamp[1] - timestamp[3]); CU_ASSERT(ctx->sendqueue->next->next->t == timestamp[2] - timestamp[1]); @@ -92,13 +97,13 @@ t_sendqueue4(void) { CU_ASSERT_PTR_EQUAL(ctx->sendqueue, node[3]); - CU_ASSERT_PTR_NOT_NULL(ctx->sendqueue->next); + ReturnIf_CU_ASSERT_PTR_NOT_NULL(ctx->sendqueue->next); CU_ASSERT_PTR_EQUAL(ctx->sendqueue->next, node[1]); - CU_ASSERT_PTR_NOT_NULL(ctx->sendqueue->next->next); + ReturnIf_CU_ASSERT_PTR_NOT_NULL(ctx->sendqueue->next->next); CU_ASSERT_PTR_EQUAL(ctx->sendqueue->next->next, node[4]); - CU_ASSERT_PTR_NOT_NULL(ctx->sendqueue->next->next->next); + ReturnIf_CU_ASSERT_PTR_NOT_NULL(ctx->sendqueue->next->next->next); CU_ASSERT_PTR_EQUAL(ctx->sendqueue->next->next->next, node[2]); CU_ASSERT(ctx->sendqueue->next->t == timestamp[1] - timestamp[3]); @@ -132,7 +137,7 @@ t_sendqueue5(void) { result = coap_adjust_basetime(ctx, now); CU_ASSERT(result == 0); - CU_ASSERT_PTR_NOT_NULL(ctx->sendqueue); + ReturnIf_CU_ASSERT_PTR_NOT_NULL(ctx->sendqueue); CU_ASSERT(ctx->sendqueue_basetime == now); CU_ASSERT(ctx->sendqueue->t == timestamp[3] + delta1); @@ -140,13 +145,13 @@ t_sendqueue5(void) { result = coap_adjust_basetime(ctx, now); CU_ASSERT(result == 2); CU_ASSERT(ctx->sendqueue_basetime == now); - CU_ASSERT_PTR_NOT_NULL(ctx->sendqueue); + ReturnIf_CU_ASSERT_PTR_NOT_NULL(ctx->sendqueue); CU_ASSERT(ctx->sendqueue->t == 0); - CU_ASSERT_PTR_NOT_NULL(ctx->sendqueue->next); + ReturnIf_CU_ASSERT_PTR_NOT_NULL(ctx->sendqueue->next); CU_ASSERT(ctx->sendqueue->next->t == 0); - CU_ASSERT_PTR_NOT_NULL(ctx->sendqueue->next->next); + ReturnIf_CU_ASSERT_PTR_NOT_NULL(ctx->sendqueue->next->next); CU_ASSERT(ctx->sendqueue->next->next->t == delta2 - delta1 - timestamp[1]); /* restore timestamps of nodes in the sendqueue */ @@ -191,19 +196,19 @@ t_sendqueue7(void) { int result; coap_queue_t *tmp_node; - CU_ASSERT_PTR_NOT_NULL(ctx->sendqueue); + ReturnIf_CU_ASSERT_PTR_NOT_NULL(ctx->sendqueue); CU_ASSERT_PTR_EQUAL(ctx->sendqueue, node[3]); - CU_ASSERT_PTR_NOT_NULL(ctx->sendqueue->next); + ReturnIf_CU_ASSERT_PTR_NOT_NULL(ctx->sendqueue->next); CU_ASSERT_PTR_EQUAL(ctx->sendqueue->next, node[1]); result = coap_remove_from_queue(&ctx->sendqueue, session, 3, &tmp_node); CU_ASSERT(result == 1); - CU_ASSERT_PTR_NOT_NULL(tmp_node); + ReturnIf_CU_ASSERT_PTR_NOT_NULL(tmp_node); CU_ASSERT_PTR_EQUAL(tmp_node, node[3]); - CU_ASSERT_PTR_NOT_NULL(ctx->sendqueue); + ReturnIf_CU_ASSERT_PTR_NOT_NULL(ctx->sendqueue); CU_ASSERT_PTR_EQUAL(ctx->sendqueue, node[1]); CU_ASSERT(ctx->sendqueue->t == timestamp[1]); @@ -217,14 +222,14 @@ t_sendqueue8(void) { result = coap_remove_from_queue(&ctx->sendqueue, session, 4, &tmp_node); CU_ASSERT(result == 1); - CU_ASSERT_PTR_NOT_NULL(tmp_node); + ReturnIf_CU_ASSERT_PTR_NOT_NULL(tmp_node); CU_ASSERT_PTR_EQUAL(tmp_node, node[4]); - CU_ASSERT_PTR_NOT_NULL(ctx->sendqueue); + ReturnIf_CU_ASSERT_PTR_NOT_NULL(ctx->sendqueue); CU_ASSERT_PTR_EQUAL(ctx->sendqueue, node[1]); CU_ASSERT(ctx->sendqueue->t == timestamp[1]); - CU_ASSERT_PTR_NOT_NULL(ctx->sendqueue->next); + ReturnIf_CU_ASSERT_PTR_NOT_NULL(ctx->sendqueue->next); CU_ASSERT_PTR_EQUAL(ctx->sendqueue->next, node[2]); CU_ASSERT(ctx->sendqueue->next->t == timestamp[2] - timestamp[1]); @@ -236,16 +241,16 @@ t_sendqueue9(void) { coap_queue_t *tmp_node; tmp_node = coap_peek_next(ctx); - CU_ASSERT_PTR_NOT_NULL(tmp_node); + ReturnIf_CU_ASSERT_PTR_NOT_NULL(tmp_node); CU_ASSERT_PTR_EQUAL(tmp_node, node[1]); CU_ASSERT_PTR_EQUAL(tmp_node, ctx->sendqueue); tmp_node = coap_pop_next(ctx); - CU_ASSERT_PTR_NOT_NULL(tmp_node); + ReturnIf_CU_ASSERT_PTR_NOT_NULL(tmp_node); CU_ASSERT_PTR_EQUAL(tmp_node, node[1]); - CU_ASSERT_PTR_NOT_NULL(ctx->sendqueue); + ReturnIf_CU_ASSERT_PTR_NOT_NULL(ctx->sendqueue); CU_ASSERT_PTR_EQUAL(ctx->sendqueue, node[2]); CU_ASSERT(tmp_node->t == timestamp[1]); @@ -262,7 +267,7 @@ t_sendqueue10(void) { tmp_node = coap_pop_next(ctx); - CU_ASSERT_PTR_NOT_NULL(tmp_node); + ReturnIf_CU_ASSERT_PTR_NOT_NULL(tmp_node); CU_ASSERT_PTR_EQUAL(tmp_node, node[2]); CU_ASSERT_PTR_NULL(ctx->sendqueue); diff --git a/tests/test_session.c b/tests/test_session.c index 316c9087..0b7c4c40 100644 --- a/tests/test_session.c +++ b/tests/test_session.c @@ -28,6 +28,10 @@ */ #define MAX_BITS 8 +#define ReturnIf_CU_ASSERT_PTR_NOT_NULL(value) \ + CU_ASSERT_PTR_NOT_NULL(value); \ + if ((void*)value == NULL) return; + static coap_context_t *ctx; /* Holds the coap context for most tests */ static coap_session_t *session; /* Holds a reference-counted session object */ @@ -159,8 +163,8 @@ t_session6(void) { saddr.addr.sin6.sin6_port = htons(20000); session = coap_new_client_session(ctx, &saddr, &laddr, COAP_PROTO_UDP); - CU_ASSERT_PTR_NOT_NULL(session); - CU_ASSERT_PTR_NOT_NULL(ctx->sessions); + ReturnIf_CU_ASSERT_PTR_NOT_NULL(session); + ReturnIf_CU_ASSERT_PTR_NOT_NULL(ctx->sessions); CU_ASSERT(session->state == COAP_SESSION_STATE_ESTABLISHED); coap_session_release(session); } diff --git a/tests/test_tls.c b/tests/test_tls.c index 759eb1ba..cbf9042d 100644 --- a/tests/test_tls.c +++ b/tests/test_tls.c @@ -50,6 +50,10 @@ #include #endif /* COAP_WITH_LIBMBEDTLS */ +#define ReturnIf_CU_ASSERT_PTR_NOT_NULL(value) \ + CU_ASSERT_PTR_NOT_NULL(value); \ + if ((void*)value == NULL) return; + static void t_tls1(void) { int need_dtls = 0; @@ -101,7 +105,7 @@ t_tls2(void) { version.type = COAP_TLS_LIBRARY_NOTLS; #endif /* COAP_WITH_LIBOPENSSL || COAP_WITH_LIBTINYDTLS */ - CU_ASSERT_PTR_NOT_NULL_FATAL(v); + ReturnIf_CU_ASSERT_PTR_NOT_NULL(v); CU_ASSERT(version.version == v->version); CU_ASSERT(version.type == v->type); } diff --git a/tests/test_wellknown.c b/tests/test_wellknown.c index 90bbded6..25b94bef 100644 --- a/tests/test_wellknown.c +++ b/tests/test_wellknown.c @@ -24,6 +24,10 @@ #define TEST_PDU_SIZE 120 #define TEST_URI_LEN 4 +#define ReturnIf_CU_ASSERT_PTR_NOT_NULL(value) \ + CU_ASSERT_PTR_NOT_NULL(value); \ + if ((void*)value == NULL) return; + static coap_context_t *ctx; /* Holds the coap context for most tests */ static coap_pdu_t *pdu; /* Holds the parsed PDU for most tests */ static coap_session_t *session; /* Holds a reference-counted session object @@ -177,7 +181,7 @@ t_wellknown4(void) { /* Check for the resource added in t_wellknown2 */ query = coap_new_string(sizeof("if=one")-1); - CU_ASSERT(query != NULL); + ReturnIf_CU_ASSERT_PTR_NOT_NULL(query); memcpy(query->s, "if=one", sizeof("if=one")-1); result = coap_print_wellknown(ctx, buf, &buflen, 0, query); CU_ASSERT((result & COAP_PRINT_STATUS_ERROR) == 0);