Skip to contentSkip to conversation

Fix for warnings when compiling for 32bit arch

PATCH In review Phil Sutter 2 participants 3 messages Oct 2 – Oct 6 netfilter

lore
Share & tools
lore

Phil Sutter

author reviewer
#0 lore raw

PATCH v2 1/1 Fix for warnings when compiling for 32bit arch

This eliminates several, probably harmless warnings when e.g. compiling with '-m32' for x86_64:

- Printing code assumes NFT_MAX_EXPR_LEN_BITS is unsigned long, when on 32bit it is unsigned int - cast to uint32_t and print using '%d'.

- Pointers are unsigned long, casting to uint64_t yields a warning on 32bit due to the different size

- Bison tokens of type 'val' are uint64_t, print their values using PRIu64 macro

- On 32bit, UINT32_MAX is of type unsigned int - cast to off_t when comparing against state->indesc->line_offset to avoid different signedness warning

---
Changes since v1:
- Prefer 32bit type for NFT_MAX_EXPR_LEN_B{IT,YTE}S

---
 include/expression.h    | 2 +-
 src/evaluate.c          | 4 ++--
 src/netlink_linearize.c | 4 ++--
 src/parser_bison.y      | 2 +-
 src/parser_json.c       | 4 ++--
 src/scanner.l           | 2 +-
 6 files changed, 9 insertions(+), 9 deletions(-)
include/expression.h +1 -1
index e6a05603552b5..844f16b8a04b0 100644
@@ -11,7 +11,7 @@
 #include <json.h>
 #include <libnftnl/udata.h>
 
-#define NFT_MAX_EXPR_LEN_BYTES (NFT_REG32_COUNT * sizeof(uint32_t))
+#define NFT_MAX_EXPR_LEN_BYTES ((uint32_t)(NFT_REG32_COUNT * sizeof(uint32_t)))
 #define NFT_MAX_EXPR_LEN_BITS  (NFT_MAX_EXPR_LEN_BYTES * BITS_PER_BYTE)
 #define NFT_MAX_EXPR_RECURSION 16
 
src/evaluate.c +2 -2
index 4f5f2694e7ed4..01b4afbbb10d8 100644
@@ -1828,7 +1828,7 @@ static int expr_evaluate_concat(struct eval_ctx *ctx, struct expr **expr)
 		ctx->inner_desc = NULL;
 
 		if (size > NFT_MAX_EXPR_LEN_BITS)
-			return expr_error(ctx->msgs, i, "Concatenation of size %u exceeds maximum size of %lu",
+			return expr_error(ctx->msgs, i, "Concatenation of size %u exceeds maximum size of %u",
 					  size, NFT_MAX_EXPR_LEN_BITS);
 	}
 
@@ -5191,7 +5191,7 @@ static int set_expr_evaluate_concat(struct eval_ctx *ctx, struct expr **expr)
 		size += netlink_padded_len(i->len);
 
 		if (size > NFT_MAX_EXPR_LEN_BITS)
-			return expr_error(ctx->msgs, i, "Concatenation of size %u exceeds maximum size of %lu",
+			return expr_error(ctx->msgs, i, "Concatenation of size %u exceeds maximum size of %u",
 					  size, NFT_MAX_EXPR_LEN_BITS);
 	}
 
src/netlink_linearize.c +2 -2
index dfa841c6a407f..8de507059fe59 100644
@@ -31,7 +31,7 @@ struct nft_expr_loc *nft_expr_loc_find(const struct nftnl_expr *nle,
 	struct nft_expr_loc *eloc;
 	uint32_t hash;
 
-	hash = (uint64_t)nle % NFT_EXPR_LOC_HSIZE;
+	hash = (unsigned long)nle % NFT_EXPR_LOC_HSIZE;
 	list_for_each_entry(eloc, &ctx->expr_loc_htable[hash], hlist) {
 		if (eloc->nle == nle)
 			return eloc;
@@ -50,7 +50,7 @@ static void nft_expr_loc_add(const struct nftnl_expr *nle,
 	eloc = xmalloc(sizeof(*eloc));
 	eloc->nle = nle;
 	eloc->loc = loc;
-	hash = (uint64_t)nle % NFT_EXPR_LOC_HSIZE;
+	hash = (unsigned long)nle % NFT_EXPR_LOC_HSIZE;
 	list_add_tail(&eloc->hlist, &ctx->expr_loc_htable[hash]);
 }
 
src/parser_bison.y +1 -1
index 6e1c1cc205331..bf2a136796619 100644
@@ -5887,7 +5887,7 @@ payload_expr		:	payload_raw_expr
 payload_raw_len		:	NUM
 			{
 				if ($1 > NFT_MAX_EXPR_LEN_BITS) {
-					erec_queue(error(&@1, "raw payload length %lu exceeds upper limit of %lu",
+					erec_queue(error(&@1, "raw payload length %" PRIu64 " exceeds upper limit of %u",
 							 $1, NFT_MAX_EXPR_LEN_BITS),
 						 state->msgs);
 					YYERROR;
src/parser_json.c +2 -2
index 0522b2cd80b96..14259ea39777b 100644
@@ -673,7 +673,7 @@ static struct expr *json_parse_payload_expr(struct json_ctx *ctx,
 		}
 
 		if (len <= 0 || len > (int)NFT_MAX_EXPR_LEN_BITS) {
-			json_error(ctx, "Payload length must be between 0 and %lu, got %d",
+			json_error(ctx, "Payload length must be between 0 and %u, got %d",
 				   NFT_MAX_EXPR_LEN_BITS, len);
 			return NULL;
 		}
@@ -750,7 +750,7 @@ static struct expr *json_parse_tcp_option_expr(struct json_ctx *ctx,
 			return NULL;
 
 		if (len < 0 || len > (int)NFT_MAX_EXPR_LEN_BITS) {
-			json_error(ctx, "option length must be between 0 and %lu, got %d",
+			json_error(ctx, "option length must be between 0 and %u, got %d",
 				   NFT_MAX_EXPR_LEN_BITS, len);
 			return NULL;
 		}
src/scanner.l +1 -1
index 61fcc4fa3bad3..37729ebc3cc55 100644
@@ -91,7 +91,7 @@ static void update_offset(struct parser_state *state, struct location *loc,
 	uint32_t line_offset;
 
 	state->indesc->token_offset	+= len;
-	if (state->indesc->line_offset > UINT32_MAX)
+	if (state->indesc->line_offset > (off_t)UINT32_MAX)
 		line_offset = UINT32_MAX;
 	else
 		line_offset = state->indesc->line_offset;
Signature · 1 line
2.54.0
maintainer Tested-by on v2 1/1
#1 lore raw
Quoted 9 lines · Phil Sutter

This eliminates several, probably harmless warnings when e.g. compiling with '-m32' for x86_64:

- Printing code assumes NFT_MAX_EXPR_LEN_BITS is unsigned long, when on 32bit it is unsigned int - cast to uint32_t and print using '%d'.

- Pointers are unsigned long, casting to uint64_t yields a warning on 32bit due to the different size

Quoted 8 lines

- Bison tokens of type 'val' are uint64_t, print their values using PRIu64 macro

- On 32bit, UINT32_MAX is of type unsigned int - cast to off_t when comparing against state->indesc->line_offset to avoid different signedness warning

Thanks Phil.

Quoted 120 lines

---
Changes since v1:
- Prefer 32bit type for NFT_MAX_EXPR_LEN_B{IT,YTE}S

---
 include/expression.h    | 2 +-
 src/evaluate.c          | 4 ++--
 src/netlink_linearize.c | 4 ++--
 src/parser_bison.y      | 2 +-
 src/parser_json.c       | 4 ++--
 src/scanner.l           | 2 +-
 6 files changed, 9 insertions(+), 9 deletions(-)
include/expression.h +1 -1
index e6a05603552b5..844f16b8a04b0 100644
@@ -11,7 +11,7 @@
 #include <json.h>
 #include <libnftnl/udata.h>
 
-#define NFT_MAX_EXPR_LEN_BYTES (NFT_REG32_COUNT * sizeof(uint32_t))
+#define NFT_MAX_EXPR_LEN_BYTES ((uint32_t)(NFT_REG32_COUNT * sizeof(uint32_t)))
 #define NFT_MAX_EXPR_LEN_BITS  (NFT_MAX_EXPR_LEN_BYTES * BITS_PER_BYTE)
 #define NFT_MAX_EXPR_RECURSION 16
 
src/evaluate.c +2 -2
index 4f5f2694e7ed4..01b4afbbb10d8 100644
@@ -1828,7 +1828,7 @@ static int expr_evaluate_concat(struct eval_ctx *ctx, struct expr **expr)
 		ctx->inner_desc = NULL;
 
 		if (size > NFT_MAX_EXPR_LEN_BITS)
-			return expr_error(ctx->msgs, i, "Concatenation of size %u exceeds maximum size of %lu",
+			return expr_error(ctx->msgs, i, "Concatenation of size %u exceeds maximum size of %u",
 					  size, NFT_MAX_EXPR_LEN_BITS);
 	}
 
@@ -5191,7 +5191,7 @@ static int set_expr_evaluate_concat(struct eval_ctx *ctx, struct expr **expr)
 		size += netlink_padded_len(i->len);
 
 		if (size > NFT_MAX_EXPR_LEN_BITS)
-			return expr_error(ctx->msgs, i, "Concatenation of size %u exceeds maximum size of %lu",
+			return expr_error(ctx->msgs, i, "Concatenation of size %u exceeds maximum size of %u",
 					  size, NFT_MAX_EXPR_LEN_BITS);
 	}
 
src/netlink_linearize.c +2 -2
index dfa841c6a407f..8de507059fe59 100644
@@ -31,7 +31,7 @@ struct nft_expr_loc *nft_expr_loc_find(const struct nftnl_expr *nle,
 	struct nft_expr_loc *eloc;
 	uint32_t hash;
 
-	hash = (uint64_t)nle % NFT_EXPR_LOC_HSIZE;
+	hash = (unsigned long)nle % NFT_EXPR_LOC_HSIZE;
 	list_for_each_entry(eloc, &ctx->expr_loc_htable[hash], hlist) {
 		if (eloc->nle == nle)
 			return eloc;
@@ -50,7 +50,7 @@ static void nft_expr_loc_add(const struct nftnl_expr *nle,
 	eloc = xmalloc(sizeof(*eloc));
 	eloc->nle = nle;
 	eloc->loc = loc;
-	hash = (uint64_t)nle % NFT_EXPR_LOC_HSIZE;
+	hash = (unsigned long)nle % NFT_EXPR_LOC_HSIZE;
 	list_add_tail(&eloc->hlist, &ctx->expr_loc_htable[hash]);
 }
 
src/parser_bison.y +1 -1
index 6e1c1cc205331..bf2a136796619 100644
@@ -5887,7 +5887,7 @@ payload_expr		:	payload_raw_expr
 payload_raw_len		:	NUM
 			{
 				if ($1 > NFT_MAX_EXPR_LEN_BITS) {
-					erec_queue(error(&@1, "raw payload length %lu exceeds upper limit of %lu",
+					erec_queue(error(&@1, "raw payload length %" PRIu64 " exceeds upper limit of %u",
 							 $1, NFT_MAX_EXPR_LEN_BITS),
 						 state->msgs);
 					YYERROR;
src/parser_json.c +2 -2
index 0522b2cd80b96..14259ea39777b 100644
@@ -673,7 +673,7 @@ static struct expr *json_parse_payload_expr(struct json_ctx *ctx,
 		}
 
 		if (len <= 0 || len > (int)NFT_MAX_EXPR_LEN_BITS) {
-			json_error(ctx, "Payload length must be between 0 and %lu, got %d",
+			json_error(ctx, "Payload length must be between 0 and %u, got %d",
 				   NFT_MAX_EXPR_LEN_BITS, len);
 			return NULL;
 		}
@@ -750,7 +750,7 @@ static struct expr *json_parse_tcp_option_expr(struct json_ctx *ctx,
 			return NULL;
 
 		if (len < 0 || len > (int)NFT_MAX_EXPR_LEN_BITS) {
-			json_error(ctx, "option length must be between 0 and %lu, got %d",
+			json_error(ctx, "option length must be between 0 and %u, got %d",
 				   NFT_MAX_EXPR_LEN_BITS, len);
 			return NULL;
 		}
src/scanner.l +1 -1
index 61fcc4fa3bad3..37729ebc3cc55 100644
@@ -91,7 +91,7 @@ static void update_offset(struct parser_state *state, struct location *loc,
 	uint32_t line_offset;
 
 	state->indesc->token_offset	+= len;
-	if (state->indesc->line_offset > UINT32_MAX)
+	if (state->indesc->line_offset > (off_t)UINT32_MAX)
 		line_offset = UINT32_MAX;
 	else
 		line_offset = state->indesc->line_offset;
Signature · 1 line
2.54.0
author reviewer
#2 lore raw
Quoted 13 lines · Pablo Neira Ayuso
Quoted 12 lines · Phil Sutter

This eliminates several, probably harmless warnings when e.g. compiling with '-m32' for x86_64:

- Printing code assumes NFT_MAX_EXPR_LEN_BITS is unsigned long, when on 32bit it is unsigned int - cast to uint32_t and print using '%d'.

- Pointers are unsigned long, casting to uint64_t yields a warning on 32bit due to the different size

- Bison tokens of type 'val' are uint64_t, print their values using PRIu64 macro

Quoted 7 lines
Quoted 5 lines

- On 32bit, UINT32_MAX is of type unsigned int - cast to off_t when comparing against state->indesc->line_offset to avoid different signedness warning

Patch applied.