Blame SOURCES/iptables-1.4.21-remove_duplicated_argument_parsing.patch

26b15f
Adapted version of
26b15f
26b15f
commit 6e2e169eb66b63d2991e1c7ada931e3cdb0ced32
26b15f
Author: Lorenzo Colitti <lorenzo@google.com>
26b15f
Date:   Thu Mar 16 16:55:01 2017 +0900
26b15f
26b15f
    iptables: remove duplicated argument parsing code
26b15f
    
26b15f
    1. Factor out repeated code to a new xs_has_arg function.
26b15f
    2. Add a new parse_wait_time option to parse the value of -w.
26b15f
    3. Make parse_wait_interval take argc and argv so its callers
26b15f
       can be simpler.
26b15f
    
26b15f
    Signed-off-by: Lorenzo Colitti <lorenzo@google.com>
26b15f
    Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
26b15f
26b15f
diff -up iptables-1.4.21/iptables/ip6tables.c.remove_duplicated_argument_parsing iptables-1.4.21/iptables/ip6tables.c
26b15f
--- iptables-1.4.21/iptables/ip6tables.c.remove_duplicated_argument_parsing	2017-04-05 14:51:44.033970476 +0200
26b15f
+++ iptables-1.4.21/iptables/ip6tables.c	2017-04-05 14:51:44.044970566 +0200
26b15f
@@ -1388,8 +1388,7 @@ int do_command6(int argc, char *argv[],
26b15f
 			add_command(&command, CMD_DELETE, CMD_NONE,
26b15f
 				    cs.invert);
26b15f
 			chain = optarg;
26b15f
-			if (optind < argc && argv[optind][0] != '-'
26b15f
-			    && argv[optind][0] != '!') {
26b15f
+			if (xs_has_arg(argc, argv)) {
26b15f
 				rulenum = parse_rulenumber(argv[optind++]);
26b15f
 				command = CMD_DELETE_NUM;
26b15f
 			}
26b15f
@@ -1399,8 +1398,7 @@ int do_command6(int argc, char *argv[],
26b15f
 			add_command(&command, CMD_REPLACE, CMD_NONE,
26b15f
 				    cs.invert);
26b15f
 			chain = optarg;
26b15f
-			if (optind < argc && argv[optind][0] != '-'
26b15f
-			    && argv[optind][0] != '!')
26b15f
+			if (xs_has_arg(argc, argv))
26b15f
 				rulenum = parse_rulenumber(argv[optind++]);
26b15f
 			else
26b15f
 				xtables_error(PARAMETER_PROBLEM,
26b15f
@@ -1412,8 +1410,7 @@ int do_command6(int argc, char *argv[],
26b15f
 			add_command(&command, CMD_INSERT, CMD_NONE,
26b15f
 				    cs.invert);
26b15f
 			chain = optarg;
26b15f
-			if (optind < argc && argv[optind][0] != '-'
26b15f
-			    && argv[optind][0] != '!')
26b15f
+			if (xs_has_arg(argc, argv))
26b15f
 				rulenum = parse_rulenumber(argv[optind++]);
26b15f
 			else rulenum = 1;
26b15f
 			break;
26b15f
@@ -1422,11 +1419,9 @@ int do_command6(int argc, char *argv[],
26b15f
 			add_command(&command, CMD_LIST,
26b15f
 				    CMD_ZERO | CMD_ZERO_NUM, cs.invert);
26b15f
 			if (optarg) chain = optarg;
26b15f
-			else if (optind < argc && argv[optind][0] != '-'
26b15f
-				 && argv[optind][0] != '!')
26b15f
+			else if (xs_has_arg(argc, argv))
26b15f
 				chain = argv[optind++];
26b15f
-			if (optind < argc && argv[optind][0] != '-'
26b15f
-			    && argv[optind][0] != '!')
26b15f
+			if (xs_has_arg(argc, argv))
26b15f
 				rulenum = parse_rulenumber(argv[optind++]);
26b15f
 			break;
26b15f
 
26b15f
@@ -1434,11 +1429,9 @@ int do_command6(int argc, char *argv[],
26b15f
 			add_command(&command, CMD_LIST_RULES,
26b15f
 				    CMD_ZERO | CMD_ZERO_NUM, cs.invert);
26b15f
 			if (optarg) chain = optarg;
26b15f
-			else if (optind < argc && argv[optind][0] != '-'
26b15f
-				 && argv[optind][0] != '!')
26b15f
+			else if (xs_has_arg(argc, argv))
26b15f
 				chain = argv[optind++];
26b15f
-			if (optind < argc && argv[optind][0] != '-'
26b15f
-			    && argv[optind][0] != '!')
26b15f
+			if (xs_has_arg(argc, argv))
26b15f
 				rulenum = parse_rulenumber(argv[optind++]);
26b15f
 			break;
26b15f
 
26b15f
@@ -1446,8 +1439,7 @@ int do_command6(int argc, char *argv[],
26b15f
 			add_command(&command, CMD_FLUSH, CMD_NONE,
26b15f
 				    cs.invert);
26b15f
 			if (optarg) chain = optarg;
26b15f
-			else if (optind < argc && argv[optind][0] != '-'
26b15f
-				 && argv[optind][0] != '!')
26b15f
+			else if (xs_has_arg(argc, argv))
26b15f
 				chain = argv[optind++];
26b15f
 			break;
26b15f
 
26b15f
@@ -1455,11 +1447,9 @@ int do_command6(int argc, char *argv[],
26b15f
 			add_command(&command, CMD_ZERO, CMD_LIST|CMD_LIST_RULES,
26b15f
 				    cs.invert);
26b15f
 			if (optarg) chain = optarg;
26b15f
-			else if (optind < argc && argv[optind][0] != '-'
26b15f
-				&& argv[optind][0] != '!')
26b15f
+			else if (xs_has_arg(argc, argv))
26b15f
 				chain = argv[optind++];
26b15f
-			if (optind < argc && argv[optind][0] != '-'
26b15f
-				&& argv[optind][0] != '!') {
26b15f
+			if (xs_has_arg(argc, argv)) {
26b15f
 				rulenum = parse_rulenumber(argv[optind++]);
26b15f
 				command = CMD_ZERO_NUM;
26b15f
 			}
26b15f
@@ -1476,8 +1466,7 @@ int do_command6(int argc, char *argv[],
26b15f
 			add_command(&command, CMD_DELETE_CHAIN, CMD_NONE,
26b15f
 				    cs.invert);
26b15f
 			if (optarg) chain = optarg;
26b15f
-			else if (optind < argc && argv[optind][0] != '-'
26b15f
-				 && argv[optind][0] != '!')
26b15f
+			else if (xs_has_arg(argc, argv))
26b15f
 				chain = argv[optind++];
26b15f
 			break;
26b15f
 
26b15f
@@ -1485,8 +1474,7 @@ int do_command6(int argc, char *argv[],
26b15f
 			add_command(&command, CMD_RENAME_CHAIN, CMD_NONE,
26b15f
 				    cs.invert);
26b15f
 			chain = optarg;
26b15f
-			if (optind < argc && argv[optind][0] != '-'
26b15f
-			    && argv[optind][0] != '!')
26b15f
+			if (xs_has_arg(argc, argv))
26b15f
 				newname = argv[optind++];
26b15f
 			else
26b15f
 				xtables_error(PARAMETER_PROBLEM,
26b15f
@@ -1499,8 +1487,7 @@ int do_command6(int argc, char *argv[],
26b15f
 			add_command(&command, CMD_SET_POLICY, CMD_NONE,
26b15f
 				    cs.invert);
26b15f
 			chain = optarg;
26b15f
-			if (optind < argc && argv[optind][0] != '-'
26b15f
-			    && argv[optind][0] != '!')
26b15f
+			if (xs_has_arg(argc, argv))
26b15f
 				policy = argv[optind++];
26b15f
 			else
26b15f
 				xtables_error(PARAMETER_PROBLEM,
26b15f
@@ -1610,16 +1597,7 @@ int do_command6(int argc, char *argv[],
26b15f
 					      "You cannot use `-w' from "
26b15f
 					      "ip6tables-restore");
26b15f
 			}
26b15f
-			wait = -1;
26b15f
-			if (optarg) {
26b15f
-				if (sscanf(optarg, "%i", &wait) != 1)
26b15f
-					xtables_error(PARAMETER_PROBLEM,
26b15f
-						"wait seconds not numeric");
26b15f
-			} else if (optind < argc && argv[optind][0] != '-'
26b15f
-						 && argv[optind][0] != '!')
26b15f
-				if (sscanf(argv[optind++], "%i", &wait) != 1)
26b15f
-					xtables_error(PARAMETER_PROBLEM,
26b15f
-						"wait seconds not numeric");
26b15f
+			wait = parse_wait_time(argc, argv);
26b15f
 			break;
26b15f
 
26b15f
 		case 'W':
26b15f
@@ -1628,14 +1606,7 @@ int do_command6(int argc, char *argv[],
26b15f
 					      "You cannot use `-W' from "
26b15f
 					      "ip6tables-restore");
26b15f
 			}
26b15f
-			if (optarg)
26b15f
-				parse_wait_interval(optarg, &wait_interval);
26b15f
-			else if (optind < argc &&
26b15f
-				argv[optind][0] != '-' &&
26b15f
-				argv[optind][0] != '!')
26b15f
-				parse_wait_interval(argv[optind++],
26b15f
-						    &wait_interval);
26b15f
-
26b15f
+			parse_wait_interval(argc, argv, &wait_interval);
26b15f
 			wait_interval_set = true;
26b15f
 			break;
26b15f
 
26b15f
@@ -1685,8 +1656,7 @@ int do_command6(int argc, char *argv[],
26b15f
 			bcnt = strchr(pcnt + 1, ',');
26b15f
 			if (bcnt)
26b15f
 			    bcnt++;
26b15f
-			if (!bcnt && optind < argc && argv[optind][0] != '-'
26b15f
-			    && argv[optind][0] != '!')
26b15f
+			if (!bcnt && xs_has_arg(argc, argv))
26b15f
 				bcnt = argv[optind++];
26b15f
 			if (!bcnt)
26b15f
 				xtables_error(PARAMETER_PROBLEM,
26b15f
diff -up iptables-1.4.21/iptables/iptables.c.remove_duplicated_argument_parsing iptables-1.4.21/iptables/iptables.c
26b15f
--- iptables-1.4.21/iptables/iptables.c.remove_duplicated_argument_parsing	2017-04-05 14:51:44.034970484 +0200
26b15f
+++ iptables-1.4.21/iptables/iptables.c	2017-04-05 14:51:44.044970566 +0200
26b15f
@@ -1381,8 +1381,7 @@ int do_command4(int argc, char *argv[],
26b15f
 			add_command(&command, CMD_DELETE, CMD_NONE,
26b15f
 				    cs.invert);
26b15f
 			chain = optarg;
26b15f
-			if (optind < argc && argv[optind][0] != '-'
26b15f
-			    && argv[optind][0] != '!') {
26b15f
+			if (xs_has_arg(argc, argv)) {
26b15f
 				rulenum = parse_rulenumber(argv[optind++]);
26b15f
 				command = CMD_DELETE_NUM;
26b15f
 			}
26b15f
@@ -1392,8 +1391,7 @@ int do_command4(int argc, char *argv[],
26b15f
 			add_command(&command, CMD_REPLACE, CMD_NONE,
26b15f
 				    cs.invert);
26b15f
 			chain = optarg;
26b15f
-			if (optind < argc && argv[optind][0] != '-'
26b15f
-			    && argv[optind][0] != '!')
26b15f
+			if (xs_has_arg(argc, argv))
26b15f
 				rulenum = parse_rulenumber(argv[optind++]);
26b15f
 			else
26b15f
 				xtables_error(PARAMETER_PROBLEM,
26b15f
@@ -1405,8 +1403,7 @@ int do_command4(int argc, char *argv[],
26b15f
 			add_command(&command, CMD_INSERT, CMD_NONE,
26b15f
 				    cs.invert);
26b15f
 			chain = optarg;
26b15f
-			if (optind < argc && argv[optind][0] != '-'
26b15f
-			    && argv[optind][0] != '!')
26b15f
+			if (xs_has_arg(argc, argv))
26b15f
 				rulenum = parse_rulenumber(argv[optind++]);
26b15f
 			else rulenum = 1;
26b15f
 			break;
26b15f
@@ -1415,11 +1412,9 @@ int do_command4(int argc, char *argv[],
26b15f
 			add_command(&command, CMD_LIST,
26b15f
 				    CMD_ZERO | CMD_ZERO_NUM, cs.invert);
26b15f
 			if (optarg) chain = optarg;
26b15f
-			else if (optind < argc && argv[optind][0] != '-'
26b15f
-				 && argv[optind][0] != '!')
26b15f
+			else if (xs_has_arg(argc, argv))
26b15f
 				chain = argv[optind++];
26b15f
-			if (optind < argc && argv[optind][0] != '-'
26b15f
-			    && argv[optind][0] != '!')
26b15f
+			if (xs_has_arg(argc, argv))
26b15f
 				rulenum = parse_rulenumber(argv[optind++]);
26b15f
 			break;
26b15f
 
26b15f
@@ -1427,11 +1422,9 @@ int do_command4(int argc, char *argv[],
26b15f
 			add_command(&command, CMD_LIST_RULES,
26b15f
 				    CMD_ZERO|CMD_ZERO_NUM, cs.invert);
26b15f
 			if (optarg) chain = optarg;
26b15f
-			else if (optind < argc && argv[optind][0] != '-'
26b15f
-				 && argv[optind][0] != '!')
26b15f
+			else if (xs_has_arg(argc, argv))
26b15f
 				chain = argv[optind++];
26b15f
-			if (optind < argc && argv[optind][0] != '-'
26b15f
-			    && argv[optind][0] != '!')
26b15f
+			if (xs_has_arg(argc, argv))
26b15f
 				rulenum = parse_rulenumber(argv[optind++]);
26b15f
 			break;
26b15f
 
26b15f
@@ -1439,8 +1432,7 @@ int do_command4(int argc, char *argv[],
26b15f
 			add_command(&command, CMD_FLUSH, CMD_NONE,
26b15f
 				    cs.invert);
26b15f
 			if (optarg) chain = optarg;
26b15f
-			else if (optind < argc && argv[optind][0] != '-'
26b15f
-				 && argv[optind][0] != '!')
26b15f
+			else if (xs_has_arg(argc, argv))
26b15f
 				chain = argv[optind++];
26b15f
 			break;
26b15f
 
26b15f
@@ -1448,11 +1440,9 @@ int do_command4(int argc, char *argv[],
26b15f
 			add_command(&command, CMD_ZERO, CMD_LIST|CMD_LIST_RULES,
26b15f
 				    cs.invert);
26b15f
 			if (optarg) chain = optarg;
26b15f
-			else if (optind < argc && argv[optind][0] != '-'
26b15f
-				&& argv[optind][0] != '!')
26b15f
+			else if (xs_has_arg(argc, argv))
26b15f
 				chain = argv[optind++];
26b15f
-			if (optind < argc && argv[optind][0] != '-'
26b15f
-				&& argv[optind][0] != '!') {
26b15f
+			if (xs_has_arg(argc, argv)) {
26b15f
 				rulenum = parse_rulenumber(argv[optind++]);
26b15f
 				command = CMD_ZERO_NUM;
26b15f
 			}
26b15f
@@ -1469,8 +1459,7 @@ int do_command4(int argc, char *argv[],
26b15f
 			add_command(&command, CMD_DELETE_CHAIN, CMD_NONE,
26b15f
 				    cs.invert);
26b15f
 			if (optarg) chain = optarg;
26b15f
-			else if (optind < argc && argv[optind][0] != '-'
26b15f
-				 && argv[optind][0] != '!')
26b15f
+			else if (xs_has_arg(argc, argv))
26b15f
 				chain = argv[optind++];
26b15f
 			break;
26b15f
 
26b15f
@@ -1478,8 +1467,7 @@ int do_command4(int argc, char *argv[],
26b15f
 			add_command(&command, CMD_RENAME_CHAIN, CMD_NONE,
26b15f
 				    cs.invert);
26b15f
 			chain = optarg;
26b15f
-			if (optind < argc && argv[optind][0] != '-'
26b15f
-			    && argv[optind][0] != '!')
26b15f
+			if (xs_has_arg(argc, argv))
26b15f
 				newname = argv[optind++];
26b15f
 			else
26b15f
 				xtables_error(PARAMETER_PROBLEM,
26b15f
@@ -1492,8 +1480,7 @@ int do_command4(int argc, char *argv[],
26b15f
 			add_command(&command, CMD_SET_POLICY, CMD_NONE,
26b15f
 				    cs.invert);
26b15f
 			chain = optarg;
26b15f
-			if (optind < argc && argv[optind][0] != '-'
26b15f
-			    && argv[optind][0] != '!')
26b15f
+			if (xs_has_arg(argc, argv))
26b15f
 				policy = argv[optind++];
26b15f
 			else
26b15f
 				xtables_error(PARAMETER_PROBLEM,
26b15f
@@ -1601,16 +1588,7 @@ int do_command4(int argc, char *argv[],
26b15f
 					      "You cannot use `-w' from "
26b15f
 					      "iptables-restore");
26b15f
 			}
26b15f
-			wait = -1;
26b15f
-			if (optarg) {
26b15f
-				if (sscanf(optarg, "%i", &wait) != 1)
26b15f
-					xtables_error(PARAMETER_PROBLEM,
26b15f
-						"wait seconds not numeric");
26b15f
-			} else if (optind < argc && argv[optind][0] != '-'
26b15f
-						 && argv[optind][0] != '!')
26b15f
-				if (sscanf(argv[optind++], "%i", &wait) != 1)
26b15f
-					xtables_error(PARAMETER_PROBLEM,
26b15f
-						"wait seconds not numeric");
26b15f
+			wait = parse_wait_time(argc, argv);
26b15f
 			break;
26b15f
 
26b15f
 		case 'W':
26b15f
@@ -1619,14 +1597,7 @@ int do_command4(int argc, char *argv[],
26b15f
 					      "You cannot use `-W' from "
26b15f
 					      "iptables-restore");
26b15f
 			}
26b15f
-			if (optarg)
26b15f
-				parse_wait_interval(optarg, &wait_interval);
26b15f
-			else if (optind < argc &&
26b15f
-				 argv[optind][0] != '-' &&
26b15f
-				 argv[optind][0] != '!')
26b15f
-				parse_wait_interval(argv[optind++],
26b15f
-						    &wait_interval);
26b15f
-
26b15f
+			parse_wait_interval(argc, argv, &wait_interval);
26b15f
 			wait_interval_set = true;
26b15f
 			break;
26b15f
 
26b15f
@@ -1676,8 +1647,7 @@ int do_command4(int argc, char *argv[],
26b15f
 			bcnt = strchr(pcnt + 1, ',');
26b15f
 			if (bcnt)
26b15f
 			    bcnt++;
26b15f
-			if (!bcnt && optind < argc && argv[optind][0] != '-'
26b15f
-			    && argv[optind][0] != '!')
26b15f
+			if (!bcnt && xs_has_arg(argc, argv))
26b15f
 				bcnt = argv[optind++];
26b15f
 			if (!bcnt)
26b15f
 				xtables_error(PARAMETER_PROBLEM,
26b15f
diff -up iptables-1.4.21/iptables/xshared.c.remove_duplicated_argument_parsing iptables-1.4.21/iptables/xshared.c
26b15f
--- iptables-1.4.21/iptables/xshared.c.remove_duplicated_argument_parsing	2017-04-05 14:51:44.042970550 +0200
26b15f
+++ iptables-1.4.21/iptables/xshared.c	2017-04-05 14:51:44.045970574 +0200
26b15f
@@ -285,12 +285,36 @@ bool xtables_lock(int wait, struct timev
26b15f
 	}
26b15f
 }
26b15f
 
26b15f
-void parse_wait_interval(const char *str, struct timeval *wait_interval)
26b15f
+int parse_wait_time(int argc, char *argv[])
26b15f
 {
26b15f
+	int wait = -1;
26b15f
+
26b15f
+	if (optarg) {
26b15f
+		if (sscanf(optarg, "%i", &wait) != 1)
26b15f
+			xtables_error(PARAMETER_PROBLEM,
26b15f
+				"wait seconds not numeric");
26b15f
+	} else if (xs_has_arg(argc, argv))
26b15f
+		if (sscanf(argv[optind++], "%i", &wait) != 1)
26b15f
+			xtables_error(PARAMETER_PROBLEM,
26b15f
+				"wait seconds not numeric");
26b15f
+
26b15f
+	return wait;
26b15f
+}
26b15f
+
26b15f
+void parse_wait_interval(int argc, char *argv[], struct timeval *wait_interval)
26b15f
+{
26b15f
+	const char *arg;
26b15f
 	unsigned int usec;
26b15f
 	int ret;
26b15f
 
26b15f
-	ret = sscanf(str, "%u", &usec);
26b15f
+	if (optarg)
26b15f
+		arg = optarg;
26b15f
+	else if (xs_has_arg(argc, argv))
26b15f
+		arg = argv[optind++];
26b15f
+	else
26b15f
+		return;
26b15f
+
26b15f
+	ret = sscanf(arg, "%u", &usec);
26b15f
 	if (ret == 1) {
26b15f
 		if (usec > 999999)
26b15f
 			xtables_error(PARAMETER_PROBLEM,
26b15f
@@ -303,3 +327,10 @@ void parse_wait_interval(const char *str
26b15f
 	}
26b15f
 	xtables_error(PARAMETER_PROBLEM, "wait interval not numeric");
26b15f
 }
26b15f
+
26b15f
+inline bool xs_has_arg(int argc, char *argv[])
26b15f
+{
26b15f
+	return optind < argc &&
26b15f
+	       argv[optind][0] != '-' &&
26b15f
+	       argv[optind][0] != '!';
26b15f
+}
26b15f
diff -up iptables-1.4.21/iptables/xshared.h.remove_duplicated_argument_parsing iptables-1.4.21/iptables/xshared.h
26b15f
--- iptables-1.4.21/iptables/xshared.h.remove_duplicated_argument_parsing	2017-04-05 14:51:44.034970484 +0200
26b15f
+++ iptables-1.4.21/iptables/xshared.h	2017-04-05 14:51:44.045970574 +0200
26b15f
@@ -86,7 +86,9 @@ extern void xs_init_target(struct xtable
26b15f
 extern void xs_init_match(struct xtables_match *);
26b15f
 bool xtables_lock(int wait, struct timeval *wait_interval);
26b15f
 
26b15f
-void parse_wait_interval(const char *str, struct timeval *wait_interval);
26b15f
+int parse_wait_time(int argc, char *argv[]);
26b15f
+void parse_wait_interval(int argc, char *argv[], struct timeval *wait_interval);
26b15f
+bool xs_has_arg(int argc, char *argv[]);
26b15f
 
26b15f
 extern const struct xtables_afinfo *afinfo;
26b15f