Skip to content

Commit c8794b8

Browse files
author
Tero Heinonen
committed
Check for return status when opening socket
unittests fixed and added test to new lines.
1 parent 4b17eb5 commit c8794b8

File tree

3 files changed

+23
-3
lines changed

3 files changed

+23
-3
lines changed

source/coap_connection_handler.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,11 @@ static internal_socket_t *int_socket_create(uint16_t listen_port, bool use_ephem
193193
}else{
194194
this->listen_socket = socket_open(SOCKET_UDP, listen_port, secure_recv_sckt_msg);
195195
}
196+
// Socket create failed
197+
if(this->listen_socket < 0){
198+
ns_dyn_mem_free(this);
199+
return NULL;
200+
}
196201
// XXX API for this? May want to get clever to do recommended first query = 1 hop, retries = whole PAN
197202
socket_setsockopt(this->listen_socket, SOCKET_IPPROTO_IPV6, SOCKET_IPV6_MULTICAST_HOPS, &(const int16_t) {
198203
16

source/coap_service_api.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -302,10 +302,14 @@ int8_t coap_service_initialize(int8_t interface_id, uint16_t listen_port, uint8_
302302
ns_dyn_mem_free(this);
303303
return -1;
304304
}
305-
coap_connection_handler_open_connection(this->conn_handler, listen_port, ((this->service_options & COAP_SERVICE_OPTIONS_EPHEMERAL_PORT) == COAP_SERVICE_OPTIONS_EPHEMERAL_PORT),
305+
if (0 > coap_connection_handler_open_connection(this->conn_handler, listen_port, ((this->service_options & COAP_SERVICE_OPTIONS_EPHEMERAL_PORT) == COAP_SERVICE_OPTIONS_EPHEMERAL_PORT),
306306
((this->service_options & COAP_SERVICE_OPTIONS_SECURE) == COAP_SERVICE_OPTIONS_SECURE),
307307
((this->service_options & COAP_SERVICE_OPTIONS_VIRTUAL_SOCKET) != COAP_SERVICE_OPTIONS_VIRTUAL_SOCKET),
308-
((this->service_options & COAP_SERVICE_OPTIONS_SECURE_BYPASS) == COAP_SERVICE_OPTIONS_SECURE_BYPASS));
308+
((this->service_options & COAP_SERVICE_OPTIONS_SECURE_BYPASS) == COAP_SERVICE_OPTIONS_SECURE_BYPASS))){
309+
ns_dyn_mem_free(this->conn_handler);
310+
ns_dyn_mem_free(this);
311+
return -1;
312+
}
309313

310314
if (!coap_service_handle) {
311315
coap_service_handle = coap_message_handler_init(&own_alloc, &own_free, &coap_tx_function);

test/coap-service/unittest/coap_service_api/test_coap_service_api.c

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,16 +35,26 @@ bool test_coap_service_initialize()
3535
if( -1 != coap_service_initialize(1, 2, 0, NULL, NULL ))
3636
return false;
3737

38+
3839
nsdynmemlib_stub.returnCounter = 1;
3940
thread_conn_handler_stub.handler_obj = NULL;
4041
if( -1 != coap_service_initialize(1, 2, 0, NULL, NULL ))
4142
return false;
4243

4344
thread_conn_handler_stub.handler_obj = (coap_conn_handler_t*)malloc(sizeof(coap_conn_handler_t));
4445
memset(thread_conn_handler_stub.handler_obj, 0, sizeof(coap_conn_handler_t));
45-
nsdynmemlib_stub.returnCounter = 1;
4646
coap_message_handler_stub.coap_ptr = NULL;
4747

48+
nsdynmemlib_stub.returnCounter = 1;
49+
thread_conn_handler_stub.int_value = -1;
50+
if( -1 != coap_service_initialize(1, 2, 0, NULL, NULL ))
51+
return false;
52+
53+
thread_conn_handler_stub.handler_obj = (coap_conn_handler_t*)malloc(sizeof(coap_conn_handler_t));
54+
memset(thread_conn_handler_stub.handler_obj, 0, sizeof(coap_conn_handler_t));
55+
nsdynmemlib_stub.returnCounter = 1;
56+
thread_conn_handler_stub.int_value = 0;
57+
4858
if( 1 != coap_service_initialize(1, 2, 0, NULL, NULL ))
4959
return false;
5060

@@ -173,6 +183,7 @@ bool test_coap_service_unregister_uri()
173183
memset(thread_conn_handler_stub.handler_obj, 0, sizeof(coap_conn_handler_t));
174184
nsdynmemlib_stub.returnCounter = 1;
175185
coap_message_handler_stub.coap_ptr = NULL;
186+
thread_conn_handler_stub.int_value = 0;
176187

177188
if( 1 != coap_service_initialize(1, 2, 0, NULL, NULL ))
178189
return false;

0 commit comments

Comments
 (0)