From d0b1fa632980a63db3b295c4d15de49b2a88c39b Mon Sep 17 00:00:00 2001 From: 0x35c <> Date: Thu, 29 May 2025 14:52:09 +0200 Subject: [PATCH] fix: parsing leaks fixed --- include/parsing.h | 1 + src/main.c | 24 +++++++++++++++--------- src/parsing.c | 10 ++++++++++ 3 files changed, 26 insertions(+), 9 deletions(-) diff --git a/include/parsing.h b/include/parsing.h index 9aed1f2..3b2ed9f 100644 --- a/include/parsing.h +++ b/include/parsing.h @@ -25,3 +25,4 @@ struct option_lst *parse_options(int ac, char *const *av); char *get_option_arg(const struct option_lst *options, e_flag flag); int parse_ports(const char *arg, uint16_t *start, uint16_t *end); e_scantype parse_type(const char *arg); +void free_options(struct option_lst *options); diff --git a/src/main.c b/src/main.c index 9a79de8..1e645a4 100644 --- a/src/main.c +++ b/src/main.c @@ -7,7 +7,6 @@ #include #include "dns.h" -#include "interface.h" #include "parsing.h" #include "print.h" #include "scan.h" @@ -23,12 +22,12 @@ static int scan_host(char *host, const struct option_lst *options) "ft_nmap: failed to retrieve ip address from " "'%s'\n", host); - return -1; + return 1; } struct response responses[1024] = {0}; if (create_threads(options, ip_addr, responses) < 0) - return -1; + return 1; return 0; } @@ -46,28 +45,35 @@ int main(int ac, char **av) return 1; char *host = get_option_arg(options, FL_IP); - if (host) - return scan_host(host, options); + if (host) { + int rv = scan_host(host, options); + free_options(options); + return rv; + } const char *hosts_path = get_option_arg(options, FL_FILE); if (!hosts_path) { dprintf(2, "ft_nmap: address/hostname required\n"); - return 1; + goto error; } FILE *hosts_file = fopen(hosts_path, "r"); if (hosts_file == NULL) { dprintf(2, "ft_nmap: unable to open file '%s'\n", hosts_path); - return 1; + goto error; } char line[256]; while (fgets(line, sizeof(line), hosts_file)) { if (scan_host(line, options) < 0) { fclose(hosts_file); - return 1; + goto error; } } fclose(hosts_file); - return 0; + free_options(options); + return EXIT_SUCCESS; +error: + free_options(options); + return EXIT_FAILURE; } diff --git a/src/parsing.c b/src/parsing.c index 1ae0a01..2e0a753 100644 --- a/src/parsing.c +++ b/src/parsing.c @@ -8,6 +8,16 @@ #include "parsing.h" +void free_options(struct option_lst *options) +{ + struct option_lst *it = options; + while (it->next) { + struct option_lst *tmp = it; + it = it->next; + free(tmp); + } +} + char *get_option_arg(const struct option_lst *options, e_flag flag) { if (!options)