1
0
mirror of https://git.libssh.org/projects/libssh.git synced 2025-12-18 18:34:10 +03:00

Always check return value of ssh_list_new()

Another item identified during code review was cases where the return
value of ssh_list_new() was not properly checked and handled. This
updates all cases that were missing this to handle failure to allocate a
new list.

Signed-off-by: Dirkjan Bussink <d.bussink@gmail.com>
Reviewed-by: Jakub Jelen <jjelen@redhat.com>
Reviewed-by: Andreas Schneider <asn@cryptomilk.org>
This commit is contained in:
Dirkjan Bussink
2020-12-10 14:14:11 +00:00
committed by Andreas Schneider
parent daeee74edd
commit 0987e6065c
5 changed files with 42 additions and 21 deletions

View File

@@ -117,6 +117,13 @@ ssh_channel ssh_channel_new(ssh_session session)
if (session->channels == NULL) { if (session->channels == NULL) {
session->channels = ssh_list_new(); session->channels = ssh_list_new();
if (session->channels == NULL) {
ssh_set_error_oom(session);
SSH_BUFFER_FREE(channel->stdout_buffer);
SSH_BUFFER_FREE(channel->stderr_buffer);
SAFE_FREE(channel);
return NULL;
}
} }
ssh_list_prepend(session->channels, channel); ssh_list_prepend(session->channels, channel);

View File

@@ -372,6 +372,7 @@ struct ssh_list *ssh_known_hosts_get_algorithms(ssh_session session)
list = ssh_list_new(); list = ssh_list_new();
if (list == NULL) { if (list == NULL) {
ssh_set_error_oom(session);
SAFE_FREE(host_port); SAFE_FREE(host_port);
return NULL; return NULL;
} }

View File

@@ -513,24 +513,30 @@ static int ssh_message_termination(void *s){
* @warning This function blocks until a message has been received. Betterset up * @warning This function blocks until a message has been received. Betterset up
* a callback if this behavior is unwanted. * a callback if this behavior is unwanted.
*/ */
ssh_message ssh_message_get(ssh_session session) { ssh_message ssh_message_get(ssh_session session)
ssh_message msg = NULL; {
int rc; ssh_message msg = NULL;
int rc;
msg=ssh_message_pop_head(session); msg = ssh_message_pop_head(session);
if(msg) { if (msg != NULL) {
return msg; return msg;
} }
if(session->ssh_message_list == NULL) { if (session->ssh_message_list == NULL) {
session->ssh_message_list = ssh_list_new(); session->ssh_message_list = ssh_list_new();
} if (session->ssh_message_list == NULL) {
rc = ssh_handle_packets_termination(session, SSH_TIMEOUT_USER, ssh_set_error_oom(session);
ssh_message_termination, session); return NULL;
if(rc || session->session_state == SSH_SESSION_STATE_ERROR) }
return NULL; }
msg=ssh_list_pop_head(ssh_message, session->ssh_message_list); rc = ssh_handle_packets_termination(session, SSH_TIMEOUT_USER,
ssh_message_termination, session);
if (rc || session->session_state == SSH_SESSION_STATE_ERROR) {
return NULL;
}
msg = ssh_list_pop_head(ssh_message, session->ssh_message_list);
return msg; return msg;
} }
/** /**

View File

@@ -1422,12 +1422,14 @@ void ssh_packet_register_socket_callback(ssh_session session, ssh_socket s){
* @brief sets the callbacks for the packet layer * @brief sets the callbacks for the packet layer
*/ */
void ssh_packet_set_callbacks(ssh_session session, ssh_packet_callbacks callbacks){ void ssh_packet_set_callbacks(ssh_session session, ssh_packet_callbacks callbacks){
if(session->packet_callbacks == NULL){ if (session->packet_callbacks == NULL) {
session->packet_callbacks = ssh_list_new(); session->packet_callbacks = ssh_list_new();
} if (session->packet_callbacks == NULL) {
if (session->packet_callbacks != NULL) { ssh_set_error_oom(session);
return;
}
}
ssh_list_append(session->packet_callbacks, callbacks); ssh_list_append(session->packet_callbacks, callbacks);
}
} }
/** @internal /** @internal

View File

@@ -138,6 +138,9 @@ static void torture_callbacks_execute_list(void **state){
}; };
(void)state; (void)state;
assert_non_null(list);
ssh_callbacks_init(&c1); ssh_callbacks_init(&c1);
ssh_callbacks_init(&c2); ssh_callbacks_init(&c2);
ssh_callbacks_init(&c3); ssh_callbacks_init(&c3);
@@ -213,6 +216,8 @@ static void torture_callbacks_iterate(void **state){
(void)state; /* unused */ (void)state; /* unused */
assert_non_null(list);
ssh_callbacks_init(&c1); ssh_callbacks_init(&c1);
ssh_callbacks_init(&c2); ssh_callbacks_init(&c2);