Skip to contentSkip to conversation

main: Fix for misleading error with negative chain priority

PATCH In review Phil Sutter 2 participants 2 messages Oct 21, 2019 netfilter

lore
Share & tools
lore

Phil Sutter

author reviewer
#0 lore raw

PATCH 1/1 main: Fix for misleading error with negative chain priority

getopt_long() would try to parse the negative priority as an option and return -1 as it is not known:

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

Fix this by prefixing optstring with a plus character. This instructs getopt_long() to not collate arguments but just stop after the first non-option, leaving the rest for manual handling. In fact, this is just what nft desires: mixing options with nft syntax leads to confusive command lines anyway.

---
 src/main.c                                           | 2 +-
 tests/shell/testcases/chains/0039negative_priority_0 | 8 ++++++++
 2 files changed, 9 insertions(+), 1 deletion(-)
 create mode 100755 tests/shell/testcases/chains/0039negative_priority_0
src/main.c +1 -1
index f77d8a820a028..577850e54f68c 100644
@@ -45,7 +45,7 @@ enum opt_vals {
 	OPT_NUMERIC_TIME	= 't',
 	OPT_INVALID		= '?',
 };
-#define OPTSTRING	"hvcf:iI:jvnsNaeSupypt"
+#define OPTSTRING	"+hvcf:iI:jvnsNaeSupypt"
 
 static const struct option options[] = {
 	{
tests/shell/testcases/chains/0039negative_priority_0 new file +8 -0
new file mode 100755
index 0000000000000..ba17b8cc19eda
@@ -0,0 +1,8 @@
+#!/bin/bash
+
+# Test parsing of negative priority values
+
+set -e
+
+$NFT add table t
+$NFT add chain t c { type filter hook input priority -30\; }
Signature · 1 line
2.23.0
maintainer Acked-by on v1 1/1
#1 lore raw
Quoted 6 lines · Phil Sutter

getopt_long() would try to parse the negative priority as an option and return -1 as it is not known:

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

Quoted 7 lines

Fix this by prefixing optstring with a plus character. This instructs getopt_long() to not collate arguments but just stop after the first non-option, leaving the rest for manual handling. In fact, this is just what nft desires: mixing options with nft syntax leads to confusive command lines anyway.