From 1cc09188079a64dc8b733f198c959cfb441e6e20 Mon Sep 17 00:00:00 2001 From: Phil Sutter Date: Thu, 2 Aug 2018 17:05:16 +0200 Subject: xshared: Consolidate parse_counters() Move this helper function into xshared. While being at it, drop the need for temporary variables and take over null pointer tolerance from the implementation in iptables-xml.c. Signed-off-by: Phil Sutter Signed-off-by: Florian Westphal --- iptables/ip6tables-restore.c | 11 ----------- iptables/iptables-restore.c | 11 ----------- iptables/iptables-xml.c | 16 ---------------- iptables/xshared.c | 14 ++++++++++++++ iptables/xshared.h | 1 + iptables/xtables-restore.c | 11 ----------- 6 files changed, 15 insertions(+), 49 deletions(-) diff --git a/iptables/ip6tables-restore.c b/iptables/ip6tables-restore.c index 3065768e..f2bd93d7 100644 --- a/iptables/ip6tables-restore.c +++ b/iptables/ip6tables-restore.c @@ -79,17 +79,6 @@ static struct xtc_handle *create_handle(const char *tablename) return handle; } -static int parse_counters(char *string, struct xt_counters *ctr) -{ - unsigned long long pcnt, bcnt; - int ret; - - ret = sscanf(string, "[%llu:%llu]", &pcnt, &bcnt); - ctr->pcnt = pcnt; - ctr->bcnt = bcnt; - return ret == 2; -} - /* global new argv and argc */ static char *newargv[255]; static int newargc; diff --git a/iptables/iptables-restore.c b/iptables/iptables-restore.c index e52e8e47..a1ae0311 100644 --- a/iptables/iptables-restore.c +++ b/iptables/iptables-restore.c @@ -76,17 +76,6 @@ static struct xtc_handle *create_handle(const char *tablename) return handle; } -static int parse_counters(char *string, struct xt_counters *ctr) -{ - unsigned long long pcnt, bcnt; - int ret; - - ret = sscanf(string, "[%llu:%llu]", &pcnt, &bcnt); - ctr->pcnt = pcnt; - ctr->bcnt = bcnt; - return ret == 2; -} - /* global new argv and argc */ static char *newargv[255]; static int newargc; diff --git a/iptables/iptables-xml.c b/iptables/iptables-xml.c index a1b09f3a..8ba45d55 100644 --- a/iptables/iptables-xml.c +++ b/iptables/iptables-xml.c @@ -50,22 +50,6 @@ print_usage(const char *name, const char *version) exit(1); } -static int -parse_counters(char *string, struct xt_counters *ctr) -{ - __u64 *pcnt, *bcnt; - - if (string != NULL) { - pcnt = &ctr->pcnt; - bcnt = &ctr->bcnt; - return (sscanf - (string, "[%llu:%llu]", - (unsigned long long *)pcnt, - (unsigned long long *)bcnt) == 2); - } else - return (0 == 2); -} - /* global new argv and argc */ static char *newargv[255]; static unsigned int newargc; diff --git a/iptables/xshared.c b/iptables/xshared.c index 06db72d4..ec5c4955 100644 --- a/iptables/xshared.c +++ b/iptables/xshared.c @@ -359,6 +359,20 @@ void parse_wait_interval(int argc, char *argv[], struct timeval *wait_interval) xtables_error(PARAMETER_PROBLEM, "wait interval not numeric"); } +int parse_counters(const char *string, struct xt_counters *ctr) +{ + int ret; + + if (!string) + return 0; + + ret = sscanf(string, "[%llu:%llu]", + (unsigned long long *)&ctr->pcnt, + (unsigned long long *)&ctr->bcnt); + + return ret == 2; +} + inline bool xs_has_arg(int argc, char *argv[]) { return optind < argc && diff --git a/iptables/xshared.h b/iptables/xshared.h index 363549db..55249341 100644 --- a/iptables/xshared.h +++ b/iptables/xshared.h @@ -150,6 +150,7 @@ extern int xtables_lock_or_exit(int wait, struct timeval *tv); int parse_wait_time(int argc, char *argv[]); void parse_wait_interval(int argc, char *argv[], struct timeval *wait_interval); +int parse_counters(const char *string, struct xt_counters *ctr); bool xs_has_arg(int argc, char *argv[]); extern const struct xtables_afinfo *afinfo; diff --git a/iptables/xtables-restore.c b/iptables/xtables-restore.c index 0a3d1a31..018d3fd3 100644 --- a/iptables/xtables-restore.c +++ b/iptables/xtables-restore.c @@ -56,17 +56,6 @@ static void print_usage(const char *name, const char *version) " [ --ipv6 ]\n", name); } -static int parse_counters(char *string, struct xt_counters *ctr) -{ - unsigned long long pcnt, bcnt; - int ret; - - ret = sscanf(string, "[%llu:%llu]", &pcnt, &bcnt); - ctr->pcnt = pcnt; - ctr->bcnt = bcnt; - return ret == 2; -} - /* global new argv and argc */ static char *newargv[255]; static int newargc; -- cgit v1.2.3