Fix memory leaks in Ncat test programs

This commit is contained in:
dmiller 2026-04-02 19:14:13 +00:00
parent d39b20c6a3
commit bfb569d8ec
3 changed files with 45 additions and 13 deletions

View file

@ -66,13 +66,14 @@ int main(int argc, char *argv[])
for (i = 1; i < argc; i++) {
if (!addrset_add_spec(set, argv[i], o.af, !o.nodns)) {
fprintf(stderr, "Error adding spec \"%s\".\n", argv[i]);
addrset_free(set);
exit(1);
}
}
while (fgets(line, sizeof(line), stdin) != NULL) {
char *s, *hostname;
struct addrinfo *addrs;
struct addrinfo *addrs = NULL;
s = line;
while ((hostname = strtok(s, " \t\n")) != NULL) {
@ -80,9 +81,12 @@ int main(int argc, char *argv[])
s = NULL;
addrs = NULL;
rc = resolve_name(hostname, &addrs);
if (rc != 0) {
fprintf(stderr, "Error resolving \"%s\": %s.\n", hostname, gai_strerror(rc));
if (addrs)
freeaddrinfo(addrs);
continue;
}
if (addrs == NULL) {

View file

@ -9,32 +9,38 @@ char **cmdline_split(const char *cmdexec);
int test_cmdline(const char *line, const char **target_args)
{
char **cmd_args;
char **cmd_args, **cur_arg;
int args_match = 1;
test_count++;
cmd_args = cmdline_split(line);
cur_arg = cmd_args;
/*
* Make sure that all of the target arguments are have been extracted
* by cmdline_split.
*/
while (*cmd_args && *target_args) {
if (strcmp(*cmd_args, *target_args)) {
while (*cur_arg && *target_args) {
if (args_match && strcmp(*cur_arg, *target_args)) {
args_match = 0;
break;
}
cmd_args++;
free(*cur_arg);
cur_arg++;
target_args++;
}
if ((*cmd_args != NULL) || (*target_args != NULL)) {
if ((*cur_arg != NULL) || (*target_args != NULL)) {
/*
* One of the argument list had more arguments than the other.
* Therefore, they do not match
*/
args_match = 0;
while (*cur_arg != NULL) {
free(*cur_arg);
cur_arg++;
}
}
free(cmd_args);
if (args_match) {
success_count++;
@ -55,11 +61,13 @@ int test_cmdline_fail(const char *line)
cmd_args = cmdline_split(line);
if (*cmd_args == NULL) {
free(cmd_args);
success_count++;
printf("PASS '%s'\n", line);
return 1;
} else {
printf("PASS '%s'\n", line);
free(cmd_args);
printf("FAIL '%s'\n", line);
return 0;
}
}

View file

@ -160,14 +160,13 @@ end:
X509_free(cert);
EVP_PKEY_free(key);
(void) BIO_destroy_bio_pair(server_bio);
SSL_CTX_free(server_ctx);
SSL_CTX_free(client_ctx);
SSL_free(server_ssl);
SSL_free(client_ssl);
SSL_free(server_ssl);
SSL_CTX_free(client_ctx);
SSL_CTX_free(server_ctx);
ERR_clear_error();
return passed;
}
@ -307,6 +306,14 @@ static int gen_cert(X509 **cert, EVP_PKEY **key,
if (*key == NULL)
goto err;
do {
if (bne != NULL) {
BN_free(bne);
bne = NULL;
}
if (rsa != NULL) {
RSA_free(rsa);
rsa = NULL;
}
/* Generate RSA key. */
bne = BN_new();
ret = BN_set_word(bne, RSA_F4);
@ -324,6 +331,7 @@ static int gen_cert(X509 **cert, EVP_PKEY **key,
goto err;
if (EVP_PKEY_assign_RSA(*key, rsa) == 0) {
RSA_free(rsa);
rsa = NULL;
goto err;
}
#else
@ -396,6 +404,12 @@ static int gen_cert(X509 **cert, EVP_PKEY **key,
return 1;
err:
#if OPENSSL_VERSION_NUMBER < 0x30000000L
if (bne != NULL)
BN_free(bne);
if (rsa != NULL)
RSA_free(rsa);
#endif
if (*cert != NULL)
X509_free(*cert);
if (*key != NULL)
@ -622,5 +636,11 @@ int main(void)
printf("%d / %d tests passed.\n", tests_passed, tests_run);
EVP_cleanup();
CRYPTO_cleanup_all_ex_data();
ERR_free_strings();
#if OPENSSL_VERSION_NUMBER >= 0x30000000L
OPENSSL_cleanup();
#endif
return tests_passed == tests_run ? 0 : 1;
}