diff mbox series

[nft,8/8] datatype: suppress "-Wformat-nonliteral" warning in integer_type_print()

Message ID 20230828144441.3303222-9-thaller@redhat.com
State Changes Requested
Delegated to: Pablo Neira
Headers show
Series fix compiler warnings with clang | expand

Commit Message

Thomas Haller Aug. 28, 2023, 2:43 p.m. UTC
datatype.c:455:22: error: format string is not a string literal [-Werror,-Wformat-nonliteral]
            nft_gmp_print(octx, fmt, expr->value);
                                ^~~

Signed-off-by: Thomas Haller <thaller@redhat.com>
---
 src/datatype.c | 2 ++
 1 file changed, 2 insertions(+)

Comments

Pablo Neira Ayuso Aug. 28, 2023, 3:08 p.m. UTC | #1
On Mon, Aug 28, 2023 at 04:43:58PM +0200, Thomas Haller wrote:
>     datatype.c:455:22: error: format string is not a string literal [-Werror,-Wformat-nonliteral]
>             nft_gmp_print(octx, fmt, expr->value);
>                                 ^~~
> 
> Signed-off-by: Thomas Haller <thaller@redhat.com>
> ---
>  src/datatype.c | 2 ++
>  1 file changed, 2 insertions(+)
> 
> diff --git a/src/datatype.c b/src/datatype.c
> index 4d0e44eeb500..12fe7141709d 100644
> --- a/src/datatype.c
> +++ b/src/datatype.c
> @@ -452,7 +452,9 @@ static void integer_type_print(const struct expr *expr, struct output_ctx *octx)
>  		}
>  	} while ((dtype = dtype->basetype));
>  
> +	_NFT_PRAGMA_WARNING_DISABLE("-Wformat-nonliteral")

Maybe simply -Wno-format-nonliteral turn off in Clang so there is no
need for this PRAGMA in order to simplify things.

>  	nft_gmp_print(octx, fmt, expr->value);
> +	_NFT_PRAGMA_WARNING_REENABLE
>  }
>  
>  static struct error_record *integer_type_parse(struct parse_ctx *ctx,
> -- 
> 2.41.0
>
Thomas Haller Aug. 28, 2023, 3:33 p.m. UTC | #2
On Mon, 2023-08-28 at 17:08 +0200, Pablo Neira Ayuso wrote:
> On Mon, Aug 28, 2023 at 04:43:58PM +0200, Thomas Haller wrote:
> > 
> > +       _NFT_PRAGMA_WARNING_DISABLE("-Wformat-nonliteral")
> 
> Maybe simply -Wno-format-nonliteral turn off in Clang so there is no
> need for this PRAGMA in order to simplify things.

"-Wformat-nonliteral" seems a useful warning. I would rather not
disable it at a larger scale.

Gcc also supports "-Wformat-nonliteral" warning, but for some reason it
does not warn here (also not, when I pass "-Wformat=2"). I don't
understand why that is.


Thomas
Pablo Neira Ayuso Aug. 28, 2023, 3:54 p.m. UTC | #3
On Mon, Aug 28, 2023 at 05:33:01PM +0200, Thomas Haller wrote:
> On Mon, 2023-08-28 at 17:08 +0200, Pablo Neira Ayuso wrote:
> > On Mon, Aug 28, 2023 at 04:43:58PM +0200, Thomas Haller wrote:
> > > 
> > > +       _NFT_PRAGMA_WARNING_DISABLE("-Wformat-nonliteral")
> > 
> > Maybe simply -Wno-format-nonliteral turn off in Clang so there is no
> > need for this PRAGMA in order to simplify things.
> 
> "-Wformat-nonliteral" seems a useful warning. I would rather not
> disable it at a larger scale.
> 
> Gcc also supports "-Wformat-nonliteral" warning, but for some reason it
> does not warn here (also not, when I pass "-Wformat=2"). I don't
> understand why that is.

Can you see any other way to fix this clang compilation eror without
this pragma? What makes clang unhappy with this code?
Thomas Haller Aug. 28, 2023, 4:24 p.m. UTC | #4
On Mon, 2023-08-28 at 17:54 +0200, Pablo Neira Ayuso wrote:
> On Mon, Aug 28, 2023 at 05:33:01PM +0200, Thomas Haller wrote:
> > On Mon, 2023-08-28 at 17:08 +0200, Pablo Neira Ayuso wrote:
> > > On Mon, Aug 28, 2023 at 04:43:58PM +0200, Thomas Haller wrote:
> > > > 
> > > > +       _NFT_PRAGMA_WARNING_DISABLE("-Wformat-nonliteral")
> > > 
> > > Maybe simply -Wno-format-nonliteral turn off in Clang so there is
> > > no
> > > need for this PRAGMA in order to simplify things.
> > 
> > "-Wformat-nonliteral" seems a useful warning. I would rather not
> > disable it at a larger scale.
> > 
> > Gcc also supports "-Wformat-nonliteral" warning, but for some
> > reason it
> > does not warn here (also not, when I pass "-Wformat=2"). I don't
> > understand why that is.
> 
> Can you see any other way to fix this clang compilation eror without
> this pragma? What makes clang unhappy with this code?
> 


the compiler warning happens, because the argument isn't a string
literal. But the deeper cause is something else entirely (and the
patches wrong).


Those "%Zu" format strings aren't intended for libc's printf. Instead,
it's intended for gmp_vfprintf(), which implements a different meaning
for "Z".

The patch "src: use "%zx" format instead of "%Zx"" should be dropped.
Likewise, "datatype: suppress "-Wformat-nonliteral" warning in
integer_type_print()".


Instead, the format attribute from

int nft_gmp_print(struct output_ctx *octx, const char *fmt, ...)
       __attribute__((format(printf, 2, 0)));

needs to be dropped. This is not the "printf" format.


Will do in v2.


Thomas
diff mbox series

Patch

diff --git a/src/datatype.c b/src/datatype.c
index 4d0e44eeb500..12fe7141709d 100644
--- a/src/datatype.c
+++ b/src/datatype.c
@@ -452,7 +452,9 @@  static void integer_type_print(const struct expr *expr, struct output_ctx *octx)
 		}
 	} while ((dtype = dtype->basetype));
 
+	_NFT_PRAGMA_WARNING_DISABLE("-Wformat-nonliteral")
 	nft_gmp_print(octx, fmt, expr->value);
+	_NFT_PRAGMA_WARNING_REENABLE
 }
 
 static struct error_record *integer_type_parse(struct parse_ctx *ctx,