[gimp/wip/Jehan/classy-GIMP: 54/55] libgimp: make sure gimp_item_get_by_id() allocates the right class.



commit 32b98e46d9a9ffca09affcfbc03d8285da72ad57
Author: Jehan <jehan girinstud io>
Date:   Mon Aug 19 17:43:43 2019 +0200

    libgimp: make sure gimp_item_get_by_id() allocates the right class.
    
    It is theoretically possible for an id to be reused. Normally here this
    should not happen, since the previous item would have been removed from
    the hash table anyway, when it got destroyed. Let's still be thorough.

 libgimp/gimpitem.c | 23 +++++++++++++++++++++++
 1 file changed, 23 insertions(+)
---
diff --git a/libgimp/gimpitem.c b/libgimp/gimpitem.c
index c2095ee749..ba111a944a 100644
--- a/libgimp/gimpitem.c
+++ b/libgimp/gimpitem.c
@@ -199,6 +199,29 @@ gimp_item_get_by_id (gint32 item_id)
       item = g_hash_table_lookup (gimp_items,
                                   GINT_TO_POINTER (item_id));
 
+      if (item)
+        {
+          /* Make sure the item is the proper class, since it could be
+           * reused (which means we'd have cycled over the whole int
+           * range; not that likely yet still possible on a very very
+           * long run process).
+           */
+          if ((_gimp_item_is_layer (item_id) &&
+               ! GIMP_IS_LAYER (item))               ||
+              (_gimp_item_is_layer_mask (item_id) &&
+               ! GIMP_IS_LAYER_MASK (item))          ||
+              (_gimp_item_is_selection (item_id)  &&
+               ! GIMP_IS_SELECTION (item))           ||
+              (_gimp_item_is_channel (item_id)    &&
+               ! GIMP_IS_CHANNEL (item))             ||
+              (_gimp_item_is_vectors (item_id)    &&
+               ! GIMP_IS_VECTORS (item)))
+            {
+              g_hash_table_remove (gimp_items, GINT_TO_POINTER (item_id));
+              item = NULL;
+            }
+        }
+
       if (! item)
         {
           if (_gimp_item_is_layer (item_id))


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