From a00d9d06b5021c27333e52e88b99493fa56d15a1 Mon Sep 17 00:00:00 2001 From: Phil Sutter Date: Tue, 9 Mar 2021 13:41:44 +0100 Subject: Get rid of single option switch statements Replace each by a conditional testing the only valid case. There is one odd example, namely src/set.c: When printing a set with type NFTNL_OUTPUT_XML, the relevant function would return 0 instead of -1 like all others. Just drop it assuming nothing depends on that (faulty) behaviour. Cc: Arturo Borrero Signed-off-by: Phil Sutter --- src/chain.c | 12 +++--------- src/flowtable.c | 13 +++---------- src/gen.c | 10 +++------- src/object.c | 13 +++---------- src/rule.c | 14 ++++---------- src/ruleset.c | 26 +++----------------------- src/set.c | 17 +++++------------ src/set_elem.c | 15 ++++----------- src/table.c | 12 +++--------- 9 files changed, 31 insertions(+), 101 deletions(-) diff --git a/src/chain.c b/src/chain.c index 7906bf4..cb5ec6b 100644 --- a/src/chain.c +++ b/src/chain.c @@ -890,17 +890,11 @@ static int nftnl_chain_cmd_snprintf(char *buf, size_t remain, { int ret, offset = 0; - switch (type) { - case NFTNL_OUTPUT_DEFAULT: - ret = nftnl_chain_snprintf_default(buf + offset, remain, c); - SNPRINTF_BUFFER_SIZE(ret, remain, offset); - break; - case NFTNL_OUTPUT_XML: - case NFTNL_OUTPUT_JSON: - default: + if (type != NFTNL_OUTPUT_DEFAULT) return -1; - } + ret = nftnl_chain_snprintf_default(buf + offset, remain, c); + SNPRINTF_BUFFER_SIZE(ret, remain, offset); return offset; } diff --git a/src/flowtable.c b/src/flowtable.c index d651066..e6c2475 100644 --- a/src/flowtable.c +++ b/src/flowtable.c @@ -630,18 +630,11 @@ static int nftnl_flowtable_cmd_snprintf(char *buf, size_t remain, { int ret, offset = 0; - switch (type) { - case NFTNL_OUTPUT_DEFAULT: - ret = nftnl_flowtable_snprintf_default(buf + offset, remain, c); - SNPRINTF_BUFFER_SIZE(ret, remain, offset); - break; - case NFTNL_OUTPUT_XML: - case NFTNL_OUTPUT_JSON: - break; - default: + if (type != NFTNL_OUTPUT_DEFAULT) return -1; - } + ret = nftnl_flowtable_snprintf_default(buf + offset, remain, c); + SNPRINTF_BUFFER_SIZE(ret, remain, offset); return offset; } diff --git a/src/gen.c b/src/gen.c index 362132e..88efbaa 100644 --- a/src/gen.c +++ b/src/gen.c @@ -162,15 +162,11 @@ static int nftnl_gen_cmd_snprintf(char *buf, size_t remain, { int ret, offset = 0; - switch(type) { - case NFTNL_OUTPUT_DEFAULT: - ret = snprintf(buf, remain, "ruleset generation ID %u", gen->id); - SNPRINTF_BUFFER_SIZE(ret, remain, offset); - break; - default: + if (type != NFTNL_OUTPUT_DEFAULT) return -1; - } + ret = snprintf(buf, remain, "ruleset generation ID %u", gen->id); + SNPRINTF_BUFFER_SIZE(ret, remain, offset); return offset; } diff --git a/src/object.c b/src/object.c index 4eb4d35..46e208b 100644 --- a/src/object.c +++ b/src/object.c @@ -411,18 +411,11 @@ static int nftnl_obj_cmd_snprintf(char *buf, size_t remain, { int ret, offset = 0; - switch (type) { - case NFTNL_OUTPUT_DEFAULT: - ret = nftnl_obj_snprintf_dflt(buf + offset, remain, obj, type, - flags); - break; - case NFTNL_OUTPUT_JSON: - case NFTNL_OUTPUT_XML: - default: + if (type != NFTNL_OUTPUT_DEFAULT) return -1; - } - SNPRINTF_BUFFER_SIZE(ret, remain, offset); + ret = nftnl_obj_snprintf_dflt(buf + offset, remain, obj, type, flags); + SNPRINTF_BUFFER_SIZE(ret, remain, offset); return offset; } diff --git a/src/rule.c b/src/rule.c index 439e451..0bb1c2a 100644 --- a/src/rule.c +++ b/src/rule.c @@ -644,18 +644,12 @@ static int nftnl_rule_cmd_snprintf(char *buf, size_t remain, inner_flags &= ~NFTNL_OF_EVENT_ANY; - switch(type) { - case NFTNL_OUTPUT_DEFAULT: - ret = nftnl_rule_snprintf_default(buf + offset, remain, r, type, - inner_flags); - SNPRINTF_BUFFER_SIZE(ret, remain, offset); - break; - case NFTNL_OUTPUT_JSON: - case NFTNL_OUTPUT_XML: - default: + if (type != NFTNL_OUTPUT_DEFAULT) return -1; - } + ret = nftnl_rule_snprintf_default(buf + offset, remain, r, type, + inner_flags); + SNPRINTF_BUFFER_SIZE(ret, remain, offset); return offset; } diff --git a/src/ruleset.c b/src/ruleset.c index f904aa4..4788cc0 100644 --- a/src/ruleset.c +++ b/src/ruleset.c @@ -522,21 +522,6 @@ nftnl_ruleset_do_snprintf(char *buf, size_t remain, return offset; } -static int nftnl_ruleset_cmd_snprintf(char *buf, size_t size, - const struct nftnl_ruleset *r, uint32_t cmd, - uint32_t type, uint32_t flags) -{ - switch (type) { - case NFTNL_OUTPUT_DEFAULT: - case NFTNL_OUTPUT_JSON: - return nftnl_ruleset_do_snprintf(buf, size, r, cmd, type, flags); - case NFTNL_OUTPUT_XML: - default: - errno = EOPNOTSUPP; - return -1; - } -} - EXPORT_SYMBOL(nftnl_ruleset_snprintf); int nftnl_ruleset_snprintf(char *buf, size_t size, const struct nftnl_ruleset *r, uint32_t type, uint32_t flags) @@ -544,17 +529,12 @@ int nftnl_ruleset_snprintf(char *buf, size_t size, const struct nftnl_ruleset *r if (size) buf[0] = '\0'; - switch (type) { - case NFTNL_OUTPUT_DEFAULT: - case NFTNL_OUTPUT_JSON: - return nftnl_ruleset_cmd_snprintf(buf, size, r, - nftnl_flag2cmd(flags), type, - flags); - case NFTNL_OUTPUT_XML: - default: + if (type != NFTNL_OUTPUT_DEFAULT) { errno = EOPNOTSUPP; return -1; } + return nftnl_ruleset_do_snprintf(buf, size, r, nftnl_flag2cmd(flags), + type, flags); } static int nftnl_ruleset_fprintf_tables(FILE *fp, const struct nftnl_ruleset *rs, diff --git a/src/set.c b/src/set.c index 021543e..1c29dd2 100644 --- a/src/set.c +++ b/src/set.c @@ -843,22 +843,15 @@ static int nftnl_set_cmd_snprintf(char *buf, size_t remain, uint32_t inner_flags = flags; int ret, offset = 0; - if (type == NFTNL_OUTPUT_XML) - return 0; + if (type != NFTNL_OUTPUT_DEFAULT) + return -1; /* prevent set_elems to print as events */ inner_flags &= ~NFTNL_OF_EVENT_ANY; - switch(type) { - case NFTNL_OUTPUT_DEFAULT: - ret = nftnl_set_snprintf_default(buf + offset, remain, s, type, - inner_flags); - SNPRINTF_BUFFER_SIZE(ret, remain, offset); - break; - default: - return -1; - } - + ret = nftnl_set_snprintf_default(buf + offset, remain, s, type, + inner_flags); + SNPRINTF_BUFFER_SIZE(ret, remain, offset); return offset; } diff --git a/src/set_elem.c b/src/set_elem.c index a1764e2..90632a2 100644 --- a/src/set_elem.c +++ b/src/set_elem.c @@ -757,19 +757,12 @@ static int nftnl_set_elem_cmd_snprintf(char *buf, size_t remain, { int ret, offset = 0; - switch(type) { - case NFTNL_OUTPUT_DEFAULT: - ret = nftnl_set_elem_snprintf_default(buf + offset, remain, e, - NFT_DATA_VALUE); - SNPRINTF_BUFFER_SIZE(ret, remain, offset); - break; - case NFTNL_OUTPUT_XML: - case NFTNL_OUTPUT_JSON: - break; - default: + if (type != NFTNL_OUTPUT_DEFAULT) return -1; - } + ret = nftnl_set_elem_snprintf_default(buf + offset, remain, e, + NFT_DATA_VALUE); + SNPRINTF_BUFFER_SIZE(ret, remain, offset); return offset; } diff --git a/src/table.c b/src/table.c index 5835788..59e7053 100644 --- a/src/table.c +++ b/src/table.c @@ -373,17 +373,11 @@ static int nftnl_table_cmd_snprintf(char *buf, size_t remain, { int ret, offset = 0; - switch (type) { - case NFTNL_OUTPUT_DEFAULT: - ret = nftnl_table_snprintf_default(buf + offset, remain, t); - SNPRINTF_BUFFER_SIZE(ret, remain, offset); - break; - case NFTNL_OUTPUT_XML: - case NFTNL_OUTPUT_JSON: - default: + if (type != NFTNL_OUTPUT_DEFAULT) return -1; - } + ret = nftnl_table_snprintf_default(buf + offset, remain, t); + SNPRINTF_BUFFER_SIZE(ret, remain, offset); return offset; } -- cgit v1.2.3