diff --git a/source/coap_connection_handler.c b/source/coap_connection_handler.c index d275b05d55..691bc25e63 100644 --- a/source/coap_connection_handler.c +++ b/source/coap_connection_handler.c @@ -193,6 +193,11 @@ static internal_socket_t *int_socket_create(uint16_t listen_port, bool use_ephem }else{ this->listen_socket = socket_open(SOCKET_UDP, listen_port, secure_recv_sckt_msg); } + // Socket create failed + if(this->listen_socket < 0){ + ns_dyn_mem_free(this); + return NULL; + } // XXX API for this? May want to get clever to do recommended first query = 1 hop, retries = whole PAN socket_setsockopt(this->listen_socket, SOCKET_IPPROTO_IPV6, SOCKET_IPV6_MULTICAST_HOPS, &(const int16_t) { 16 diff --git a/source/coap_service_api.c b/source/coap_service_api.c index 27ac19998c..5eda0af1f9 100644 --- a/source/coap_service_api.c +++ b/source/coap_service_api.c @@ -302,10 +302,14 @@ int8_t coap_service_initialize(int8_t interface_id, uint16_t listen_port, uint8_ ns_dyn_mem_free(this); return -1; } - coap_connection_handler_open_connection(this->conn_handler, listen_port, ((this->service_options & COAP_SERVICE_OPTIONS_EPHEMERAL_PORT) == COAP_SERVICE_OPTIONS_EPHEMERAL_PORT), + 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), ((this->service_options & COAP_SERVICE_OPTIONS_SECURE) == COAP_SERVICE_OPTIONS_SECURE), ((this->service_options & COAP_SERVICE_OPTIONS_VIRTUAL_SOCKET) != COAP_SERVICE_OPTIONS_VIRTUAL_SOCKET), - ((this->service_options & COAP_SERVICE_OPTIONS_SECURE_BYPASS) == COAP_SERVICE_OPTIONS_SECURE_BYPASS)); + ((this->service_options & COAP_SERVICE_OPTIONS_SECURE_BYPASS) == COAP_SERVICE_OPTIONS_SECURE_BYPASS))){ + ns_dyn_mem_free(this->conn_handler); + ns_dyn_mem_free(this); + return -1; + } if (!coap_service_handle) { coap_service_handle = coap_message_handler_init(&own_alloc, &own_free, &coap_tx_function); diff --git a/test/coap-service/unittest/coap_service_api/test_coap_service_api.c b/test/coap-service/unittest/coap_service_api/test_coap_service_api.c index 331e2501b6..3d41dc42f8 100644 --- a/test/coap-service/unittest/coap_service_api/test_coap_service_api.c +++ b/test/coap-service/unittest/coap_service_api/test_coap_service_api.c @@ -35,6 +35,7 @@ bool test_coap_service_initialize() if( -1 != coap_service_initialize(1, 2, 0, NULL, NULL )) return false; + nsdynmemlib_stub.returnCounter = 1; thread_conn_handler_stub.handler_obj = NULL; if( -1 != coap_service_initialize(1, 2, 0, NULL, NULL )) @@ -42,9 +43,18 @@ bool test_coap_service_initialize() thread_conn_handler_stub.handler_obj = (coap_conn_handler_t*)malloc(sizeof(coap_conn_handler_t)); memset(thread_conn_handler_stub.handler_obj, 0, sizeof(coap_conn_handler_t)); - nsdynmemlib_stub.returnCounter = 1; coap_message_handler_stub.coap_ptr = NULL; + nsdynmemlib_stub.returnCounter = 1; + thread_conn_handler_stub.int_value = -1; + if( -1 != coap_service_initialize(1, 2, 0, NULL, NULL )) + return false; + + thread_conn_handler_stub.handler_obj = (coap_conn_handler_t*)malloc(sizeof(coap_conn_handler_t)); + memset(thread_conn_handler_stub.handler_obj, 0, sizeof(coap_conn_handler_t)); + nsdynmemlib_stub.returnCounter = 1; + thread_conn_handler_stub.int_value = 0; + if( 1 != coap_service_initialize(1, 2, 0, NULL, NULL )) return false; @@ -173,6 +183,7 @@ bool test_coap_service_unregister_uri() memset(thread_conn_handler_stub.handler_obj, 0, sizeof(coap_conn_handler_t)); nsdynmemlib_stub.returnCounter = 1; coap_message_handler_stub.coap_ptr = NULL; + thread_conn_handler_stub.int_value = 0; if( 1 != coap_service_initialize(1, 2, 0, NULL, NULL )) return false;