Skip to contentSkip to conversation

fix compiler warnings with clang and "-Wextra"

PATCH In review Thomas Haller 2 participants 12 messages Aug 29, 2023 – Aug 30, 2023 netfilter

lore
Share & tools
lore
#0 lore raw

cover fix compiler warnings with clang and "-Wextra"

More fixes (workarounds) for compiler warnings. Mostly by enabling "-Wextra" with gcc.

Thomas Haller (5):
rule: fix "const static" declaration
utils: call abort() after BUG() macro
src: silence "implicit-fallthrough" warnings
xt: avoid "-Wmissing-field-initializers" for "original_opts"
datatype: check against negative "type" argument in datatype_lookup()

 include/utils.h | 3 ++-
 src/datatype.c  | 2 +-
 src/optimize.c  | 1 +
 src/rule.c      | 4 ++--
 src/segtree.c   | 5 ++---
 src/xt.c        | 2 +-
 6 files changed, 9 insertions(+), 8 deletions(-)
Signature · 1 line
2.41.0
#1 lore raw

PATCH 1/5 rule: fix "const static" declaration

Gcc warns against this with "-Wextra":

    src/rule.c:869:1: error: static is not at beginning of declaration [-Werror=old-style-declaration]
      869 | const static struct prio_tag std_prios[] = {
          | ^~~~~
    src/rule.c:878:1: error: static is not at beginning of declaration [-Werror=old-style-declaration]
      878 | const static struct prio_tag bridge_std_prios[] = {
          | ^~~~~
---
 src/rule.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
src/rule.c +2 -2
index 8ea606e146b2..bbea05d5b288 100644
@@ -866,7 +866,7 @@ struct prio_tag {
 	const char *str;
 };
 
-const static struct prio_tag std_prios[] = {
+static const struct prio_tag std_prios[] = {
 	{ NF_IP_PRI_RAW,      "raw" },
 	{ NF_IP_PRI_MANGLE,   "mangle" },
 	{ NF_IP_PRI_NAT_DST,  "dstnat" },
@@ -875,7 +875,7 @@ const static struct prio_tag std_prios[] = {
 	{ NF_IP_PRI_NAT_SRC,  "srcnat" },
 };
 
-const static struct prio_tag bridge_std_prios[] = {
+static const struct prio_tag bridge_std_prios[] = {
 	{ NF_BR_PRI_NAT_DST_BRIDGED,  "dstnat" },
 	{ NF_BR_PRI_FILTER_BRIDGED,   "filter" },
 	{ NF_BR_PRI_NAT_DST_OTHER,    "out" },
Signature · 1 line
2.41.0
#2 lore raw

PATCH 2/5 utils: call abort() after BUG() macro

In reply to Thomas Haller

Otherwise, we get spurious warnings. The compiler should be aware that there is no return from BUG(). Call abort() there, which is marked as __attribute__ ((__noreturn__)).

    In file included from ./include/nftables.h:6,
                     from ./include/rule.h:4,
                     from src/payload.c:26:
    src/payload.c: In function 'icmp_dep_to_type':
    ./include/utils.h:39:34: error: this statement may fall through [-Werror=implicit-fallthrough=]
       39 | #define BUG(fmt, arg...)        ({ fprintf(stderr, "BUG: " fmt, ##arg); assert(0); })
          |                                 ~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    src/payload.c:791:17: note: in expansion of macro 'BUG'
      791 |                 BUG("Invalid map for simple dependency");
          |                 ^~~
    src/payload.c:792:9: note: here
      792 |         case PROTO_ICMP_ECHO: return ICMP_ECHO;
          |         ^~~~
---
 include/utils.h | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
include/utils.h +2 -1
index efc8dec013a1..5b8b181c1e99 100644
@@ -3,6 +3,7 @@
 
 #include <asm/byteorder.h>
 #include <stdarg.h>
+#include <stdlib.h>
 #include <stdio.h>
 #include <unistd.h>
 #include <assert.h>
@@ -36,7 +37,7 @@
 #define __must_check		__attribute__((warn_unused_result))
 #define __noreturn		__attribute__((__noreturn__))
 
-#define BUG(fmt, arg...)	({ fprintf(stderr, "BUG: " fmt, ##arg); assert(0); })
+#define BUG(fmt, arg...)	({ fprintf(stderr, "BUG: " fmt, ##arg); assert(0); abort(); })
 
 #define BUILD_BUG_ON(condition)	((void)sizeof(char[1 - 2*!!(condition)]))
 #define BUILD_BUG_ON_ZERO(e)	(sizeof(char[1 - 2 * !!(e)]) - 1)
Signature · 1 line
2.41.0
#3 lore raw

PATCH 3/5 src: silence "implicit-fallthrough" warnings

In reply to Thomas Haller

Gcc with "-Wextra" warns:

CC segtree.lo
segtree.c: In function 'get_set_interval_find':
segtree.c:129:28: error: this statement may fall through [-Werror=implicit-fallthrough=]

    129 |                         if (expr_basetype(i->key)->type != TYPE_STRING)
        |                            ^
  segtree.c:134:17: note: here
    134 |                 case EXPR_PREFIX:
        |                 ^~~~

CC optimize.lo
optimize.c: In function 'rule_collect_stmts':
optimize.c:396:28: error: this statement may fall through [-Werror=implicit-fallthrough=]

    396 |                         if (stmt->expr->left->etype == EXPR_CONCAT) {
        |                            ^
  optimize.c:400:17: note: here
    400 |                 case STMT_VERDICT:
        |                 ^~~~
---
 src/optimize.c | 1 +
 src/segtree.c  | 5 ++---
 2 files changed, 3 insertions(+), 3 deletions(-)
src/optimize.c +1 -0
index 0b99b6726115..9c1704831693 100644
@@ -397,6 +397,7 @@ static int rule_collect_stmts(struct optimize_ctx *ctx, struct rule *rule)
 				clone->ops = &unsupported_stmt_ops;
 				break;
 			}
+			/* fall-through */
 		case STMT_VERDICT:
 			clone->expr = expr_get(stmt->expr);
 			break;
src/segtree.c +2 -3
index a265a0b30d64..bf207402c945 100644
@@ -128,9 +128,8 @@ static struct expr *get_set_interval_find(const struct set *cache_set,
 		case EXPR_VALUE:
 			if (expr_basetype(i->key)->type != TYPE_STRING)
 				break;
-			/* string type, check if its a range (wildcard), so
-			 * fall through.
-			 */
+			/* string type, check if its a range (wildcard). */
+			/* fall-through */
 		case EXPR_PREFIX:
 		case EXPR_RANGE:
 			range_expr_value_low(val, i);
Signature · 1 line
2.41.0
#4 lore raw

PATCH 4/5 xt: avoid "-Wmissing-field-initializers" for "original_opts"

In reply to Thomas Haller

Avoid this warning with clang:

      CC       src/xt.lo
    src/xt.c:353:9: error: missing field 'has_arg' initializer [-Werror,-Wmissing-field-initializers]
            { NULL },
                   ^

The warning seems not very useful, because it's well understood that specifying only some initializers leaves the remaining fields initialized with the default. However, as this warning is only hit once in the code base, it doesn't seem that we violate this style frequently. Hence, fix it instead of disabling the warning.

---
 src/xt.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
src/xt.c +1 -1
index df7140b4fa97..a217cc7b6bd0 100644
@@ -350,7 +350,7 @@ err:
 }
 
 static struct option original_opts[] = {
-	{ NULL },
+	{ },
 };
 
 static struct xtables_globals xt_nft_globals = {
Signature · 1 line
2.41.0
#5 lore raw

PATCH 5/5 datatype: check against negative "type" argument in datatype_lookup()

In reply to Thomas Haller

An enum can be either signed or unsigned (implementation defined).

datatype_lookup() checks for invalid type arguments. Also check, whether the argument is not negative (which, depending on the compiler it may never be).

---
 src/datatype.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
src/datatype.c +1 -1
index ba1192c83595..91735ff8b360 100644
@@ -87,7 +87,7 @@ const struct datatype *datatype_lookup(enum datatypes type)
 {
 	BUILD_BUG_ON(TYPE_MAX & ~TYPE_MASK);
 
-	if (type > TYPE_MAX)
+	if ((uintmax_t) type > TYPE_MAX)
 		return NULL;
 	return datatypes[type];
 }
Signature · 1 line
2.41.0
#6 lore raw

Re: [PATCH nft 5/5] datatype: check against negative "type" argument in datatype_lookup()

Quoted 16 lines · Thomas Haller

An enum can be either signed or unsigned (implementation defined).

datatype_lookup() checks for invalid type arguments. Also check, whether the argument is not negative (which, depending on the compiler it may never be).

---
 src/datatype.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
src/datatype.c +0 -0
index ba1192c83595..91735ff8b360 100644

src/datatype.c const struct datatype *datatype_lookup(enum datatypes type) · line 90

Quoted 5 lines
src/datatype.c +1 -1
@@ -87,7 +87,7 @@ const struct datatype *datatype_lookup(enum datatypes type)
 {
 	BUILD_BUG_ON(TYPE_MAX & ~TYPE_MASK);
 
-	if (type > TYPE_MAX)
+	if ((uintmax_t) type > TYPE_MAX)
            uint32_t ?
Quoted 6 lines
src/datatype.c +0 -0
 		return NULL;
 	return datatypes[type];
 }
Signature · 1 line
2.41.0
#7 lore raw

Re: [PATCH nft 5/5] datatype: check against negative "type" argument in datatype_lookup()

Quoted 20 lines · Pablo Neira Ayuso
Quoted 19 lines · Thomas Haller

An enum can be either signed or unsigned (implementation defined).

datatype_lookup() checks for invalid type arguments. Also check, whether the argument is not negative (which, depending on the compiler it may never be).

---
 src/datatype.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
src/datatype.c +0 -0
index ba1192c83595..91735ff8b360 100644
@@ -87,7 +87,7 @@ const struct datatype *datatype_lookup(enum datatypes type)
 {
 	BUILD_BUG_ON(TYPE_MAX & ~TYPE_MASK);
 
Quoted 4 lines

src/datatype.c const struct datatype *datatype_lookup(enum datatypes type) · line 90

Quoted 2 lines
src/datatype.c +1 -1
-	if (type > TYPE_MAX)
+	if ((uintmax_t) type > TYPE_MAX)
            uint32_t ?

Another question: What warning does clang print on this one? Description does not specify.

Quoted 6 lines
Quoted 6 lines
src/datatype.c +0 -0
 		return NULL;
 	return datatypes[type];
 }
Signature · 1 line
2.41.0
#8 lore raw

Re: [PATCH nft 5/5] datatype: check against negative "type" argument in datatype_lookup()

Quoted 16 lines · Pablo Neira Ayuso
Quoted 15 lines · Pablo Neira Ayuso
Quoted 14 lines · Thomas Haller

An enum can be either signed or unsigned (implementation defined).

datatype_lookup() checks for invalid type arguments. Also check, whether
the argument is not negative (which, depending on the compiler it may
never be).

---
 src/datatype.c | 2 +-

 1 file changed, 1 insertion(+), 1 deletion(-)
Quoted 13 lines
Quoted 5 lines
Quoted 5 lines
src/datatype.c +0 -0
index ba1192c83595..91735ff8b360 100644
@@ -87,7 +87,7 @@ const struct datatype *datatype_lookup(enum
Quoted 8 lines
Quoted 6 lines
src/datatype.c +0 -0
 datatypes type)
  {
         BUILD_BUG_ON(TYPE_MAX & ~TYPE_MASK);


-       if (type > TYPE_MAX)
+       if ((uintmax_t) type > TYPE_MAX)

            uint32_t ?

The more straight forward way would be

    if (type < 0 || type > TYPE_MAX)

However, if the enum is unsigned, then the compiler might see that the condition is never true and warn against that. It does warn, if "type" were just an "unsigned int". I cannot actually reproduce a compiler warning with the enum (for now).

The size of the enum is most likely int/unsigned (or smaller, with "- fshort-enums" or packed). Is it on POSIX/Linux always guaranteed that an int is 32bit? I think not, but I cannot find an architecture where int is larger either. Also, if someone would add an enum value larger than the 32 bit range, then the behavior is compiler dependent, but most likely the enum type would be a 64 bit integer and "uint"/"uint32_t" would not be the right check.

All of this is highly theoretical. But "uintmax_t" avoids all those problems and makes fewer assumptions on what the enum actually is. Is there a hypothetical scenario where it wouldn't work correctly?

Quoted 3 lines

Another question: What warning does clang print on this one? Description does not specify.

this one isn't about a compiler warning. Sorry, I should not have included it in this set.

Thomas

#9 lore raw

Re: [PATCH nft 5/5] datatype: check against negative "type" argument in datatype_lookup()

Quoted 31 lines · Thomas Haller
Quoted 16 lines · Pablo Neira Ayuso
Quoted 15 lines · Pablo Neira Ayuso
Quoted 14 lines · Thomas Haller

An enum can be either signed or unsigned (implementation defined).

datatype_lookup() checks for invalid type arguments. Also check, whether
the argument is not negative (which, depending on the compiler it may
never be).

---
 src/datatype.c | 2 +-

 1 file changed, 1 insertion(+), 1 deletion(-)
Quoted 13 lines
Quoted 5 lines
Quoted 5 lines
src/datatype.c +0 -0
index ba1192c83595..91735ff8b360 100644
@@ -87,7 +87,7 @@ const struct datatype *datatype_lookup(enum
Quoted 8 lines
Quoted 6 lines
src/datatype.c +0 -0
 datatypes type)
  {
         BUILD_BUG_ON(TYPE_MAX & ~TYPE_MASK);


-       if (type > TYPE_MAX)
+       if ((uintmax_t) type > TYPE_MAX)

            uint32_t ?

Quoted 8 lines

The more straight forward way would be

    if (type < 0 || type > TYPE_MAX)

However, if the enum is unsigned, then the compiler might see that the condition is never true and warn against that. It does warn, if "type" were just an "unsigned int". I cannot actually reproduce a compiler warning with the enum (for now).

Then, better keep it back?

Quoted 7 lines

The size of the enum is most likely int/unsigned (or smaller, with "- fshort-enums" or packed). Is it on POSIX/Linux always guaranteed that an int is 32bit? I think not, but I cannot find an architecture where int is larger either. Also, if someone would add an enum value larger than the 32 bit range, then the behavior is compiler dependent, but most likely the enum type would be a 64 bit integer and "uint"/"uint32_t" would not be the right check.

I don't expect to ever have such a large number of types. Specifically because there are API restrictions that apply in this case.

Quoted 3 lines

All of this is highly theoretical. But "uintmax_t" avoids all those problems and makes fewer assumptions on what the enum actually is. Is there a hypothetical scenario where it wouldn't work correctly?

I was trying to figure out what this is fixing.

Quoted 5 lines
Quoted 2 lines

Another question: What warning does clang print on this one? Description does not specify.

this one isn't about a compiler warning. Sorry, I should not have included it in this set.

This TYPE_MAX will not ever become very large to require 64-bits. With an implementation where enum is taken as signed, then this should be sufficient too:

if (type > TYPE_MAX)

If this is not fixing up anything right now, I would prefer to keep this back.

I'll take this series except this one.

Thanks.

#10 lore raw

Re: [PATCH nft 5/5] datatype: check against negative "type" argument in datatype_lookup()

Quoted 71 lines · Pablo Neira Ayuso
Quoted 36 lines · Thomas Haller
Quoted 19 lines · Pablo Neira Ayuso
Quoted 17 lines · Pablo Neira Ayuso
Quoted 16 lines · Thomas Haller

An enum can be either signed or unsigned (implementation defined).

datatype_lookup() checks for invalid type arguments. Also check,
whether
the argument is not negative (which, depending on the compiler it
may
never be).

---
 src/datatype.c | 2 +-

 1 file changed, 1 insertion(+), 1 deletion(-)
Quoted 13 lines
Quoted 5 lines
Quoted 5 lines
src/datatype.c +0 -0
index ba1192c83595..91735ff8b360 100644
@@ -87,7 +87,7 @@ const struct datatype *datatype_lookup(enum
Quoted 8 lines
Quoted 6 lines
src/datatype.c +0 -0
 datatypes type)
  {
         BUILD_BUG_ON(TYPE_MAX & ~TYPE_MASK);


-       if (type > TYPE_MAX)
+       if ((uintmax_t) type > TYPE_MAX)

            uint32_t ?

The more straight forward way would be

Quoted 8 lines

    if (type < 0 || type > TYPE_MAX)

However, if the enum is unsigned, then the compiler might see that the
condition is never true and warn against that. It does warn, if "type"
were just an "unsigned int". I cannot actually reproduce a compiler warning with the enum (for now).

Then, better keep it back?

Quoted 11 lines

The size of the enum is most likely int/unsigned (or smaller, with "-
fshort-enums" or packed). Is it on POSIX/Linux always guaranteed that
an int is 32bit? I think not, but I cannot find an architecture where
int is larger either. Also, if someone would add an enum value larger
than the 32 bit range, then the behavior is compiler dependent, but most likely the enum type would be a 64 bit integer and "uint"/"uint32_t" would not be the right check.

I don't expect to ever have such a large number of types. Specifically
because there are API restrictions that apply in this case.

Quoted 4 lines

All of this is highly theoretical. But "uintmax_t" avoids all those problems and makes fewer assumptions on what the enum actually is. Is
there a hypothetical scenario where it wouldn't work correctly?

I was trying to figure out what this is fixing.

Quoted 7 lines
Quoted 5 lines
Quoted 2 lines

Another question: What warning does clang print on this one? Description does not specify.

this one isn't about a compiler warning. Sorry, I should not have included it in this set.

This TYPE_MAX will not ever become very large to require 64-bits.

TYPE_MAX is not relevant.

Quoted 8 lines

With an implementation where enum is taken as signed, then this should
be sufficient too:

     if (type > TYPE_MAX)

If this is not fixing up anything right now, I would prefer to keep this back.

I don't think it suffices. The following fail the assertion (or would access out of bounds).

include/datatype.h +2 -1
index 9ce7359cd340..7d3b6b20d27c 100644
@@ -98,7 +98,8 @@ enum datatypes {
     TYPE_TIME_HOUR,
     TYPE_TIME_DAY,
     TYPE_CGROUPV2,
-    __TYPE_MAX
+    __TYPE_MAX,
+    __TYPE_FORCE_SIGNED = -1,
 };
 #define TYPE_MAX        (__TYPE_MAX - 1)
 
src/datatype.c +0 -0
index ba1192c83595..1ff8a4a08551 100644
@@ -89,6 +89,7 @@ const struct datatype *datatype_lookup(enum datatypes

type)

if (type > TYPE_MAX)
return NULL;

+    assert(type != (enum datatypes) -1);
     return datatypes[type];

}

src/libnftables.c +2 -0
index 9c802ec95f27..7e60d1a18d39 100644
@@ -203,6 +203,8 @@ struct nft_ctx *nft_ctx_new(uint32_t flags)
 #endif
     }
 
+    datatype_lookup(-1);
+
     ctx = xzalloc(sizeof(struct nft_ctx));
     nft_init(ctx);
 

If you expect that "type" is always valid, then there is no need to check against >TYPE_MAX. If you expect that it might be invalid, it seems prudent to also check against negative values.

Thomas

#11 lore raw

Re: [PATCH nft 5/5] datatype: check against negative "type" argument in datatype_lookup()

On Wed, Aug 30, 2023 at 10:08:50AM +0200, Thomas Haller wrote:
[...]

Quoted 9 lines

I don't think it suffices. The following fail the assertion (or would access out of bounds).

include/datatype.h +0 -0
index 9ce7359cd340..7d3b6b20d27c 100644

include/datatype.h enum datatypes { · line 102

Quoted 6 lines
include/datatype.h +2 -1
@@ -98,7 +98,8 @@ enum datatypes {
     TYPE_TIME_HOUR,
     TYPE_TIME_DAY,
     TYPE_CGROUPV2,
-    __TYPE_MAX
+    __TYPE_MAX,
+    __TYPE_FORCE_SIGNED = -1,

I don't expect to ever have a negative defined here.

Quoted 40 lines
include/datatype.h +0 -0
 };
 #define TYPE_MAX        (__TYPE_MAX - 1)
 
src/datatype.c +1 -0
index ba1192c83595..1ff8a4a08551 100644
@@ -89,6 +89,7 @@ const struct datatype *datatype_lookup(enum datatypes
 type)
 
     if (type > TYPE_MAX)
          return NULL;
+    assert(type != (enum datatypes) -1);
     return datatypes[type];
 }
src/libnftables.c +2 -0
index 9c802ec95f27..7e60d1a18d39 100644
@@ -203,6 +203,8 @@ struct nft_ctx *nft_ctx_new(uint32_t flags)
 #endif
     }
 
+    datatype_lookup(-1);
+
     ctx = xzalloc(sizeof(struct nft_ctx));
     nft_init(ctx);
 

If you expect that "type" is always valid, then there is no need to check against >TYPE_MAX. If you expect that it might be invalid, it seems prudent to also check against negative values.

Thomas