Skip to contentSkip to conversation

main: misleading error reporting in chain definitions

PATCH In review Pablo Neira Ayuso 2 participants 3 messages Oct 21, 2019 netfilter

lore
Share & tools
lore

Pablo Neira Ayuso

author maintainer
#0 lore raw

PATCH 1/1 main: misleading error reporting in chain definitions

 # nft add chain x y { type filter hook input priority -30\; }
 nft: invalid option -- '3'

Fix this by restricting getopt_long() to the first curly brace.

---
 src/main.c | 23 +++++++++++++++++++++--
 1 file changed, 21 insertions(+), 2 deletions(-)
src/main.c +21 -2
index f77d8a820a02..0d4a45b30d20 100644
@@ -192,19 +192,38 @@ static const struct {
 	},
 };
 
+static int argc_getopt(int argc, char * const *argv)
+{
+	int i;
+
+	/* Restrict getopt_long() parsing to the first curly brace, so users
+	 * do not need to invoke nft with an upfront -- to specify chain
+	 * priority.
+	 */
+	for (i = 0; i < argc; i++) {
+		if (argv[i][0] == '{') {
+			return i;
+		}
+	}
+
+	return argc;
+}
+
 int main(int argc, char * const *argv)
 {
 	char *buf = NULL, *filename = NULL;
 	unsigned int output_flags = 0;
+	int i, val, rc, __argc;
 	bool interactive = false;
 	unsigned int debug_mask;
 	unsigned int len;
-	int i, val, rc;
+
+	__argc = argc_getopt(argc, argv);
 
 	nft = nft_ctx_new(NFT_CTX_DEFAULT);
 
 	while (1) {
-		val = getopt_long(argc, argv, OPTSTRING, options, NULL);
+		val = getopt_long(__argc, argv, OPTSTRING, options, NULL);
 		if (val == -1)
 			break;
 
Signature · 1 line
2.11.0
reviewer
#1 lore raw

Hi Pablo,

Quoted 4 lines · Pablo Neira Ayuso
 # nft add chain x y { type filter hook input priority -30\; }
 nft: invalid option -- '3'

Fix this by restricting getopt_long() to the first curly brace.

Wouldn't it suffice to set the first char of OPTSTRING to '+'? Exporting POSIXLY_CORRECT=1 prior to calling the above command works at least.

Cheers, Phil

author maintainer
#2 lore raw
Quoted 10 lines · Phil Sutter

Hi Pablo,

Quoted 4 lines · Pablo Neira Ayuso
 # nft add chain x y { type filter hook input priority -30\; }
 nft: invalid option -- '3'

Fix this by restricting getopt_long() to the first curly brace.

Wouldn't it suffice to set the first char of OPTSTRING to '+'? Exporting POSIXLY_CORRECT=1 prior to calling the above command works at least.

Would you post a patch? :-)