gtk_message_dialog_new showing literal warning!

This is a discussion on gtk_message_dialog_new showing literal warning! within the Linux Programming forums, part of the Platform Specific Boards category; Hello friends, Please have a look to the code below: Code: void error_message(const gchar* message) { GtkWidget *dialog; g_warning("%s", message); ...

  1. #1
    Registered User Ravi Raj's Avatar
    Join Date
    May 2012
    Location
    INDIA
    Posts
    43

    gtk_message_dialog_new showing literal warning!

    Hello friends,

    Please have a look to the code below:

    Code:
    void
    error_message(const gchar* message)
    {
        GtkWidget *dialog;
        
        g_warning("%s", message);
        
        dialog = gtk_message_dialog_new(NULL, GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT, GTK_MESSAGE_ERROR, GTK_BUTTONS_OK, message);
    
        gtk_window_set_title(GTK_WINDOW(dialog), "Program Error!");
        gtk_dialog_run(GTK_DIALOG(dialog));
        gtk_widget_destroy(dialog);
    }
    The above given code is showing the following error:
    Code:
    warning: format not a string literal and no format arguments [-Wformat-security]
    in line:
    Code:
    dialog = gtk_message_dialog_new(NULL, GTK_DIALOG_MODAL |  GTK_DIALOG_DESTROY_WITH_PARENT, GTK_MESSAGE_ERROR, GTK_BUTTONS_OK,  message);
    near 'message' variable, I don't know where I am wrong.
    Please help.
    Last edited by Ravi Raj; 05-15-2012 at 07:17 AM. Reason: simple code changes

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,452
    It's warning you that you're doing basically the same as this

    Code:
    void foo ( char *message ) {
        printf(message);
    }
    If this code is called with a string containing % characters, then it is well and truly broken.
    Get the right mix of % formats, and all sorts of really bad things happen (lookup format string attacks on the web).

    Instead, consider doing something like
    Code:
    dialog = gtk_message_dialog_new(NULL, 
            GTK_DIALOG_MODAL |  GTK_DIALOG_DESTROY_WITH_PARENT, 
            GTK_MESSAGE_ERROR, GTK_BUTTONS_OK,  
            "%s", message);
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User Ravi Raj's Avatar
    Join Date
    May 2012
    Location
    INDIA
    Posts
    43

    Thanks.

    Quote Originally Posted by Salem View Post
    It's warning you that you're doing basically the same as this

    Code:
    void foo ( char *message ) {
        printf(message);
    }
    If this code is called with a string containing % characters, then it is well and truly broken.
    Get the right mix of % formats, and all sorts of really bad things happen (lookup format string attacks on the web).

    Instead, consider doing something like
    Code:
    dialog = gtk_message_dialog_new(NULL, 
            GTK_DIALOG_MODAL |  GTK_DIALOG_DESTROY_WITH_PARENT, 
            GTK_MESSAGE_ERROR, GTK_BUTTONS_OK,  
            "%s", message);
    That solved my problem.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Modifying a string literal
    By Richardcavell in forum C Programming
    Replies: 3
    Last Post: 02-14-2011, 11:26 PM
  2. string literal assignment
    By @nthony in forum C Programming
    Replies: 1
    Last Post: 03-13-2009, 12:06 PM
  3. Literal UL
    By shani in forum C Programming
    Replies: 4
    Last Post: 02-09-2008, 02:53 PM
  4. use of hex literal constant
    By happycoder in forum C++ Programming
    Replies: 6
    Last Post: 06-07-2003, 11:45 PM
  5. String literal
    By subdene in forum C++ Programming
    Replies: 5
    Last Post: 11-05-2002, 01:10 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21