After an evaluation, GNOME has moved from Bugzilla to GitLab. Learn more about GitLab.
No new issues can be reported in GNOME Bugzilla anymore.
To report an issue in a GNOME project, go to GNOME GitLab.
Do not go to GNOME Gitlab for: Bluefish, Doxygen, GnuCash, GStreamer, java-gnome, LDTP, NetworkManager, Tomboy.
Bug 793074 - g_message() does not get -Wformat warnings when compiling with G_LOG_USE_STRUCTURED
g_message() does not get -Wformat warnings when compiling with G_LOG_USE_STRU...
Status: RESOLVED FIXED
Product: glib
Classification: Platform
Component: gerror
2.55.x
Other Linux
: High major
: 2.56
Assigned To: gtkdev
gtkdev
Depends on:
Blocks:
 
 
Reported: 2018-01-31 17:44 UTC by Philip Withnall
Modified: 2018-02-02 09:11 UTC
See Also:
GNOME target: ---
GNOME version: ---


Attachments
gmessages: Fix -Wformat warnings for g_message() and friends (13.10 KB, patch)
2018-02-01 13:47 UTC, Philip Withnall
none Details | Review
gmessages: Fix -Wformat warnings for g_message() and friends (13.05 KB, patch)
2018-02-01 14:09 UTC, Philip Withnall
committed Details | Review

Description Philip Withnall 2018-01-31 17:44:54 UTC
Because g_message() (and the other macros) pass format parameters as part of a larger set of varargs to g_log_structured(), there is (and can be) no G_GNUC_PRINTF annotation for g_log_structured(), and hence they don’t get checked. This is a regression on the previous version of g_message().

The fix for this is to replace

#define g_message(...)  g_log_structured (G_LOG_DOMAIN, G_LOG_LEVEL_MESSAGE,    \
                                          "CODE_FILE", __FILE__,                \
                                          "CODE_LINE", G_STRINGIFY (__LINE__),  \
                                          "CODE_FUNC", G_STRFUNC,                \
                                          "MESSAGE", __VA_ARGS__)

with

#define g_message(...) g_log_structured_standard (G_LOG_DOMAIN, G_LOG_LEVEL_MESSAGE, __FILE__, G_STRINGIFY (__LINE__), G_STRFUNC, __VA_ARGS__)

g_log_structured_standard() (better name suggestions welcome) would be a new semi-private helper function which has the appropriate G_GNUC_PRINTF annotation.
Comment 1 Philip Withnall 2018-02-01 13:47:17 UTC
Created attachment 367757 [details] [review]
gmessages: Fix -Wformat warnings for g_message() and friends

When compiling with G_LOG_USE_STRUCTURED, g_message(), g_debug(), etc.
use g_log_structured(). The message format string and its format
arguments are passed as the final set of arguments in a longer varargs
list, which includes the log domain and level (and other) fields.
Passing the message format in this way means it’s not possible for the
compiler to know to check its format placeholders when compiling with
-Wformat.

Fix support for this by adding a new semi-private helper function,
_g_log_structured_standard(), which only uses varargs for the message
format and its arguments, and uses fixed arguments for the other fields.
This is then converted to a set of GLogFields and passed to
g_log_structured() as normal.

Support for -Wformat when compiling *without* G_LOG_USE_STRUCTURED was
never broken.

Signed-off-by: Philip Withnall <withnall@endlessm.com>
Comment 2 Philip Withnall 2018-02-01 14:09:20 UTC
Created attachment 367758 [details] [review]
gmessages: Fix -Wformat warnings for g_message() and friends

When compiling with G_LOG_USE_STRUCTURED, g_message(), g_debug(), etc.
use g_log_structured(). The message format string and its format
arguments are passed as the final set of arguments in a longer varargs
list, which includes the log domain and level (and other) fields.
Passing the message format in this way means it’s not possible for the
compiler to know to check its format placeholders when compiling with
-Wformat.

Fix support for this by adding a new semi-private helper function,
_g_log_structured_standard(), which only uses varargs for the message
format and its arguments, and uses fixed arguments for the other fields.
This is then converted to a set of GLogFields and passed to
g_log_structured() as normal.

Support for -Wformat when compiling *without* G_LOG_USE_STRUCTURED was
never broken.

Signed-off-by: Philip Withnall <withnall@endlessm.com>
Comment 3 Matthias Clasen 2018-02-02 08:31:28 UTC
Review of attachment 367758 [details] [review]:

Looks good to me. Will gtk-doc complain about this function being undocumented ?
Comment 4 Philip Withnall 2018-02-02 09:10:35 UTC
(In reply to Matthias Clasen from comment #3)
> Review of attachment 367758 [details] [review] [review]:
> 
> Will gtk-doc complain about this function being
> undocumented ?

Shouldn’t do; the function’s not listed in glib-sections.txt.
Comment 5 Philip Withnall 2018-02-02 09:11:29 UTC
Merged to master, thanks.

Attachment 367758 [details] pushed as 32cc60d - gmessages: Fix -Wformat warnings for g_message() and friends