[gnome-games/gnome-2-28] gnobots: Fix build on Hurd (Bug #603909)



commit 95e662d8b9589344d5b7903647f1fdd095cb0591
Author: Emilio Pozuelo Monfort <pochu27 gmail com>
Date:   Wed Dec 16 18:00:38 2009 -0600

    gnobots: Fix build on Hurd (Bug #603909)
    
    gnobots uses PATH_MAX unconditionally, which is undefined on OSes that don't
    have a max path limit (e.g. the Hurd).
    
    - In one of the two places, it's used to read a config file (and not a path!).
    It could cause buffer overflows if PATH_MAX was smaller than the 256 bytes that
    can be read into the buffer... but anyway that PATH_MAX usage is weird and
    since there's a 256 hardcoded there I've made the buffer 256 bytes long too.
    
    - The other usage is to return the config name. If the config name was returned
    unmodified, it could return it directly and there would be no problem, but
    since it does, a solution is to return a new string that the caller must free
    (which is what this patch does).

 gnobots2/gameconfig.c |   12 ++++++------
 gnobots2/gnobots.c    |    6 +++++-
 gnobots2/properties.c |   24 +++++++++++++++++++-----
 3 files changed, 30 insertions(+), 12 deletions(-)
---
diff --git a/gnobots2/gameconfig.c b/gnobots2/gameconfig.c
index 8be6bf2..fd97af5 100644
--- a/gnobots2/gameconfig.c
+++ b/gnobots2/gameconfig.c
@@ -71,7 +71,7 @@ load_config (gchar * fname)
   GameConfig *gcfg;
   gint pflag = 0;
   FILE *fp;
-  gchar buffer[PATH_MAX];
+  gchar buffer[256];
   gchar *bptr;
   gchar *bpstart, *bpstart2;
   gchar *vptr;
@@ -93,7 +93,7 @@ load_config (gchar * fname)
   gcfg->description = g_string_new (bpstart2);
   g_free (bpstart2);
 
-  while (fgets (buffer, 256, fp) != NULL) {
+  while (fgets (buffer, sizeof(buffer), fp) != NULL) {
     if (strlen (buffer) < 3)
       continue;
 
@@ -416,13 +416,12 @@ current_game_config (void)
  * returns a string containing the name of the specified configuration
  *
  * Returns:
- * pointer to a string containing the name
+ * pointer to a string containing the name that the caller must free.
  **/
 gchar *
 game_config_name (gint n)
 {
-  static gchar buffer[PATH_MAX];
-  gchar *ptr = buffer;
+  gchar *buffer, *ptr;
 
   if (game_configs == NULL)
     return NULL;
@@ -430,7 +429,8 @@ game_config_name (gint n)
   if ((n < 0) || (n >= num_configs))
     return NULL;
 
-  strcpy (buffer, game_configs[n]->description->str);
+  buffer = g_strdup (game_configs[n]->description->str);
+  ptr = buffer;
 
   while (*ptr) {
     if (*ptr == '_') {
diff --git a/gnobots2/gnobots.c b/gnobots2/gnobots.c
index 1e3d6fe..c3c028f 100644
--- a/gnobots2/gnobots.c
+++ b/gnobots2/gnobots.c
@@ -200,6 +200,7 @@ main (int argc, char *argv[])
   GOptionContext *context;
   struct timeval tv;
   gint i;
+  gchar *config;
   gboolean retval;
   GError *error = NULL;
 #ifdef WITH_SMCLIENT
@@ -356,10 +357,13 @@ main (int argc, char *argv[])
 
   if (cmdline_config) {
     for (i = 0; i < num_game_configs (); ++i) {
-      if (!strcmp (cmdline_config, game_config_name (i))) {
+      config = game_config_name (i);
+      if (!strcmp (cmdline_config, config)) {
 	properties_set_config (i);
+	g_free (config);
 	break;
       }
+      g_free (config);
     }
   }
 
diff --git a/gnobots2/properties.c b/gnobots2/properties.c
index 32d70f4..0d0b347 100644
--- a/gnobots2/properties.c
+++ b/gnobots2/properties.c
@@ -213,11 +213,14 @@ pmap_selection (GtkWidget * widget, gpointer data)
 static void
 type_selection (GtkWidget * widget, gpointer data)
 {
+  gchar *config;
   gint num = gtk_combo_box_get_active (GTK_COMBO_BOX (widget));
 
   properties.selected_config = num;
 
-  conf_set_configuration (game_config_name (properties.selected_config));
+  config = game_config_name (properties.selected_config);
+  conf_set_configuration (config);
+  g_free (config);
 
   set_game_config (properties.selected_config);
 
@@ -307,6 +310,7 @@ static void
 fill_typemenu (GtkWidget * menu)
 {
   gint i;
+  gchar *config;
 
 #if 0
   /* this is just a place holder so that xgettext can found the strings to
@@ -322,7 +326,9 @@ fill_typemenu (GtkWidget * menu)
 #endif
 
   for (i = 0; i < num_game_configs (); ++i) {
-    gtk_combo_box_append_text (GTK_COMBO_BOX (menu), _(game_config_name (i)));
+    config = game_config_name (i);
+    gtk_combo_box_append_text (GTK_COMBO_BOX (menu), _(config));
+    g_free (config);
   }
 
   gtk_combo_box_set_active (GTK_COMBO_BOX (menu), properties.selected_config);
@@ -593,7 +599,7 @@ load_properties (void)
 {
   gchar *cname = NULL;
   gint i;
-  gchar *bgcolour;
+  gchar *bgcolour, *config;
 
   load_keys ();
 
@@ -610,10 +616,13 @@ load_properties (void)
 
   properties.selected_config = 0;
   for (i = 0; i < num_game_configs (); ++i) {
-    if (!strcmp (cname, game_config_name (i))) {
+    config = game_config_name (i);
+    if (!strcmp (cname, config)) {
+      g_free (config);
       properties.selected_config = i;
       break;
     }
+    g_free (config);
   }
   g_free (cname);
 
@@ -717,13 +726,18 @@ gboolean
 save_properties (void)
 {
   gint i;
+  gchar *config;
 
   for (i = 0; i < 12; i++) {
     conf_set_control_key (i, properties.keys[i]);
   }
 
   conf_set_theme (properties.themename);
-  conf_set_configuration (game_config_name (properties.selected_config));
+
+  config = game_config_name (properties.selected_config);
+  conf_set_configuration (config);
+  g_free (config);
+
   conf_set_use_safe_moves (properties.safe_moves);
   conf_set_use_super_safe_moves (properties.super_safe_moves);
   conf_set_enable_sound (properties.sound);



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]