--- /tmp/02092018/asterisk-15.5.0/channels/chan_sip.c 2018-07-12 18:36:50.000000000 +0200 +++ chan_sip.c 2018-09-03 14:20:36.000000000 +0200 @@ -2574,12 +2574,15 @@ } ao2_lock(tcptls_session); - - if (!(th = ao2_t_find(threadt, &tmp, OBJ_POINTER, "ao2_find, getting sip_threadinfo in tcp helper thread")) || + + // check stream isn't NULL + if ( (!tcptls_session->stream) || + !(th = ao2_t_find(threadt, &tmp, OBJ_POINTER, "ao2_find, getting sip_threadinfo in tcp helper thread")) || !(packet = ao2_alloc(sizeof(*packet), tcptls_packet_destructor)) || !(packet->data = ast_str_create(len))) { goto tcptls_write_setup_error; } + /* goto tcptls_write_error should _NOT_ be used beyond this point */ ast_str_set(&packet->data, 0, "%s", (char *) buf); @@ -3097,7 +3100,7 @@ if (read(me->alert_pipe[0], &alert, sizeof(alert)) == -1) { ast_log(LOG_ERROR, "read() failed: %s\n", strerror(errno)); - continue; + goto cleanup; // read failed, usually Bad file descriptor, so go to cleanup to be safe } switch (alert) { @@ -3116,9 +3119,11 @@ } ao2_t_ref(packet, -1, "tcptls packet sent, this is no longer needed"); } + else goto cleanup; //frame_q is empty, nothing to write, go to cleanup to be safe break; default: ast_log(LOG_ERROR, "Unknown tcptls thread alert '%u'\n", alert); + goto cleanup; // weird things, go to cleanup to be safe } } }