[libchamplain] Fix array usage in ChamplainZoomLevel



commit 576e4e7d94f21a9f3ed2d84d08552987269e0f53
Author: Pierre-Luc Beaudoin <pierre-luc pierlux com>
Date:   Sat Jun 13 18:18:39 2009 -0400

    Fix array usage in ChamplainZoomLevel
    
    Based on how the array was iterated, it wasn't rare that the requested
    index would be well over the array's end.

 champlain/champlain-view.c       |   28 ++++++++++++++++++++++++++--
 champlain/champlain-zoom-level.c |    8 +++++---
 2 files changed, 31 insertions(+), 5 deletions(-)
---
diff --git a/champlain/champlain-view.c b/champlain/champlain-view.c
index 7958136..84f3fb3 100644
--- a/champlain/champlain-view.c
+++ b/champlain/champlain-view.c
@@ -1638,14 +1638,23 @@ view_load_visible_tiles (ChamplainView *view)
   DEBUG ("Range %d, %d to %d, %d", x_first, y_first, x_count, y_count);
 
   int i, j;
-  guint k;
+  guint k = 0;
 
   // Get rid of old tiles first
-  for (k = 0; k < champlain_zoom_level_tile_count (level); k++)
+  int count = champlain_zoom_level_tile_count (level);
+  while (k < count)
     {
       ChamplainTile *tile = champlain_zoom_level_get_nth_tile (level, k);
+
+      if (tile == NULL)
+        {
+          k++;
+          continue;
+        }
+
       gint tile_x = champlain_tile_get_x (tile);
       gint tile_y = champlain_tile_get_y (tile);
+
       if (tile_x < x_first || tile_x > x_count ||
           tile_y < y_first || tile_y > y_count)
       {
@@ -1657,7 +1666,10 @@ view_load_visible_tiles (ChamplainView *view)
             clutter_container_remove_actor (CLUTTER_CONTAINER (group), actor);
           }
         champlain_zoom_level_remove_tile (level, tile);
+        count = champlain_zoom_level_tile_count (level);
       }
+      else
+        k++;
     }
 
   //Load new tiles if needed
@@ -1669,6 +1681,10 @@ view_load_visible_tiles (ChamplainView *view)
           for (k = 0; k < champlain_zoom_level_tile_count (level) && !exist; k++)
             {
               ChamplainTile *tile = champlain_zoom_level_get_nth_tile (level, k);
+
+              if (tile == NULL)
+                continue;
+
               gint tile_x = champlain_tile_get_x (tile);
               gint tile_y = champlain_tile_get_y (tile);
 
@@ -1726,6 +1742,10 @@ view_tiles_reposition (ChamplainView* view)
   for (i = 0; i < champlain_zoom_level_tile_count (priv->map->current_level); i++)
     {
       ChamplainTile *tile = champlain_zoom_level_get_nth_tile (priv->map->current_level, i);
+
+      if (tile == NULL)
+        continue;
+
       if (champlain_tile_get_state (tile) == CHAMPLAIN_STATE_DONE)
         view_position_tile (view, tile);
     }
@@ -1750,6 +1770,10 @@ view_update_state (ChamplainView *view)
   for (i = 0; i < champlain_zoom_level_tile_count (priv->map->current_level); i++)
     {
       ChamplainTile *tile = champlain_zoom_level_get_nth_tile (priv->map->current_level, i);
+
+      if (tile == NULL)
+        continue;
+
       if (champlain_tile_get_state (tile) == CHAMPLAIN_STATE_LOADING)
         new_state = CHAMPLAIN_STATE_LOADING;
     }
diff --git a/champlain/champlain-zoom-level.c b/champlain/champlain-zoom-level.c
index 155e323..6654879 100644
--- a/champlain/champlain-zoom-level.c
+++ b/champlain/champlain-zoom-level.c
@@ -108,7 +108,6 @@ champlain_zoom_level_set_property (GObject *object,
 static void
 champlain_zoom_level_dispose (GObject *object)
 {
-  //FIXME: Get rid of tiles here?
   guint k;
   ChamplainZoomLevel *level = CHAMPLAIN_ZOOM_LEVEL (object);
   ChamplainZoomLevelPrivate *priv = level->priv;
@@ -124,8 +123,9 @@ champlain_zoom_level_dispose (GObject *object)
       int count = champlain_zoom_level_tile_count (level);
       for (k = 0; k < count; k++)
         {
-          ChamplainTile *tile = champlain_zoom_level_get_nth_tile (level, k);
-          champlain_zoom_level_remove_tile (level, tile);
+          ChamplainTile *tile = champlain_zoom_level_get_nth_tile (level, 0);
+          if (tile != NULL)
+              champlain_zoom_level_remove_tile (level, tile);
         }
       g_ptr_array_free (priv->tiles, TRUE);
       priv->tiles = NULL;
@@ -260,6 +260,8 @@ champlain_zoom_level_get_nth_tile (ChamplainZoomLevel *self,
   g_return_val_if_fail (CHAMPLAIN_ZOOM_LEVEL (self), NULL);
 
   ChamplainZoomLevelPrivate *priv = self->priv;
+  g_return_val_if_fail (index < priv->tiles->len, NULL);
+  g_return_val_if_fail (index >= 0, NULL);
 
   return g_ptr_array_index (priv->tiles, index);
 }



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