Fix for warnings when compiling for 32bit arch
Phil Sutter
reviewerPATCH 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 - multiply by 1UL to force the implicit type
- 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
--- include/expression.h | 2 +- src/netlink_linearize.c | 4 ++-- src/parser_bison.y | 2 +- src/scanner.l | 2 +- 4 files changed, 5 insertions(+), 5 deletions(-)
include/expression.h +1 -1
@@ -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 (1UL * 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/netlink_linearize.c +2 -2
@@ -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
@@ -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 %lu", $1, NFT_MAX_EXPR_LEN_BITS), state->msgs); YYERROR;
src/scanner.l +1 -1
@@ -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
Pablo Neira Ayuso
maintainerHi Phil,
Quoted 29 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_BITSis unsigned long, when on 32bit it is unsigned int - multiply by 1UL to force the implicit type- Pointers are unsigned long, casting to
uint64_tyields 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_MAXis of type unsigned int - cast tooff_twhen comparing againststate->indesc->line_offsetto avoid different signedness warningSigned-off-by Phil Sutter <[email protected]>--- include/expression.h | 2 +- src/netlink_linearize.c | 4 ++-- src/parser_bison.y | 2 +- src/scanner.l | 2 +- 4 files changed, 5 insertions(+), 5 deletions(-)include/expression.h +0
-0
include/expression.h line 14
Quoted 5 lines
include/expression.h +1
-1@@ -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 (1UL * NFT_REG32_COUNT * sizeof(uint32_t))
I would suggest to stick to unsigned 32-bit for this.
Related, I checked that NFT_MAX_EXPR_LEN_BITS is used with uint32_t vars after quickly browsing code here.
Then, this is used to allocate a small buffer.
unsigned char data[NFT_MAX_EXPR_LEN_BYTES];
Thanks.
Quoted 54 lines
include/expression.h +0
-0#define NFT_MAX_EXPR_LEN_BITS (NFT_MAX_EXPR_LEN_BYTES * BITS_PER_BYTE)#define NFT_MAX_EXPR_RECURSION 16src/netlink_linearize.c +2
-2@@ -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@@ -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 %lu", $1, NFT_MAX_EXPR_LEN_BITS), state->msgs); YYERROR;src/scanner.l +1
-1@@ -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