From dedbaf05290929de985590dbfd7d0751ddab0a0c Mon Sep 17 00:00:00 2001 From: kivkiv12345 Date: Sat, 27 Apr 2024 13:02:21 +0200 Subject: [PATCH] replace explicit calls to optparse_del with __attribute__((cleanup)) This commit makes us more dependant on GCC, but introduces a more reliable way to free optparse_t pointers. This commit also solves the following memory leaks: 2 missing calls to optparse_del() in loki.c (loki_start_cmd()) 6 missing calls to optparse_del() in slash_apm.c (apm_load_cmd(), apm_info_cmd()) 1 call to optparse_del() after return in prometheus.c (prometheus_start_cmd()) 1 missing call to optparse_del() in builtins.c (slash_builtin_watch()) --- lib/param | 2 +- lib/slash | 2 +- src/csp_init_cmd.c | 43 +++++++-------------------------- src/csp_scan.c | 4 +-- src/hk_param_sniffer.c | 4 +-- src/known_hosts.c | 5 +--- src/loki.c | 6 +---- src/prometheus.c | 4 +-- src/resbuf_dump.c | 7 +----- src/slash_apm.c | 7 ++---- src/slash_csp.c | 55 ++++++++---------------------------------- src/spaceboot_slash.c | 27 +++------------------ src/stdbuf_client.c | 6 +---- src/stdbuf_mon.c | 5 +--- src/victoria_metrics.c | 6 +---- src/vts.c | 8 +----- 16 files changed, 36 insertions(+), 155 deletions(-) diff --git a/lib/param b/lib/param index b638d7ec..e7de77f4 160000 --- a/lib/param +++ b/lib/param @@ -1 +1 @@ -Subproject commit b638d7ec547a5613a0a8fd0ead19abadf6583720 +Subproject commit e7de77f4acc6ea7c25df9ca8d6fff8dbb56a9f77 diff --git a/lib/slash b/lib/slash index 570c0be1..f6ace8bf 160000 --- a/lib/slash +++ b/lib/slash @@ -1 +1 @@ -Subproject commit 570c0be1863651ccd43f70c995b4168a1c31fde1 +Subproject commit f6ace8bf0c62aa68ddf3b97af55c71db1a135a7f diff --git a/src/csp_init_cmd.c b/src/csp_init_cmd.c index 170166ed..07f4fe0f 100644 --- a/src/csp_init_cmd.c +++ b/src/csp_init_cmd.c @@ -43,6 +43,8 @@ static int csp_init_cmd(struct slash *slash) { int version = 2; int dedup = 3; + // optparse_t * parser __attribute__((cleanup(optparse_del))) = optparse_new("csp init", ""); + /* No __attribute__((cleanup(optparse_del))), as the arg string pointers might be used */ optparse_t * parser = optparse_new("csp init", ""); optparse_add_help(parser); optparse_add_string(parser, 'n', "host", "HOSTNAME", &hostname, "Hostname (default = linux hostname)"); @@ -126,7 +128,7 @@ static int csp_ifadd_zmq_cmd(struct slash *slash) { unsigned int subport = 0; unsigned int pubport = 0; - optparse_t * parser = optparse_new("csp add zmq", " "); + optparse_t * parser __attribute__((cleanup(optparse_del))) = optparse_new("csp add zmq", " "); optparse_add_help(parser); optparse_add_set(parser, 'p', "promisc", 1, &promisc, "Promiscuous Mode"); optparse_add_int(parser, 'm', "mask", "NUM", 0, &mask, "Netmask (defaults to 8)"); @@ -138,13 +140,11 @@ static int csp_ifadd_zmq_cmd(struct slash *slash) { int argi = optparse_parse(parser, slash->argc - 1, (const char **) slash->argv + 1); if (argi < 0) { - optparse_del(parser); return SLASH_EINVAL; } if (++argi >= slash->argc) { printf("missing parameter addr\n"); - optparse_del(parser); return SLASH_EINVAL; } char * endptr; @@ -152,7 +152,6 @@ static int csp_ifadd_zmq_cmd(struct slash *slash) { if (++argi >= slash->argc) { printf("missing parameter server\n"); - optparse_del(parser); return SLASH_EINVAL; } char * server = slash->argv[argi]; @@ -179,7 +178,6 @@ static int csp_ifadd_zmq_cmd(struct slash *slash) { FILE *file = fopen(key_file_local, "r"); if(file == NULL){ printf("Could not open config %s\n", key_file_local); - optparse_del(parser); return SLASH_EINVAL; } @@ -187,7 +185,6 @@ static int csp_ifadd_zmq_cmd(struct slash *slash) { if (sec_key == NULL) { printf("Failed to allocate memory for secret key.\n"); fclose(file); - optparse_del(parser); return SLASH_EINVAL; } @@ -195,7 +192,6 @@ static int csp_ifadd_zmq_cmd(struct slash *slash) { printf("Failed to read secret key from file.\n"); free(sec_key); fclose(file); - optparse_del(parser); return SLASH_EINVAL; } fclose(file); @@ -210,7 +206,6 @@ static int csp_ifadd_zmq_cmd(struct slash *slash) { if (sec_key != NULL) { free(sec_key); } - optparse_del(parser); return SLASH_SUCCESS; } @@ -229,7 +224,7 @@ static int csp_ifadd_kiss_cmd(struct slash *slash) { int baud = 1000000; char * device = "/dev/ttyUSB0"; - optparse_t * parser = optparse_new("csp add kiss", ""); + optparse_t * parser __attribute__((cleanup(optparse_del))) = optparse_new("csp add kiss", ""); optparse_add_help(parser); optparse_add_set(parser, 'p', "promisc", 1, &promisc, "Promiscuous Mode"); optparse_add_int(parser, 'm', "mask", "NUM", 0, &mask, "Netmask (defaults to 8)"); @@ -245,7 +240,6 @@ static int csp_ifadd_kiss_cmd(struct slash *slash) { if (++argi >= slash->argc) { printf("missing parameter addr\n"); - optparse_del(parser); return SLASH_EINVAL; } char * endptr; @@ -265,7 +259,6 @@ static int csp_ifadd_kiss_cmd(struct slash *slash) { int error = csp_usart_open_and_add_kiss_interface(&conf, name, &iface); if (error != CSP_ERR_NONE) { printf("Failed to add kiss interface\n"); - optparse_del(parser); return SLASH_EINVAL; } @@ -293,7 +286,7 @@ static int csp_ifadd_can_cmd(struct slash *slash) { int baud = 1000000; char * device = "can0"; - optparse_t * parser = optparse_new("csp add can", ""); + optparse_t * parser __attribute__((cleanup(optparse_del))) = optparse_new("csp add can", ""); optparse_add_help(parser); optparse_add_set(parser, 'p', "promisc", 1, &promisc, "Promiscous Mode"); optparse_add_int(parser, 'm', "mask", "NUM", 0, &mask, "Netmask (defaults to 8)"); @@ -304,13 +297,11 @@ static int csp_ifadd_can_cmd(struct slash *slash) { int argi = optparse_parse(parser, slash->argc - 1, (const char **) slash->argv + 1); if (argi < 0) { - optparse_del(parser); return SLASH_EINVAL; } if (++argi >= slash->argc) { printf("missing parameter addr\n"); - optparse_del(parser); return SLASH_EINVAL; } char * endptr; @@ -321,7 +312,6 @@ static int csp_ifadd_can_cmd(struct slash *slash) { int error = csp_can_socketcan_open_and_add_interface(device, name, addr, baud, promisc, &iface); if (error != CSP_ERR_NONE) { csp_print("failed to add CAN interface [%s], error: %d", device, error); - optparse_del(parser); return SLASH_EINVAL; } @@ -377,7 +367,7 @@ static int csp_ifadd_eth_cmd(struct slash *slash) { int dfl = 0; int mtu = 1200; - optparse_t * parser = optparse_new("csp add eth", ""); + optparse_t * parser __attribute__((cleanup(optparse_del))) = optparse_new("csp add eth", ""); optparse_add_help(parser); optparse_add_string(parser, 'e', "device", "STR", (char **)&device, "Ethernet device name or name prefix (defaults to enp)"); optparse_add_set(parser, 'p', "promisc", 1, &promisc, "Promiscous Mode"); @@ -393,7 +383,6 @@ static int csp_ifadd_eth_cmd(struct slash *slash) { if (++argi >= slash->argc) { printf("missing parameter addr\n"); - optparse_del(parser); return SLASH_EINVAL; } char * endptr; @@ -401,7 +390,6 @@ static int csp_ifadd_eth_cmd(struct slash *slash) { eth_select_interface(&device); if (strlen(device) == 0) { - optparse_del(parser); return SLASH_EINVAL; } @@ -414,7 +402,6 @@ static int csp_ifadd_eth_cmd(struct slash *slash) { iface->addr = addr; iface->netmask = mask; - optparse_del(parser); return SLASH_SUCCESS; } @@ -433,7 +420,7 @@ static int csp_ifadd_udp_cmd(struct slash *slash) { int listen_port = 9220; int remote_port = 9220; - optparse_t * parser = optparse_new("csp add udp", " "); + optparse_t * parser __attribute__((cleanup(optparse_del))) = optparse_new("csp add udp", " "); optparse_add_help(parser); optparse_add_set(parser, 'p', "promisc", 1, &promisc, "Promiscous Mode"); optparse_add_int(parser, 'm', "mask", "NUM", 0, &mask, "Netmask (defaults to 8)"); @@ -444,13 +431,11 @@ static int csp_ifadd_udp_cmd(struct slash *slash) { int argi = optparse_parse(parser, slash->argc - 1, (const char **) slash->argv + 1); if (argi < 0) { - optparse_del(parser); return SLASH_EINVAL; } if (++argi >= slash->argc) { printf("missing parameter addr\n"); - optparse_del(parser); return SLASH_EINVAL; } char * endptr; @@ -458,7 +443,6 @@ static int csp_ifadd_udp_cmd(struct slash *slash) { if (++argi >= slash->argc) { printf("missing parameter server\n"); - optparse_del(parser); return SLASH_EINVAL; } char * server = slash->argv[argi]; @@ -476,7 +460,6 @@ static int csp_ifadd_udp_cmd(struct slash *slash) { iface->addr = addr; iface->netmask = mask; - optparse_del(parser); return SLASH_SUCCESS; } @@ -493,7 +476,7 @@ static int csp_ifadd_tun_cmd(struct slash *slash) { int mask = 8; int dfl = 0; - optparse_t * parser = optparse_new("csp add tun", " "); + optparse_t * parser __attribute__((cleanup(optparse_del))) = optparse_new("csp add tun", " "); optparse_add_help(parser); optparse_add_set(parser, 'p', "promisc", 1, &promisc, "Promiscous Mode"); optparse_add_int(parser, 'm', "mask", "NUM", 0, &mask, "Netmask (defaults to 8)"); @@ -507,7 +490,6 @@ static int csp_ifadd_tun_cmd(struct slash *slash) { if (++argi >= slash->argc) { printf("missing parameter addr\n"); - optparse_del(parser); return SLASH_EINVAL; } char * endptr; @@ -515,14 +497,12 @@ static int csp_ifadd_tun_cmd(struct slash *slash) { if (++argi >= slash->argc) { printf("missing parameter tun src\n"); - optparse_del(parser); return SLASH_EINVAL; } unsigned int tun_src = strtoul(slash->argv[argi], &endptr, 10); if (++argi >= slash->argc) { printf("missing parameter tun dst\n"); - optparse_del(parser); return SLASH_EINVAL; } unsigned int tun_dst = strtoul(slash->argv[argi], &endptr, 10); @@ -539,7 +519,6 @@ static int csp_ifadd_tun_cmd(struct slash *slash) { iface->addr = addr; iface->netmask = mask; - optparse_del(parser); return SLASH_SUCCESS; } @@ -550,7 +529,7 @@ static int csp_routeadd_cmd(struct slash *slash) { char route[50]; - optparse_t * parser = optparse_new("csp add route", "/ "); + optparse_t * parser __attribute__((cleanup(optparse_del))) = optparse_new("csp add route", "/ "); optparse_add_help(parser); int argi = optparse_parse(parser, slash->argc - 1, (const char **) slash->argv + 1); @@ -562,7 +541,6 @@ static int csp_routeadd_cmd(struct slash *slash) { /* Build string from the two slash input arguments */ if (++argi >= slash->argc) { printf("missing parameter addr/mask\n"); - optparse_del(parser); return SLASH_EINVAL; } strcpy(route, slash->argv[argi]); @@ -571,7 +549,6 @@ static int csp_routeadd_cmd(struct slash *slash) { if (++argi >= slash->argc) { printf("missing parameter ifname\n"); - optparse_del(parser); return SLASH_EINVAL; } @@ -579,11 +556,9 @@ static int csp_routeadd_cmd(struct slash *slash) { if (csp_rtable_load(route) == 1) { /* function returns number of routes added */ printf("Added route %s\n", route); - optparse_del(parser); return SLASH_SUCCESS; } else { printf("Error during route add\n"); - optparse_del(parser); return SLASH_EINVAL; } } diff --git a/src/csp_scan.c b/src/csp_scan.c index 8d62f25e..ef454c57 100644 --- a/src/csp_scan.c +++ b/src/csp_scan.c @@ -11,14 +11,13 @@ static int csp_scan(struct slash *slash) unsigned int end = 0x3FFE; char * search_str = 0; - optparse_t * parser = optparse_new("csp scan", NULL); + optparse_t * parser __attribute__((cleanup(optparse_del))) = optparse_new("csp scan", NULL); optparse_add_help(parser); optparse_add_unsigned(parser, 'b', "begin", "NUM", 0, &begin, "begin at node"); optparse_add_unsigned(parser, 'e', "end", "NUM", 0, &end, "end at node"); optparse_add_string(parser, 's', "search", "STR", &search_str, "host name search sub-string"); int argi = optparse_parse(parser, slash->argc - 1, (const char **) slash->argv + 1); if (argi < 0) { - optparse_del(parser); return SLASH_EINVAL; } @@ -47,7 +46,6 @@ static int csp_scan(struct slash *slash) break; } printf("\r"); - optparse_del(parser); return SLASH_SUCCESS; } diff --git a/src/hk_param_sniffer.c b/src/hk_param_sniffer.c index b4475ee3..3e387837 100644 --- a/src/hk_param_sniffer.c +++ b/src/hk_param_sniffer.c @@ -51,12 +51,11 @@ void hk_set_epoch(time_t epoch, uint16_t node) { static int hk_timeoffset(struct slash *slash) { unsigned int node = slash_dfl_node; - optparse_t * parser = optparse_new("hk timeoffset [epoch]", "Satellite epoch time in seconds relative to Jan 1th 1970"); + optparse_t * parser __attribute__((cleanup(optparse_del))) = optparse_new("hk timeoffset [epoch]", "Satellite epoch time in seconds relative to Jan 1th 1970"); optparse_add_unsigned(parser, 'n', "node", "NUM", 0, &node, "node (default = )"); optparse_add_help(parser); int argi = optparse_parse(parser, slash->argc - 1, (const char **) slash->argv + 1); if (argi < 0) { - optparse_del(parser); return SLASH_EINVAL; } @@ -77,7 +76,6 @@ static int hk_timeoffset(struct slash *slash) { } } - optparse_del(parser); return SLASH_SUCCESS; } diff --git a/src/known_hosts.c b/src/known_hosts.c index a55db77a..168c13d5 100644 --- a/src/known_hosts.c +++ b/src/known_hosts.c @@ -137,27 +137,24 @@ static int cmd_hosts_add(struct slash *slash) int node = slash_dfl_node; - optparse_t * parser = optparse_new("hosts add", ""); + optparse_t * parser __attribute__((cleanup(optparse_del))) = optparse_new("hosts add", ""); optparse_add_help(parser); optparse_add_int(parser, 'n', "node", "NUM", 0, &node, "node (default = )"); int argi = optparse_parse(parser, slash->argc - 1, (const char **) slash->argv + 1); if (argi < 0) { - optparse_del(parser); return SLASH_EINVAL; } /* Check if name is present */ if (++argi >= slash->argc) { printf("missing parameter name\n"); - optparse_del(parser); return SLASH_EINVAL; } char * name = slash->argv[argi]; known_hosts_add(node, name); - optparse_del(parser); return SLASH_SUCCESS; } diff --git a/src/loki.c b/src/loki.c index 421846bc..327ae85e 100644 --- a/src/loki.c +++ b/src/loki.c @@ -247,7 +247,7 @@ static int loki_start_cmd(struct slash * slash) { char * tmp_username = NULL; char * tmp_password = NULL; - optparse_t * parser = optparse_new("loki start", ""); + optparse_t * parser __attribute__((cleanup(optparse_del))) = optparse_new("loki start", ""); optparse_add_help(parser); optparse_add_string(parser, 'u', "user", "STRING", &tmp_username, "Username for vmauth"); optparse_add_string(parser, 'p', "pass", "STRING", &tmp_password, "Password for vmauth"); @@ -259,20 +259,17 @@ static int loki_start_cmd(struct slash * slash) { int argi = optparse_parse(parser, slash->argc - 1, (const char **)slash->argv + 1); if (argi < 0) { - optparse_del(parser); return SLASH_EINVAL; } if (++argi >= slash->argc) { printf("Missing server ip/domain\n"); - optparse_del(parser); return SLASH_EINVAL; } if (tmp_username) { if (!tmp_password) { printf("Provide password with -p\n"); - optparse_del(parser); return SLASH_EINVAL; } args.username = strdup(tmp_username); @@ -346,7 +343,6 @@ static int loki_start_cmd(struct slash * slash) { loki_running = 1; printf("Loki logging started\n"); - optparse_del(parser); return SLASH_SUCCESS; } diff --git a/src/prometheus.c b/src/prometheus.c index 8dcefb87..cbef777b 100644 --- a/src/prometheus.c +++ b/src/prometheus.c @@ -119,7 +119,7 @@ static int prometheus_start_cmd(struct slash *slash) { int logfile = 0; - optparse_t * parser = optparse_new("prometheus start", ""); + optparse_t * parser __attribute__((cleanup(optparse_del))) = optparse_new("prometheus start", ""); optparse_add_help(parser); optparse_add_set(parser, 'l', "logfile", 1, &logfile, "Enable logging to param_sniffer.log"); @@ -127,13 +127,11 @@ static int prometheus_start_cmd(struct slash *slash) { if (argi < 0) { return SLASH_EINVAL; - optparse_del(parser); } prometheus_init(); param_sniffer_init(logfile); prometheus_started = 1; - optparse_del(parser); return SLASH_SUCCESS; } diff --git a/src/resbuf_dump.c b/src/resbuf_dump.c index b3f005a6..46fb17f6 100644 --- a/src/resbuf_dump.c +++ b/src/resbuf_dump.c @@ -69,14 +69,13 @@ static int resbuf_dump_slash(struct slash *slash) { unsigned int node = slash_dfl_node; char * filename = NULL; - optparse_t * parser = optparse_new("resbuf", ""); + optparse_t * parser __attribute__((cleanup(optparse_del))) = optparse_new("resbuf", ""); optparse_add_help(parser); optparse_add_unsigned(parser, 'n', "node", "NUM", 0, &node, "node (default = )"); optparse_add_string(parser, 'f', "filename", "PATH", &filename, "write to file, or 'timestamp' for timestamped file in cwd"); int argi = optparse_parse(parser, slash->argc - 1, (const char **) slash->argv + 1); if (argi < 0) { - optparse_del(parser); return SLASH_EINVAL; } @@ -103,7 +102,6 @@ static int resbuf_dump_slash(struct slash *slash) { vmem_list_t vmem = resbuf_get_base(node, 1000); if (vmem.size == 0 || vmem.vaddr == 0) { printf("Could not find result buffer on node %u\n", node); - optparse_del(parser); return SLASH_EINVAL; } @@ -121,12 +119,10 @@ static int resbuf_dump_slash(struct slash *slash) { printf("Got resbuf size %u in %u out %u\n", vmem.size, in, out); if (in > vmem.size){ - optparse_del(parser); return SLASH_EINVAL; } if (out > vmem.size){ - optparse_del(parser); return SLASH_EINVAL; } @@ -141,7 +137,6 @@ static int resbuf_dump_slash(struct slash *slash) { } } - optparse_del(parser); return SLASH_SUCCESS; } diff --git a/src/slash_apm.c b/src/slash_apm.c index d810fff8..f74ac0eb 100644 --- a/src/slash_apm.c +++ b/src/slash_apm.c @@ -252,14 +252,13 @@ static int apm_load_cmd(struct slash *slash) { lib_search.path = NULL; lib_search.search_str = NULL; - optparse_t * parser = optparse_new("apm load", "-f -p "); + optparse_t * parser __attribute__((cleanup(optparse_del))) = optparse_new("apm load", "-f -p "); optparse_add_help(parser); optparse_add_string(parser, 'p', "path", "PATHNAME", &lib_search.path, "Search paths separated by ';'"); optparse_add_string(parser, 'f', "file", "FILENAME", &lib_search.search_str, "Search string on APM file name"); int argi = optparse_parse(parser, slash->argc - 1, (const char **) slash->argv + 1); if (argi < 0) { - optparse_del(parser); return SLASH_EINVAL; } @@ -269,7 +268,6 @@ static int apm_load_cmd(struct slash *slash) { if (p == NULL) { p = getpwuid(getuid())->pw_dir; if(p == NULL){ - optparse_del(parser); printf("No home folder found\n"); return SLASH_EINVAL; } @@ -338,13 +336,12 @@ static int apm_info_cmd(struct slash *slash) { char * search_str = 0; - optparse_t * parser = optparse_new("apm info", ""); + optparse_t * parser __attribute__((cleanup(optparse_del))) = optparse_new("apm info", ""); optparse_add_help(parser); optparse_add_string(parser, 's', "search", "SEARCHSTR", &search_str, "Search string on APM file name"); int argi = optparse_parse(parser, slash->argc - 1, (const char **) slash->argv + 1); if (argi < 0) { - optparse_del(parser); return SLASH_EINVAL; } diff --git a/src/slash_csp.c b/src/slash_csp.c index baea1275..1aa94302 100644 --- a/src/slash_csp.c +++ b/src/slash_csp.c @@ -56,7 +56,7 @@ static int slash_csp_ping(struct slash *slash) unsigned int timeout = slash_dfl_timeout; unsigned int size = 0; - optparse_t * parser = optparse_new("ping", "[node]"); + optparse_t * parser __attribute__((cleanup(optparse_del))) = optparse_new("ping", "[node]"); optparse_add_help(parser); optparse_add_unsigned(parser, 'n', "node", "NUM", 0, &node, "node (default = )"); optparse_add_unsigned(parser, 't', "timeout", "NUM", 0, &timeout, "timeout (default = )"); @@ -64,7 +64,6 @@ static int slash_csp_ping(struct slash *slash) int argi = optparse_parse(parser, slash->argc - 1, (const char **) slash->argv + 1); if (argi < 0) { - optparse_del(parser); return SLASH_EINVAL; } @@ -82,7 +81,6 @@ static int slash_csp_ping(struct slash *slash) slash_printf(slash, "No reply\n"); } - optparse_del(parser); return SLASH_SUCCESS; } @@ -93,13 +91,12 @@ static int slash_csp_reboot(struct slash *slash) unsigned int node = slash_dfl_node; - optparse_t * parser = optparse_new("reboot", "[node]"); + optparse_t * parser __attribute__((cleanup(optparse_del))) = optparse_new("reboot", "[node]"); optparse_add_help(parser); optparse_add_unsigned(parser, 'n', "node", "NUM", 0, &node, "node (default = )"); int argi = optparse_parse(parser, slash->argc - 1, (const char **) slash->argv + 1); if (argi < 0) { - optparse_del(parser); return SLASH_EINVAL; } @@ -110,7 +107,6 @@ static int slash_csp_reboot(struct slash *slash) csp_reboot(node); - optparse_del(parser); return SLASH_SUCCESS; } @@ -121,13 +117,12 @@ static int slash_csp_shutdown(struct slash *slash) unsigned int node = slash_dfl_node; - optparse_t * parser = optparse_new("shutdown", "[node]"); + optparse_t * parser __attribute__((cleanup(optparse_del))) = optparse_new("shutdown", "[node]"); optparse_add_help(parser); optparse_add_unsigned(parser, 'n', "node", "NUM", 0, &node, "node (default = )"); int argi = optparse_parse(parser, slash->argc - 1, (const char **) slash->argv + 1); if (argi < 0) { - optparse_del(parser); return SLASH_EINVAL; } @@ -138,7 +133,6 @@ static int slash_csp_shutdown(struct slash *slash) csp_shutdown(node); - optparse_del(parser); return SLASH_SUCCESS; } @@ -150,14 +144,13 @@ static int slash_csp_buffree(struct slash *slash) unsigned int node = slash_dfl_node; unsigned int timeout = slash_dfl_timeout; - optparse_t * parser = optparse_new("buffree", "[node]"); + optparse_t * parser __attribute__((cleanup(optparse_del))) = optparse_new("buffree", "[node]"); optparse_add_help(parser); optparse_add_unsigned(parser, 'n', "node", "NUM", 0, &node, "node (default = )"); optparse_add_unsigned(parser, 't', "timeout", "NUM", 0, &timeout, "timeout (default = )"); int argi = optparse_parse(parser, slash->argc - 1, (const char **) slash->argv + 1); if (argi < 0) { - optparse_del(parser); return SLASH_EINVAL; } @@ -168,7 +161,6 @@ static int slash_csp_buffree(struct slash *slash) csp_buf_free(node, timeout); - optparse_del(parser); return SLASH_SUCCESS; } @@ -180,14 +172,13 @@ static int slash_csp_uptime(struct slash *slash) unsigned int node = slash_dfl_node; unsigned int timeout = slash_dfl_timeout; - optparse_t * parser = optparse_new("uptime", "[node]"); + optparse_t * parser __attribute__((cleanup(optparse_del))) = optparse_new("uptime", "[node]"); optparse_add_help(parser); optparse_add_unsigned(parser, 'n', "node", "NUM", 0, &node, "node (default = )"); optparse_add_unsigned(parser, 't', "timeout", "NUM", 0, &timeout, "timeout (default = )"); int argi = optparse_parse(parser, slash->argc - 1, (const char **) slash->argv + 1); if (argi < 0) { - optparse_del(parser); return SLASH_EINVAL; } @@ -198,7 +189,6 @@ static int slash_csp_uptime(struct slash *slash) csp_uptime(node, timeout); - optparse_del(parser); return SLASH_SUCCESS; } @@ -211,14 +201,13 @@ static int slash_csp_cmp_ident(struct slash *slash) unsigned int node = slash_dfl_node; unsigned int timeout = slash_dfl_timeout; - optparse_t * parser = optparse_new("ident", "[node]"); + optparse_t * parser __attribute__((cleanup(optparse_del))) = optparse_new("ident", "[node]"); optparse_add_help(parser); optparse_add_unsigned(parser, 'n', "node", "NUM", 0, &node, "node (default = )"); optparse_add_unsigned(parser, 't', "timeout", "NUM", 0, &timeout, "timeout (default = )"); int argi = optparse_parse(parser, slash->argc - 1, (const char **) slash->argv + 1); if (argi < 0) { - optparse_del(parser); return SLASH_EINVAL; } @@ -233,14 +222,12 @@ static int slash_csp_cmp_ident(struct slash *slash) csp_conn_t * conn = csp_connect(CSP_PRIO_NORM, node, CSP_CMP, timeout, CSP_O_CRC32); if (conn == NULL) { - optparse_del(parser); return 0; } csp_packet_t * packet = csp_buffer_get(size); if (packet == NULL) { csp_close(conn); - optparse_del(parser); return 0; } @@ -262,7 +249,6 @@ static int slash_csp_cmp_ident(struct slash *slash) csp_close(conn); - optparse_del(parser); return SLASH_SUCCESS; } @@ -275,21 +261,19 @@ static int slash_csp_cmp_ifstat(struct slash *slash) unsigned int node = slash_dfl_node; unsigned int timeout = slash_dfl_timeout; - optparse_t * parser = optparse_new("ifstat", ""); + optparse_t * parser __attribute__((cleanup(optparse_del))) = optparse_new("ifstat", ""); optparse_add_help(parser); optparse_add_unsigned(parser, 'n', "node", "NUM", 0, &node, "node (default = )"); optparse_add_unsigned(parser, 't', "timeout", "NUM", 0, &timeout, "timeout (default = )"); int argi = optparse_parse(parser, slash->argc - 1, (const char **) slash->argv + 1); if (argi < 0) { - optparse_del(parser); return SLASH_EINVAL; } /* Expect ifname */ if (++argi >= slash->argc) { printf("missing interface name\n"); - optparse_del(parser); return SLASH_EINVAL; } @@ -301,7 +285,6 @@ static int slash_csp_cmp_ifstat(struct slash *slash) if (csp_cmp_if_stats(node, timeout, &message) != CSP_ERR_NONE) { printf("No response\n"); - optparse_del(parser); return SLASH_EINVAL; } @@ -331,7 +314,6 @@ static int slash_csp_cmp_ifstat(struct slash *slash) message.if_stats.txbytes, message.if_stats.rxbytes); - optparse_del(parser); return SLASH_SUCCESS; } @@ -343,21 +325,19 @@ static int slash_csp_cmp_peek(struct slash *slash) unsigned int node = slash_dfl_node; unsigned int timeout = slash_dfl_timeout; - optparse_t * parser = optparse_new("peek", " "); + optparse_t * parser __attribute__((cleanup(optparse_del))) = optparse_new("peek", " "); optparse_add_help(parser); optparse_add_unsigned(parser, 'n', "node", "NUM", 0, &node, "node (default = )"); optparse_add_unsigned(parser, 't', "timeout", "NUM", 0, &timeout, "timeout (default = )"); int argi = optparse_parse(parser, slash->argc - 1, (const char **) slash->argv + 1); if (argi < 0) { - optparse_del(parser); return SLASH_EINVAL; } /* Expect address */ if (++argi >= slash->argc) { printf("missing address\n"); - optparse_del(parser); return SLASH_EINVAL; } @@ -365,7 +345,6 @@ static int slash_csp_cmp_peek(struct slash *slash) uint32_t address = strtoul(slash->argv[argi], &endptr, 16); if (*endptr != '\0') { printf("Failed to parse address\n"); - optparse_del(parser); return SLASH_EUSAGE; } @@ -373,14 +352,12 @@ static int slash_csp_cmp_peek(struct slash *slash) /* Expect length */ if (++argi >= slash->argc) { printf("missing length\n"); - optparse_del(parser); return SLASH_EINVAL; } unsigned int length = strtoul(slash->argv[argi], &endptr, 10); if (*endptr != '\0') { printf("Failed to parse length\n"); - optparse_del(parser); return SLASH_EUSAGE; } @@ -391,14 +368,12 @@ static int slash_csp_cmp_peek(struct slash *slash) if (csp_cmp_peek(node, timeout, &message) != CSP_ERR_NONE) { printf("No response\n"); - optparse_del(parser); return SLASH_EINVAL; } printf("Peek at address %p len %u\n", (void *) (intptr_t) address, length); csp_hex_dump(NULL, message.peek.data, length); - optparse_del(parser); return SLASH_SUCCESS; } @@ -410,21 +385,19 @@ static int slash_csp_cmp_poke(struct slash *slash) unsigned int node = slash_dfl_node; unsigned int timeout = slash_dfl_timeout; - optparse_t * parser = optparse_new("poke", " "); + optparse_t * parser __attribute__((cleanup(optparse_del))) = optparse_new("poke", " "); optparse_add_help(parser); optparse_add_unsigned(parser, 'n', "node", "NUM", 0, &node, "node (default = )"); optparse_add_unsigned(parser, 't', "timeout", "NUM", 0, &timeout, "timeout (default = )"); int argi = optparse_parse(parser, slash->argc - 1, (const char **) slash->argv + 1); if (argi < 0) { - optparse_del(parser); return SLASH_EINVAL; } /* Expect address */ if (++argi >= slash->argc) { printf("missing address\n"); - optparse_del(parser); return SLASH_EINVAL; } @@ -432,14 +405,12 @@ static int slash_csp_cmp_poke(struct slash *slash) uint32_t address = strtoul(slash->argv[argi], &endptr, 16); if (*endptr != '\0') { printf("Failed to parse address\n"); - optparse_del(parser); return SLASH_EUSAGE; } /* Expect data */ if (++argi >= slash->argc) { printf("missing data\n"); - optparse_del(parser); return SLASH_EINVAL; } @@ -457,13 +428,11 @@ static int slash_csp_cmp_poke(struct slash *slash) if (csp_cmp_poke(node, timeout, &message) != CSP_ERR_NONE) { printf("No response\n"); - optparse_del(parser); return SLASH_EINVAL; } printf("Poke ok\n"); - optparse_del(parser); return SLASH_SUCCESS; } @@ -476,7 +445,7 @@ static int slash_csp_cmp_time(struct slash *slash) unsigned int timestamp = 0; int sync = 0; - optparse_t * parser = optparse_new("time", ""); + optparse_t * parser __attribute__((cleanup(optparse_del))) = optparse_new("time", ""); optparse_add_help(parser); optparse_add_set(parser, 's', "sync", 1, &sync, "sync time with CSH"); optparse_add_unsigned(parser, 'n', "node", "NUM", 0, &node, "node (default = )"); @@ -485,13 +454,11 @@ static int slash_csp_cmp_time(struct slash *slash) int argi = optparse_parse(parser, slash->argc - 1, (const char **) slash->argv + 1); if (argi < 0) { - optparse_del(parser); return SLASH_EINVAL; } if (sync != 0 && timestamp != 0) { printf("You cannot sync with both a specific timestamp and the local time\n"); - optparse_del(parser); return SLASH_EINVAL; } @@ -510,7 +477,6 @@ static int slash_csp_cmp_time(struct slash *slash) if (csp_cmp_clock(node, timeout, &message) != CSP_ERR_NONE) { printf("No response\n"); - optparse_del(parser); return SLASH_EINVAL; } @@ -522,7 +488,6 @@ static int slash_csp_cmp_time(struct slash *slash) printf("Remote time is %"PRIu32".%09"PRIu32" (diff %"PRIi64" ms)\n", message.clock.tv_sec, message.clock.tv_nsec, (remote_time_ns - local_time_ns) / 1000000); - optparse_del(parser); return SLASH_SUCCESS; } diff --git a/src/spaceboot_slash.c b/src/spaceboot_slash.c index 491751c8..1f31e831 100644 --- a/src/spaceboot_slash.c +++ b/src/spaceboot_slash.c @@ -83,7 +83,7 @@ static int slash_csp_switch(struct slash * slash) { unsigned int times = 1; unsigned int reboot_delay = 1000; - optparse_t * parser = optparse_new("switch", ""); + optparse_t * parser __attribute__((cleanup(optparse_del))) = optparse_new("switch", ""); optparse_add_help(parser); optparse_add_unsigned(parser, 'n', "node", "NUM", 0, &node, "node (default = )"); optparse_add_unsigned(parser, 'c', "count", "NUM", 0, ×, "number of times to boot into this slow (deafult = 1)"); @@ -91,14 +91,12 @@ static int slash_csp_switch(struct slash * slash) { int argi = optparse_parse(parser, slash->argc - 1, (const char **) slash->argv + 1); if (argi < 0) { - optparse_del(parser); return SLASH_EINVAL; } /* Expect slot */ if (++argi >= slash->argc) { printf("missing slot number\n"); - optparse_del(parser); return SLASH_EINVAL; } @@ -106,7 +104,6 @@ static int slash_csp_switch(struct slash * slash) { reset_to_flash(node, slot, times, reboot_delay); - optparse_del(parser); return SLASH_SUCCESS; } @@ -289,7 +286,7 @@ static int slash_csp_program(struct slash * slash) { int force = 0; int do_crc32 = 0; - optparse_t * parser = optparse_new("program", ""); + optparse_t * parser __attribute__((cleanup(optparse_del))) = optparse_new("program", ""); optparse_add_help(parser); optparse_add_unsigned(parser, 'n', "node", "NUM", 0, &node, "node (default = )"); optparse_add_string(parser, 'f', "file", "FILENAME", &filename, "File to upload (defaults to AUTO)"); @@ -300,7 +297,6 @@ static int slash_csp_program(struct slash * slash) { int argi = optparse_parse(parser, slash->argc - 1, (const char **) slash->argv + 1); if (argi < 0) { - optparse_del(parser); return SLASH_EINVAL; } @@ -309,7 +305,6 @@ static int slash_csp_program(struct slash * slash) { /* Expect slot */ if (++argi >= slash->argc) { printf("missing slot number\n"); - optparse_del(parser); return SLASH_EINVAL; } @@ -324,7 +319,6 @@ static int slash_csp_program(struct slash * slash) { vmem_client_find(node, slash_dfl_timeout, (void*)&vmem, 1, vmem_name, strlen(vmem_name)); if (vmem.size == 0) { printf("Failed to find vmem on subsystem\n"); - optparse_del(parser); return SLASH_EINVAL; } else { printf(" Found vmem\n"); @@ -357,7 +351,6 @@ static int slash_csp_program(struct slash * slash) { printf(" Found no valid binary for the selected slot.\n"); printf("\033[0m\n"); - optparse_del(parser); return SLASH_EINVAL; } } @@ -368,7 +361,6 @@ static int slash_csp_program(struct slash * slash) { char * c = slash_readline(slash); if (strlen(c) == 0) { printf("Abort\n"); - optparse_del(parser); return SLASH_EUSAGE; } index = atoi(c); @@ -380,7 +372,6 @@ static int slash_csp_program(struct slash * slash) { printf("ABOUT TO PROGRAM: %s\n", path); printf("\033[0m\n"); if (ping(node) == 0) { - optparse_del(parser); return SLASH_EINVAL; } printf("\n"); @@ -391,7 +382,6 @@ static int slash_csp_program(struct slash * slash) { if (strcmp(c, "yes") != 0) { printf("Abort\n"); - optparse_del(parser); return SLASH_EUSAGE; } } @@ -399,11 +389,9 @@ static int slash_csp_program(struct slash * slash) { char * data; int len; if (image_get(path, &data, &len) < 0) { - optparse_del(parser); return SLASH_EIO; } - optparse_del(parser); int result = SLASH_SUCCESS; @@ -452,7 +440,7 @@ static int slash_sps(struct slash * slash) { unsigned int reboot_delay = 1000; int do_crc32 = 0; - optparse_t * parser = optparse_new("sps", " "); + optparse_t * parser __attribute__((cleanup(optparse_del))) = optparse_new("sps", " "); optparse_add_help(parser); optparse_add_unsigned(parser, 'n', "node", "NUM", 0, &node, "node (default = )"); optparse_add_unsigned(parser, 'd', "delay", "NUM", 0, &reboot_delay, "Delay to allow module to boot (default = 1000 ms)"); @@ -462,7 +450,6 @@ static int slash_sps(struct slash * slash) { int argi = optparse_parse(parser, slash->argc - 1, (const char **) slash->argv + 1); if (argi < 0) { - optparse_del(parser); return SLASH_EINVAL; } @@ -471,7 +458,6 @@ static int slash_sps(struct slash * slash) { /* Expect from slot */ if (++argi >= slash->argc) { printf("missing from number\n"); - optparse_del(parser); return SLASH_EINVAL; } @@ -480,7 +466,6 @@ static int slash_sps(struct slash * slash) { /* Expect to slot */ if (++argi >= slash->argc) { printf("missing to number\n"); - optparse_del(parser); return SLASH_EINVAL; } @@ -496,7 +481,6 @@ static int slash_sps(struct slash * slash) { vmem_client_find(node, slash_dfl_timeout, (void*)&vmem, 1, vmem_name, strlen(vmem_name)); if (vmem.size == 0) { printf("Failed to find vmem on subsystem\n"); - optparse_del(parser); return SLASH_EINVAL; } else { printf(" Found vmem\n"); @@ -523,7 +507,6 @@ static int slash_sps(struct slash * slash) { printf("\033[31m\n"); printf(" Found no valid binary for the selected slot.\n"); printf("\033[0m\n"); - optparse_del(parser); return SLASH_EINVAL; } @@ -533,7 +516,6 @@ static int slash_sps(struct slash * slash) { char * c = slash_readline(slash); if (strlen(c) == 0) { printf("Abort\n"); - optparse_del(parser); return SLASH_EUSAGE; } index = atoi(c); @@ -545,7 +527,6 @@ static int slash_sps(struct slash * slash) { printf("ABOUT TO PROGRAM: %s\n", path); printf("\033[0m\n"); if (ping(node) == 0) { - optparse_del(parser); return SLASH_EINVAL; } printf("\n"); @@ -553,11 +534,9 @@ static int slash_sps(struct slash * slash) { char * data; int len; if (image_get(path, &data, &len) < 0) { - optparse_del(parser); return SLASH_EIO; } - optparse_del(parser); int result = SLASH_SUCCESS; diff --git a/src/stdbuf_client.c b/src/stdbuf_client.c index e0af50dc..e82353ef 100644 --- a/src/stdbuf_client.c +++ b/src/stdbuf_client.c @@ -17,7 +17,7 @@ static int stdbuf2_mon_slash(struct slash *slash) { unsigned int timeout = slash_dfl_timeout; char * log_name_tmp = 0; - optparse_t * parser = optparse_new("stdbuf2", ""); + optparse_t * parser __attribute__((cleanup(optparse_del))) = optparse_new("stdbuf2", ""); optparse_add_help(parser); optparse_add_unsigned(parser, 'n', "node", "NUM", 0, &node, "node (default = )"); optparse_add_unsigned(parser, 't', "timeout", "NUM", 0, &timeout, "timeout (default = )"); @@ -25,7 +25,6 @@ static int stdbuf2_mon_slash(struct slash *slash) { int argi = optparse_parse(parser, slash->argc - 1, (const char **) slash->argv + 1); if (argi < 0) { - optparse_del(parser); return SLASH_EINVAL; } @@ -65,14 +64,12 @@ static int stdbuf2_mon_slash(struct slash *slash) { csp_conn_t * conn = csp_connect(CSP_PRIO_HIGH, node, 15, 0, CSP_O_CRC32); if (conn == NULL) { - optparse_del(parser); return SLASH_ENOMEM; } csp_packet_t * packet = csp_buffer_get(1); if (packet == NULL) { csp_close(conn); - optparse_del(parser); return SLASH_ENOMEM; } packet->data[0] = 0xAA; @@ -111,7 +108,6 @@ static int stdbuf2_mon_slash(struct slash *slash) { csp_close(conn); - optparse_del(parser); return SLASH_SUCCESS; } diff --git a/src/stdbuf_mon.c b/src/stdbuf_mon.c index eaeebd22..c5891563 100644 --- a/src/stdbuf_mon.c +++ b/src/stdbuf_mon.c @@ -89,7 +89,7 @@ static int stdbuf_mon_slash(struct slash *slash) { unsigned int timeout = slash_dfl_timeout; unsigned int version = 2; - optparse_t * parser = optparse_new("stdbuf2", ""); + optparse_t * parser __attribute__((cleanup(optparse_del))) = optparse_new("stdbuf2", ""); optparse_add_help(parser); optparse_add_unsigned(parser, 'n', "node", "NUM", 0, &node, "node (default = )"); optparse_add_unsigned(parser, 't', "timeout", "NUM", 0, &timeout, "timeout (default = )"); @@ -97,7 +97,6 @@ static int stdbuf_mon_slash(struct slash *slash) { int argi = optparse_parse(parser, slash->argc - 1, (const char **) slash->argv + 1); if (argi < 0) { - optparse_del(parser); return SLASH_EINVAL; } @@ -115,7 +114,6 @@ static int stdbuf_mon_slash(struct slash *slash) { if (vmem.size == 0 || vmem.vaddr == 0) { printf("Could not find stdbuffer on node %u\n", node); - optparse_del(parser); return SLASH_EINVAL; } @@ -166,7 +164,6 @@ static int stdbuf_mon_slash(struct slash *slash) { }; - optparse_del(parser); return SLASH_SUCCESS; } diff --git a/src/victoria_metrics.c b/src/victoria_metrics.c index a72c7a18..1ad27908 100644 --- a/src/victoria_metrics.c +++ b/src/victoria_metrics.c @@ -212,7 +212,7 @@ static int vm_start_cmd(struct slash * slash) { char * tmp_password = NULL; vm_args * args = calloc(1, sizeof(vm_args)); - optparse_t * parser = optparse_new("vm start", ""); + optparse_t * parser __attribute__((cleanup(optparse_del))) = optparse_new("vm start", ""); optparse_add_help(parser); optparse_add_string(parser, 'u', "user", "STRING", &tmp_username, "Username for vmauth"); optparse_add_string(parser, 'p', "pass", "STRING", &tmp_password, "Password for vmauth"); @@ -225,14 +225,12 @@ static int vm_start_cmd(struct slash * slash) { int argi = optparse_parse(parser, slash->argc - 1, (const char **)slash->argv + 1); if (argi < 0) { - optparse_del(parser); free(args); return SLASH_EINVAL; } if (++argi >= slash->argc) { printf("Missing server ip/domain\n"); - optparse_del(parser); free(args); return SLASH_EINVAL; } @@ -240,7 +238,6 @@ static int vm_start_cmd(struct slash * slash) { if (tmp_username) { if (!tmp_password) { printf("Provide password with -p\n"); - optparse_del(parser); free(args); return SLASH_EINVAL; } @@ -257,7 +254,6 @@ static int vm_start_cmd(struct slash * slash) { param_sniffer_init(logfile); pthread_create(&vm_push_thread, NULL, &vm_push, args); vm_running = 1; - optparse_del(parser); return SLASH_SUCCESS; } diff --git a/src/vts.c b/src/vts.c index 9bb97f66..45478ad1 100644 --- a/src/vts.c +++ b/src/vts.c @@ -73,7 +73,7 @@ static int vts_init(struct slash * slash) { char * server_ip = NULL; int port_num = 8888; - optparse_t * parser = optparse_new("vts init", ""); + optparse_t * parser __attribute__((cleanup(optparse_del))) = optparse_new("vts init", ""); optparse_add_help(parser); optparse_add_string(parser, 's', "server-ip", "STRING", &server_ip, "Overwrite default ip 127.0.0.1"); optparse_add_int(parser, 'p', "server-port", "NUM", 0, &port_num, "Overwrite default port 8888"); @@ -82,7 +82,6 @@ static int vts_init(struct slash * slash) { int argi = optparse_parse(parser, slash->argc - 1, (const char **)slash->argv + 1); if (argi < 0) { - optparse_del(parser); return SLASH_EINVAL; } @@ -93,7 +92,6 @@ static int vts_init(struct slash * slash) { sockfd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); if (sockfd < 0) { printf("Failed to get socket for VTS\n"); - optparse_del(parser); return SLASH_EINVAL; } @@ -104,14 +102,12 @@ static int vts_init(struct slash * slash) { if(inet_pton(AF_INET, server_ip, &server_addr.sin_addr)<=0) { printf("Invalid address/ Address not supported\n"); close(sockfd); - optparse_del(parser); return SLASH_EINVAL; } if (connect(sockfd, (struct sockaddr *)&server_addr, sizeof(server_addr)) < 0) { printf("Connection failed\n"); close(sockfd); - optparse_del(parser); return SLASH_EINVAL; } @@ -119,7 +115,6 @@ static int vts_init(struct slash * slash) { if (send(sockfd, init_cmd, strlen(init_cmd), MSG_NOSIGNAL) == -1) { printf("Failed to send init\n"); close(sockfd); - optparse_del(parser); return SLASH_EINVAL; } @@ -127,7 +122,6 @@ static int vts_init(struct slash * slash) { printf("Streaming data to VTS at %s:%d\n", server_ip,port_num); vts_running = 1; - optparse_del(parser); return SLASH_SUCCESS; } slash_command_sub(vts, init, vts_init, "", "Push data to VTS");