From 9376c42a59929031610ea70d2fc6347fbfa46e67 Mon Sep 17 00:00:00 2001 From: Phil Sutter Date: Thu, 17 Oct 2019 23:36:47 +0200 Subject: xshared: Introduce struct argv_store The use of global variables in code around add_argv() is error-prone and hard to follow. Replace them by a struct which functions will modify instead of causing side-effects. Given the lack of static variables, this effectively makes argv construction code reentrant. Signed-off-by: Phil Sutter Acked-by: Florian Westphal --- iptables/iptables-restore.c | 28 +++++++++++++--------------- 1 file changed, 13 insertions(+), 15 deletions(-) (limited to 'iptables/iptables-restore.c') diff --git a/iptables/iptables-restore.c b/iptables/iptables-restore.c index 50d0708e..b0a51d49 100644 --- a/iptables/iptables-restore.c +++ b/iptables/iptables-restore.c @@ -94,6 +94,7 @@ ip46tables_restore_main(const struct iptables_restore_cb *cb, int argc, char *argv[]) { struct xtc_handle *handle = NULL; + struct argv_store av_store = {}; char buffer[10240]; int c, lock; char curtable[XT_TABLE_MAXNAMELEN + 1] = {}; @@ -311,34 +312,31 @@ ip46tables_restore_main(const struct iptables_restore_cb *cb, ret = 1; } else if (in_table) { - int a; char *pcnt = NULL; char *bcnt = NULL; char *parsestart = buffer; - add_argv(argv[0], 0); - add_argv("-t", 0); - add_argv(curtable, 0); + add_argv(&av_store, argv[0], 0); + add_argv(&av_store, "-t", 0); + add_argv(&av_store, curtable, 0); tokenize_rule_counters(&parsestart, &pcnt, &bcnt, line); if (counters && pcnt && bcnt) { - add_argv("--set-counters", 0); - add_argv(pcnt, 0); - add_argv(bcnt, 0); + add_argv(&av_store, "--set-counters", 0); + add_argv(&av_store, pcnt, 0); + add_argv(&av_store, bcnt, 0); } - add_param_to_argv(parsestart, line); + add_param_to_argv(&av_store, parsestart, line); DEBUGP("calling do_command(%u, argv, &%s, handle):\n", - newargc, curtable); + av_store.argc, curtable); + debug_print_argv(&av_store); - for (a = 0; a < newargc; a++) - DEBUGP("argv[%u]: %s\n", a, newargv[a]); + ret = cb->do_command(av_store.argc, av_store.argv, + &av_store.argv[2], &handle, true); - ret = cb->do_command(newargc, newargv, - &newargv[2], &handle, true); - - free_argv(); + free_argv(&av_store); fflush(stdout); } if (tablename && strcmp(tablename, curtable) != 0) -- cgit v1.2.3