ssh_known_hosts_get_algorithms: Simplify cleanup ...

...  and prevent memory leak of host_port on memory allocation failure.

Thanks Xiaoke Wang for the report!

Signed-off-by: Jakub Jelen <jjelen@redhat.com>
Reviewed-by: Pavol Žáčik <pzacik@redhat.com>
Reviewed-by: Andreas Schneider <asn@cryptomilk.org>
This commit is contained in:
Jakub Jelen
2025-11-24 18:05:36 +01:00
parent ee180c660e
commit 9d6df9d0fa

View File

@@ -376,25 +376,23 @@ struct ssh_list *ssh_known_hosts_get_algorithms(ssh_session session)
} }
} }
host_port = ssh_session_get_host_port(session);
if (host_port == NULL) {
return NULL;
}
list = ssh_list_new(); list = ssh_list_new();
if (list == NULL) { if (list == NULL) {
ssh_set_error_oom(session); ssh_set_error_oom(session);
SAFE_FREE(host_port);
return NULL; return NULL;
} }
host_port = ssh_session_get_host_port(session);
if (host_port == NULL) {
goto error;
}
rc = ssh_known_hosts_read_entries(host_port, rc = ssh_known_hosts_read_entries(host_port,
session->opts.knownhosts, session->opts.knownhosts,
&entry_list); &entry_list);
if (rc != 0) { if (rc != 0) {
ssh_list_free(entry_list); SAFE_FREE(host_port);
ssh_list_free(list); goto error;
return NULL;
} }
rc = ssh_known_hosts_read_entries(host_port, rc = ssh_known_hosts_read_entries(host_port,
@@ -402,21 +400,16 @@ struct ssh_list *ssh_known_hosts_get_algorithms(ssh_session session)
&entry_list); &entry_list);
SAFE_FREE(host_port); SAFE_FREE(host_port);
if (rc != 0) { if (rc != 0) {
ssh_list_free(entry_list); goto error;
ssh_list_free(list);
return NULL;
} }
if (entry_list == NULL) { if (entry_list == NULL) {
ssh_list_free(list); goto error;
return NULL;
} }
count = ssh_list_count(entry_list); count = ssh_list_count(entry_list);
if (count == 0) { if (count == 0) {
ssh_list_free(list); goto error;
ssh_list_free(entry_list);
return NULL;
} }
for (it = ssh_list_get_iterator(entry_list); for (it = ssh_list_get_iterator(entry_list);
@@ -460,6 +453,7 @@ struct ssh_list *ssh_known_hosts_get_algorithms(ssh_session session)
return list; return list;
error: error:
ssh_list_free(entry_list);
ssh_list_free(list); ssh_list_free(list);
return NULL; return NULL;
} }