[virt-tools-list] [virt-viewer PATCH 2/5] Annotate message dialog helpers with format(gnu_printf)

Christophe Fergeau cfergeau at redhat.com
Thu Feb 14 09:35:17 UTC 2019


Hey,

On Wed, Feb 13, 2019 at 06:25:24PM +0100, Marc-André Lureau wrote:
> Hi
> On Wed, Feb 13, 2019 at 6:05 PM Daniel P. Berrangé <berrange at redhat.com> wrote:
> >
> > This allows the compiler to validate the format string and args passed
> > to the function.
> >
> > Signed-off-by: Daniel P. Berrangé <berrange at redhat.com>
> 
> Could we consider the slightly less uglier and foreign
> compiler-friendly G_GNUC_PRINTF?

If the format string is passed to a glib function, G_GNUC_PRINTF is
better, for example on Windows, glib G_GNUC_PRINTF changed from
format(printf) to format(gnu_printf) which gives different warnings for
some of the % formatters. Using G_GNUC_PRINTF will give us better
consistency in these warnings.

Christophe

> 
> 
> > ---
> >  src/virt-viewer-app.c      | 38 +++++++++++++++++++++++---------------
> >  src/virt-viewer-notebook.c |  4 ++--
> >  2 files changed, 25 insertions(+), 17 deletions(-)
> >
> > diff --git a/src/virt-viewer-app.c b/src/virt-viewer-app.c
> > index fd48014..03a7c00 100644
> > --- a/src/virt-viewer-app.c
> > +++ b/src/virt-viewer-app.c
> > @@ -197,19 +197,16 @@ virt_viewer_app_set_debug(gboolean debug)
> >      doDebug = debug;
> >  }
> >
> > -static GtkWidget*
> > -virt_viewer_app_make_message_dialog(VirtViewerApp *self,
> > -                                    const char *fmt, ...)
> > +__attribute__((format(gnu_printf, 2, 0))) static GtkWidget*
> > +virt_viewer_app_make_message_dialogv(VirtViewerApp *self,
> > +                                     const char *fmt, va_list vargs)
> >  {
> >      g_return_val_if_fail(VIRT_VIEWER_IS_APP(self), NULL);
> >      GtkWindow *window = GTK_WINDOW(virt_viewer_window_get_window(self->priv->main_window));
> >      GtkWidget *dialog;
> >      char *msg;
> > -    va_list vargs;
> >
> > -    va_start(vargs, fmt);
> >      msg = g_strdup_vprintf(fmt, vargs);
> > -    va_end(vargs);
> >
> >      dialog = gtk_message_dialog_new(window,
> >                                      GTK_DIALOG_MODAL |
> > @@ -224,23 +221,34 @@ virt_viewer_app_make_message_dialog(VirtViewerApp *self,
> >      return dialog;
> >  }
> >
> > -void
> > +__attribute__((format(gnu_printf, 2, 3))) static GtkWidget*
> > +virt_viewer_app_make_message_dialog(VirtViewerApp *self,
> > +                                    const char *fmt, ...)
> > +{
> > +    g_return_val_if_fail(VIRT_VIEWER_IS_APP(self), NULL);
> > +    GtkWidget *dialog;
> > +    va_list vargs;
> > +
> > +    va_start(vargs, fmt);
> > +    dialog = virt_viewer_app_make_message_dialogv(self, fmt, vargs);
> > +    va_end(vargs);
> > +
> > +    return dialog;
> > +}
> > +
> > +__attribute__((format(gnu_printf, 2, 3))) void
> >  virt_viewer_app_simple_message_dialog(VirtViewerApp *self,
> >                                        const char *fmt, ...)
> >  {
> >      GtkWidget *dialog;
> > -    char *msg;
> >      va_list vargs;
> >
> >      va_start(vargs, fmt);
> > -    msg = g_strdup_vprintf(fmt, vargs);
> > +    dialog = virt_viewer_app_make_message_dialogv(self, fmt, vargs);
> >      va_end(vargs);
> >
> > -    dialog = virt_viewer_app_make_message_dialog(self, msg);
> >      gtk_dialog_run(GTK_DIALOG(dialog));
> >      gtk_widget_destroy(dialog);
> > -
> > -    g_free(msg);
> >  }
> >
> >  static void
> > @@ -668,7 +676,7 @@ virt_viewer_app_open_unix_sock(const char *unixsock, GError **error)
> >
> >  #endif /* defined(HAVE_SOCKETPAIR) && defined(HAVE_FORK) */
> >
> > -void
> > +__attribute__((format(gnu_printf, 2, 3))) void
> >  virt_viewer_app_trace(VirtViewerApp *self,
> >                        const char *fmt, ...)
> >  {
> > @@ -1871,7 +1879,7 @@ virt_viewer_app_on_application_startup(GApplication *app)
> >
> >      if (!virt_viewer_app_start(self, &error)) {
> >          if (error && !g_error_matches(error, VIRT_VIEWER_ERROR, VIRT_VIEWER_ERROR_CANCELLED))
> > -            virt_viewer_app_simple_message_dialog(self, error->message);
> > +            virt_viewer_app_simple_message_dialog(self, "%s", error->message);
> >
> >          g_clear_error(&error);
> >          g_application_quit(app);
> > @@ -2477,7 +2485,7 @@ show_status_cb(gpointer value,
> >      virt_viewer_notebook_show_status(nb, text);
> >  }
> >
> > -void
> > +__attribute__((format(gnu_printf, 2, 3))) void
> >  virt_viewer_app_show_status(VirtViewerApp *self, const gchar *fmt, ...)
> >  {
> >      va_list args;
> > diff --git a/src/virt-viewer-notebook.c b/src/virt-viewer-notebook.c
> > index 3a74e9f..a2092d2 100644
> > --- a/src/virt-viewer-notebook.c
> > +++ b/src/virt-viewer-notebook.c
> > @@ -82,7 +82,7 @@ virt_viewer_notebook_init (VirtViewerNotebook *self)
> >      gtk_notebook_append_page(GTK_NOTEBOOK(self), priv->status, NULL);
> >  }
> >
> > -void
> > +__attribute__((format(gnu_printf, 2, 0))) void
> >  virt_viewer_notebook_show_status_va(VirtViewerNotebook *self, const gchar *fmt, va_list args)
> >  {
> >      VirtViewerNotebookPrivate *priv;
> > @@ -99,7 +99,7 @@ virt_viewer_notebook_show_status_va(VirtViewerNotebook *self, const gchar *fmt,
> >      g_free(text);
> >  }
> >
> > -void
> > +__attribute__((format(gnu_printf, 2, 3))) void
> >  virt_viewer_notebook_show_status(VirtViewerNotebook *self, const gchar *fmt, ...)
> >  {
> >      va_list args;
> > --
> > 2.20.1
> >
> > _______________________________________________
> > virt-tools-list mailing list
> > virt-tools-list at redhat.com
> > https://www.redhat.com/mailman/listinfo/virt-tools-list
> 
> _______________________________________________
> virt-tools-list mailing list
> virt-tools-list at redhat.com
> https://www.redhat.com/mailman/listinfo/virt-tools-list
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 833 bytes
Desc: not available
URL: <http://listman.redhat.com/archives/virt-tools-list/attachments/20190214/713a582d/attachment.sig>


More information about the virt-tools-list mailing list