channel: reduce duplicated free and returns

Simplified the code by trying to free data and return on a single
spot.
This commit is contained in:
Daniel Stenberg 2010-04-24 13:30:32 +02:00
parent 046ff03c3f
commit c5602fac58

View File

@ -501,25 +501,21 @@ channel_forward_listen(LIBSSH2_SESSION * session, const char *host,
LIBSSH2_LISTENER *listener; LIBSSH2_LISTENER *listener;
listener = LIBSSH2_ALLOC(session, sizeof(LIBSSH2_LISTENER)); listener = LIBSSH2_ALLOC(session, sizeof(LIBSSH2_LISTENER));
if (!listener) { if (!listener)
_libssh2_error(session, LIBSSH2_ERROR_ALLOC, _libssh2_error(session, LIBSSH2_ERROR_ALLOC,
"Unable to allocate memory for listener queue"); "Unable to allocate memory for listener queue");
LIBSSH2_FREE(session, data); else {
session->fwdLstn_state = libssh2_NB_state_idle;
return NULL;
}
memset(listener, 0, sizeof(LIBSSH2_LISTENER)); memset(listener, 0, sizeof(LIBSSH2_LISTENER));
listener->session = session;
listener->host = listener->host =
LIBSSH2_ALLOC(session, session->fwdLstn_host_len + 1); LIBSSH2_ALLOC(session, session->fwdLstn_host_len + 1);
if (!listener->host) { if (!listener->host) {
_libssh2_error(session, LIBSSH2_ERROR_ALLOC, _libssh2_error(session, LIBSSH2_ERROR_ALLOC,
"Unable to allocate memory for listener queue"); "Unable to allocate memory for listener queue");
LIBSSH2_FREE(session, listener); LIBSSH2_FREE(session, listener);
LIBSSH2_FREE(session, data); listener = NULL;
session->fwdLstn_state = libssh2_NB_state_idle;
return NULL;
} }
else {
listener->session = session;
memcpy(listener->host, host ? host : "0.0.0.0", memcpy(listener->host, host ? host : "0.0.0.0",
session->fwdLstn_host_len); session->fwdLstn_host_len);
listener->host[session->fwdLstn_host_len] = 0; listener->host[session->fwdLstn_host_len] = 0;
@ -528,9 +524,9 @@ channel_forward_listen(LIBSSH2_SESSION * session, const char *host,
_libssh2_debug(session, LIBSSH2_TRACE_CONN, _libssh2_debug(session, LIBSSH2_TRACE_CONN,
"Dynamic tcpip-forward port allocated: %d", "Dynamic tcpip-forward port allocated: %d",
listener->port); listener->port);
} else {
listener->port = port;
} }
else
listener->port = port;
listener->queue_size = 0; listener->queue_size = 0;
listener->queue_maxsize = queue_maxsize; listener->queue_maxsize = queue_maxsize;
@ -541,13 +537,14 @@ channel_forward_listen(LIBSSH2_SESSION * session, const char *host,
if (bound_port) { if (bound_port) {
*bound_port = listener->port; *bound_port = listener->port;
} }
}
}
LIBSSH2_FREE(session, data); LIBSSH2_FREE(session, data);
session->fwdLstn_state = libssh2_NB_state_idle; session->fwdLstn_state = libssh2_NB_state_idle;
return listener; return listener;
} }
else if (data[0] == SSH_MSG_REQUEST_FAILURE) {
if (data[0] == SSH_MSG_REQUEST_FAILURE) {
LIBSSH2_FREE(session, data); LIBSSH2_FREE(session, data);
_libssh2_error(session, LIBSSH2_ERROR_REQUEST_DENIED, _libssh2_error(session, LIBSSH2_ERROR_REQUEST_DENIED,
"Unable to complete request for forward-listen"); "Unable to complete request for forward-listen");