Skip to content

Commit

Permalink
coap_session.c: reference new session objects before release
Browse files Browse the repository at this point in the history
In case of failure, coap_session_accept() and coap_session_connect()
need to release the session object that has been passed as parameter.
Usually, session->ref would be 0 at the time these functions are
called, causing coap_session_release() to abort in debug mode.
To ensure that the object is referenced at least once,
coap_session_reference() is therefore called before
coap_session_release(). This fixes an error introduced in ommit 46c908a.

This change closes issue #155.
  • Loading branch information
obgm committed Mar 12, 2018
1 parent a674334 commit f3c1542
Showing 1 changed file with 15 additions and 0 deletions.
15 changes: 15 additions & 0 deletions src/coap_session.c
Original file line number Diff line number Diff line change
Expand Up @@ -531,6 +531,11 @@ coap_session_connect(coap_session_t *session) {
if (session->tls) {
session->state = COAP_SESSION_STATE_HANDSHAKE;
} else {
/* Need to free session object. As a new session may not yet
* have been referenced, we call coap_session_reference() first
* before trying to release the object.
*/
coap_session_reference(session);
coap_session_release(session);
return NULL;
}
Expand All @@ -547,6 +552,11 @@ coap_session_connect(coap_session_t *session) {
coap_session_send_csm(session);
}
} else {
/* Need to free session object. As a new session may not yet
* have been referenced, we call coap_session_reference()
* first before trying to release the object.
*/
coap_session_reference(session);
coap_session_release(session);
return NULL;
}
Expand All @@ -572,6 +582,11 @@ coap_session_accept(coap_session_t *session) {
coap_session_send_csm(session);
}
} else {
/* Need to free session object. As a new session may not yet
* have been referenced, we call coap_session_reference() first
* before trying to release the object.
*/
coap_session_reference(session);
coap_session_release(session);
session = NULL;
}
Expand Down

0 comments on commit f3c1542

Please sign in to comment.