c - 使用 GTK+2 时收到 “format not a string literal and no format arguments” 警告

标签 c gcc gtk gcc-warning gtk2

我遇到这样的错误:

warning: format not a string literal and no format arguments [-Wformat-security]
                                              GTK_BUTTONS_OK,
(const gchar*)message);
                   ^

因为这个函数:

static void show_message (gchar *message, GtkMessageType type) {
  GtkWidget *dialog = gtk_message_dialog_new(NULL, 0, type,
                                             GTK_BUTTONS_OK,
                                             message);
  gtk_dialog_run(GTK_DIALOG(dialog));
  gtk_widget_destroy(dialog);
}

我该如何解决?

最佳答案

答案很简单。

您必须将 "%s" 添加到 gtk_message_dialog_new() 函数的参数中,如下所示:

static void show_message (gchar *message, GtkMessageType type) {
  GtkWidget *dialog = gtk_message_dialog_new(NULL, 0, type,
                                             GTK_BUTTONS_OK, "%s",
                                             message);
  gtk_dialog_run(GTK_DIALOG(dialog));
  gtk_widget_destroy(dialog);
}

基本上,"%s" 的缺失被 gcc 认为是不安全的。

您可以在这里阅读更多相关信息:

关于c - 使用 GTK+2 时收到 “format not a string literal and no format arguments” 警告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30823717/

相关文章:

C memmove 相当于 for 循环 - 段错误

c - 由于从 -128 到 -1 的字符与从 +128 到 +255 的字符相同,那么使用 unsigned char 有什么意义呢?

c++ - 从 Code::Blocks 配置 g++ 不会影响命令行

c - Lxpanel 插件和线程

c++ - 关于tcl测试的问题

c - 需要有关 CS50 使用 'C' 的 luhns 算法的帮助

c++ - 针对 libx264 的链接不起作用 (Cygwin)

c - 如何使用 gcc (gtk3) 建立库的静态链接

GtkFlowBox 不使用水平方向在列中对齐

c - 如何循环更新 GtkProgressBar?