diff mbox series

debug: Add printf attribute to logging function

Message ID 20180525031531.22785-1-joel@jms.id.au
State Accepted
Headers show
Series debug: Add printf attribute to logging function | expand

Commit Message

Joel Stanley May 25, 2018, 3:15 a.m. UTC
This allows the compiler to warn about mismatched types in printf
arguments.

Without this we don't get warnings from the logging macros when format
specifiers and arguments don't match up, and instead you get segfaults.

Signed-off-by: Joel Stanley <joel@jms.id.au>
---
 libpdbg/debug.h | 2 ++
 1 file changed, 2 insertions(+)

Comments

Alistair Popple June 1, 2018, 3:29 a.m. UTC | #1
Thanks Joel, it seems I get the following warnings/errors after applying this:

In file included from libpdbg/operations.h:21:0,
                 from libpdbg/cfam.c:24:
libpdbg/cfam.c: In function ‘opb_poll’:
libpdbg/debug.h:32:23: error: format ‘%llx’ expects argument of type ‘long long unsigned int’, but argument 5 has type ‘uint64_t {aka long unsigned int}’ [-Werror=format=]
  pdbg_log(PDBG_DEBUG, "%s[%d]: " x, __FUNCTION__, __LINE__, ##args)
                       ^
libpdbg/cfam.c:146:3: note: in expansion of macro ‘PR_DEBUG’
   PR_DEBUG("  STAT=0x%16llx...\n", sval);
   ^~~~~~~~
libpdbg/cfam.c: In function ‘p8_opb_read’:
libpdbg/debug.h:32:23: error: format ‘%llx’ expects argument of type ‘long long unsigned int’, but argument 5 has type ‘uint64_t {aka long unsigned int}’ [-Werror=format=]
  pdbg_log(PDBG_DEBUG, "%s[%d]: " x, __FUNCTION__, __LINE__, ##args)
                       ^
libpdbg/cfam.c:196:2: note: in expansion of macro ‘PR_DEBUG’
  PR_DEBUG("MFSI_OPB_READ: Writing 0x%16llx\n", opb_cmd);
  ^~~~~~~~
libpdbg/cfam.c: In function ‘p8_opb_write’:
libpdbg/debug.h:32:23: error: format ‘%llx’ expects argument of type ‘long long unsigned int’, but argument 5 has type ‘uint64_t {aka long unsigned int}’ [-Werror=format=]
  pdbg_log(PDBG_DEBUG, "%s[%d]: " x, __FUNCTION__, __LINE__, ##args)
                       ^
libpdbg/cfam.c:219:2: note: in expansion of macro ‘PR_DEBUG’
  PR_DEBUG("MFSI_OPB_WRITE: Writing 0x%16llx\n", opb_cmd);
  ^~~~~~~~
cc1: all warnings being treated as errors

But I have taken this patch and will roll the fixes for the above into it.

- Alistair

On Friday, 25 May 2018 12:45:31 PM AEST Joel Stanley wrote:
> This allows the compiler to warn about mismatched types in printf
> arguments.
> 
> Without this we don't get warnings from the logging macros when format
> specifiers and arguments don't match up, and instead you get segfaults.
> 
> Signed-off-by: Joel Stanley <joel@jms.id.au>
> ---
>  libpdbg/debug.h | 2 ++
>  1 file changed, 2 insertions(+)
> 
> diff --git a/libpdbg/debug.h b/libpdbg/debug.h
> index c318e3a566e6..5fc027a64ead 100644
> --- a/libpdbg/debug.h
> +++ b/libpdbg/debug.h
> @@ -18,6 +18,8 @@
>  
>  #include "libpdbg.h"
>  
> +void pdbg_log(int log_level, const char* fmt, ...) __attribute__((format (printf, 2, 3)));
> +
>  #define PR_ERROR(x, args...) \
>  	pdbg_log(PDBG_ERROR, x, ##args)
>  #define PR_WARNING(x, args...) \
>
Joel Stanley June 1, 2018, 3:43 a.m. UTC | #2
On 1 June 2018 at 12:59, Alistair Popple <alistair@popple.id.au> wrote:
> Thanks Joel, it seems I get the following warnings/errors after applying this:
>
> In file included from libpdbg/operations.h:21:0,
>                  from libpdbg/cfam.c:24:
> libpdbg/cfam.c: In function ‘opb_poll’:
> libpdbg/debug.h:32:23: error: format ‘%llx’ expects argument of type ‘long long unsigned int’, but argument 5 has type ‘uint64_t {aka long unsigned int}’ [-Werror=format=]
>   pdbg_log(PDBG_DEBUG, "%s[%d]: " x, __FUNCTION__, __LINE__, ##args)
>                        ^
> libpdbg/cfam.c:146:3: note: in expansion of macro ‘PR_DEBUG’
>    PR_DEBUG("  STAT=0x%16llx...\n", sval);
>    ^~~~~~~~
> libpdbg/cfam.c: In function ‘p8_opb_read’:
> libpdbg/debug.h:32:23: error: format ‘%llx’ expects argument of type ‘long long unsigned int’, but argument 5 has type ‘uint64_t {aka long unsigned int}’ [-Werror=format=]
>   pdbg_log(PDBG_DEBUG, "%s[%d]: " x, __FUNCTION__, __LINE__, ##args)
>                        ^
> libpdbg/cfam.c:196:2: note: in expansion of macro ‘PR_DEBUG’
>   PR_DEBUG("MFSI_OPB_READ: Writing 0x%16llx\n", opb_cmd);
>   ^~~~~~~~
> libpdbg/cfam.c: In function ‘p8_opb_write’:
> libpdbg/debug.h:32:23: error: format ‘%llx’ expects argument of type ‘long long unsigned int’, but argument 5 has type ‘uint64_t {aka long unsigned int}’ [-Werror=format=]
>   pdbg_log(PDBG_DEBUG, "%s[%d]: " x, __FUNCTION__, __LINE__, ##args)
>                        ^
> libpdbg/cfam.c:219:2: note: in expansion of macro ‘PR_DEBUG’
>   PR_DEBUG("MFSI_OPB_WRITE: Writing 0x%16llx\n", opb_cmd);
>   ^~~~~~~~
> cc1: all warnings being treated as errors

Cool, the patch works!

> But I have taken this patch and will roll the fixes for the above into it.

Cheers!

Joel
diff mbox series

Patch

diff --git a/libpdbg/debug.h b/libpdbg/debug.h
index c318e3a566e6..5fc027a64ead 100644
--- a/libpdbg/debug.h
+++ b/libpdbg/debug.h
@@ -18,6 +18,8 @@ 
 
 #include "libpdbg.h"
 
+void pdbg_log(int log_level, const char* fmt, ...) __attribute__((format (printf, 2, 3)));
+
 #define PR_ERROR(x, args...) \
 	pdbg_log(PDBG_ERROR, x, ##args)
 #define PR_WARNING(x, args...) \