Blame SOURCES/0001-cogl-Use-autopointers-to-free-structs-on-return.patch

79df40
From a5fa286732b08b832ea642347963cb0456c1e19f Mon Sep 17 00:00:00 2001
79df40
From: =?UTF-8?q?Marco=20Trevisan=20=28Trevi=C3=B1o=29?= <mail@3v1n0.net>
79df40
Date: Tue, 9 Jul 2019 11:13:09 +0200
79df40
Subject: [PATCH 01/28] cogl: Use autopointers to free structs on return
79df40
79df40
This is a potential leak discovered by static analysis, in fact if
79df40
_COGL_GET_CONTEXT returns, the newly allocated struct isn't released.
79df40
79df40
https://gitlab.gnome.org/GNOME/mutter/merge_requests/682
79df40
---
79df40
 cogl/cogl-pango/cogl-pango-fontmap.c | 4 ++--
79df40
 cogl/cogl/cogl-onscreen.c            | 4 +++-
79df40
 cogl/cogl/cogl-pipeline-cache.c      | 4 ++--
79df40
 3 files changed, 7 insertions(+), 5 deletions(-)
79df40
79df40
diff --git a/cogl/cogl-pango/cogl-pango-fontmap.c b/cogl/cogl-pango/cogl-pango-fontmap.c
79df40
index 145f3b9e8..3ec64eee6 100644
79df40
--- a/cogl/cogl-pango/cogl-pango-fontmap.c
79df40
+++ b/cogl/cogl-pango/cogl-pango-fontmap.c
79df40
@@ -49,72 +49,72 @@
79df40
 #include <pango/pango-renderer.h>
79df40
 
79df40
 #include "cogl-pango.h"
79df40
 #include "cogl-pango-private.h"
79df40
 #include "cogl-util.h"
79df40
 #include "cogl/cogl-context-private.h"
79df40
 
79df40
 static GQuark cogl_pango_font_map_get_priv_key (void) G_GNUC_CONST;
79df40
 
79df40
 typedef struct _CoglPangoFontMapPriv
79df40
 {
79df40
   CoglContext *ctx;
79df40
   PangoRenderer *renderer;
79df40
 } CoglPangoFontMapPriv;
79df40
 
79df40
 static void
79df40
 free_priv (gpointer data)
79df40
 {
79df40
   CoglPangoFontMapPriv *priv = data;
79df40
 
79df40
   cogl_object_unref (priv->ctx);
79df40
   cogl_object_unref (priv->renderer);
79df40
 
79df40
   g_free (priv);
79df40
 }
79df40
 
79df40
 PangoFontMap *
79df40
 cogl_pango_font_map_new (void)
79df40
 {
79df40
   PangoFontMap *fm = pango_cairo_font_map_new ();
79df40
-  CoglPangoFontMapPriv *priv = g_new0 (CoglPangoFontMapPriv, 1);
79df40
+  g_autofree CoglPangoFontMapPriv *priv = g_new0 (CoglPangoFontMapPriv, 1);
79df40
 
79df40
   _COGL_GET_CONTEXT (context, NULL);
79df40
 
79df40
   priv->ctx = cogl_object_ref (context);
79df40
 
79df40
   /* XXX: The public pango api doesn't let us sub-class
79df40
    * PangoCairoFontMap so we attach our own private data using qdata
79df40
    * for now. */
79df40
   g_object_set_qdata_full (G_OBJECT (fm),
79df40
                            cogl_pango_font_map_get_priv_key (),
79df40
-                           priv,
79df40
+                           g_steal_pointer (&priv),
79df40
                            free_priv);
79df40
 
79df40
   return fm;
79df40
 }
79df40
 
79df40
 PangoContext *
79df40
 cogl_pango_font_map_create_context (CoglPangoFontMap *fm)
79df40
 {
79df40
   _COGL_RETURN_VAL_IF_FAIL (COGL_PANGO_IS_FONT_MAP (fm), NULL);
79df40
 
79df40
 #if PANGO_VERSION_CHECK (1, 22, 0)
79df40
   /* We can just directly use the pango context from the Cairo font
79df40
      map */
79df40
   return pango_font_map_create_context (PANGO_FONT_MAP (fm));
79df40
 #else
79df40
   return pango_cairo_font_map_create_context (PANGO_CAIRO_FONT_MAP (fm));
79df40
 #endif
79df40
 }
79df40
 
79df40
 static CoglPangoFontMapPriv *
79df40
 _cogl_pango_font_map_get_priv (CoglPangoFontMap *fm)
79df40
 {
79df40
   return g_object_get_qdata (G_OBJECT (fm),
79df40
 			     cogl_pango_font_map_get_priv_key ());
79df40
 }
79df40
 
79df40
 PangoRenderer *
79df40
 _cogl_pango_font_map_get_renderer (CoglPangoFontMap *fm)
79df40
 {
79df40
   CoglPangoFontMapPriv *priv = _cogl_pango_font_map_get_priv (fm);
79df40
diff --git a/cogl/cogl/cogl-onscreen.c b/cogl/cogl/cogl-onscreen.c
79df40
index e766e59d0..fcf7f4168 100644
79df40
--- a/cogl/cogl/cogl-onscreen.c
79df40
+++ b/cogl/cogl/cogl-onscreen.c
79df40
@@ -72,64 +72,66 @@ COGL_GTYPE_DEFINE_BOXED (FrameClosure, frame_closure,
79df40
                          cogl_dummy_free);
79df40
 COGL_GTYPE_DEFINE_BOXED (OnscreenResizeClosure,
79df40
                          onscreen_resize_closure,
79df40
                          cogl_dummy_copy,
79df40
                          cogl_dummy_free);
79df40
 COGL_GTYPE_DEFINE_BOXED (OnscreenDirtyClosure,
79df40
                          onscreen_dirty_closure,
79df40
                          cogl_dummy_copy,
79df40
                          cogl_dummy_free);
79df40
 
79df40
 static void
79df40
 _cogl_onscreen_init_from_template (CoglOnscreen *onscreen,
79df40
                                    CoglOnscreenTemplate *onscreen_template)
79df40
 {
79df40
   CoglFramebuffer *framebuffer = COGL_FRAMEBUFFER (onscreen);
79df40
 
79df40
   _cogl_list_init (&onscreen->frame_closures);
79df40
   _cogl_list_init (&onscreen->resize_closures);
79df40
   _cogl_list_init (&onscreen->dirty_closures);
79df40
 
79df40
   framebuffer->config = onscreen_template->config;
79df40
   cogl_object_ref (framebuffer->config.swap_chain);
79df40
 }
79df40
 
79df40
 /* XXX: While we still have backend in Clutter we need a dummy object
79df40
  * to represent the CoglOnscreen framebuffer that the backend
79df40
  * creates... */
79df40
 CoglOnscreen *
79df40
 _cogl_onscreen_new (void)
79df40
 {
79df40
-  CoglOnscreen *onscreen = g_new0 (CoglOnscreen, 1);
79df40
+  g_autofree CoglOnscreen *onscreen_ptr = g_new0 (CoglOnscreen, 1);
79df40
+  CoglOnscreen *onscreen;
79df40
 
79df40
   _COGL_GET_CONTEXT (ctx, NULL);
79df40
 
79df40
+  onscreen = g_steal_pointer (&onscreen_ptr);
79df40
   _cogl_framebuffer_init (COGL_FRAMEBUFFER (onscreen),
79df40
                           ctx,
79df40
                           COGL_FRAMEBUFFER_TYPE_ONSCREEN,
79df40
                           0x1eadbeef, /* width */
79df40
                           0x1eadbeef); /* height */
79df40
   /* NB: make sure to pass positive width/height numbers here
79df40
    * because otherwise we'll hit input validation assertions!*/
79df40
 
79df40
   _cogl_onscreen_init_from_template (onscreen, ctx->display->onscreen_template);
79df40
 
79df40
   COGL_FRAMEBUFFER (onscreen)->allocated = TRUE;
79df40
 
79df40
   /* XXX: Note we don't initialize onscreen->winsys in this case. */
79df40
 
79df40
   return _cogl_onscreen_object_new (onscreen);
79df40
 }
79df40
 
79df40
 CoglOnscreen *
79df40
 cogl_onscreen_new (CoglContext *ctx, int width, int height)
79df40
 {
79df40
   CoglOnscreen *onscreen;
79df40
 
79df40
   /* FIXME: We are assuming onscreen buffers will always be
79df40
      premultiplied so we'll set the premult flag on the bitmap
79df40
      format. This will usually be correct because the result of the
79df40
      default blending operations for Cogl ends up with premultiplied
79df40
      data in the framebuffer. However it is possible for the
79df40
      framebuffer to be in whatever format depending on what
79df40
      CoglPipeline is used to render to it. Eventually we may want to
79df40
      add a way for an application to inform Cogl that the framebuffer
79df40
diff --git a/cogl/cogl/cogl-pipeline-cache.c b/cogl/cogl/cogl-pipeline-cache.c
79df40
index 62b372406..4267d2ccb 100644
79df40
--- a/cogl/cogl/cogl-pipeline-cache.c
79df40
+++ b/cogl/cogl/cogl-pipeline-cache.c
79df40
@@ -25,91 +25,91 @@
79df40
  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
79df40
  * SOFTWARE.
79df40
  *
79df40
  *
79df40
  * Authors:
79df40
  *   Neil Roberts <neil@linux.intel.com>
79df40
  *   Robert Bragg <robert@linux.intel.com>
79df40
  */
79df40
 
79df40
 #ifdef HAVE_CONFIG_H
79df40
 #include "cogl-config.h"
79df40
 #endif
79df40
 
79df40
 #include <test-fixtures/test-unit.h>
79df40
 
79df40
 #include "cogl-context-private.h"
79df40
 #include "cogl-pipeline-private.h"
79df40
 #include "cogl-pipeline-cache.h"
79df40
 #include "cogl-pipeline-hash-table.h"
79df40
 
79df40
 struct _CoglPipelineCache
79df40
 {
79df40
   CoglPipelineHashTable fragment_hash;
79df40
   CoglPipelineHashTable vertex_hash;
79df40
   CoglPipelineHashTable combined_hash;
79df40
 };
79df40
 
79df40
 CoglPipelineCache *
79df40
 _cogl_pipeline_cache_new (void)
79df40
 {
79df40
-  CoglPipelineCache *cache = g_new (CoglPipelineCache, 1);
79df40
+  g_autofree CoglPipelineCache *cache = g_new (CoglPipelineCache, 1);
79df40
   unsigned long vertex_state;
79df40
   unsigned long layer_vertex_state;
79df40
   unsigned int fragment_state;
79df40
   unsigned int layer_fragment_state;
79df40
 
79df40
   _COGL_GET_CONTEXT (ctx, 0);
79df40
 
79df40
   vertex_state =
79df40
     _cogl_pipeline_get_state_for_vertex_codegen (ctx);
79df40
   layer_vertex_state =
79df40
     COGL_PIPELINE_LAYER_STATE_AFFECTS_VERTEX_CODEGEN;
79df40
   fragment_state =
79df40
     _cogl_pipeline_get_state_for_fragment_codegen (ctx);
79df40
   layer_fragment_state =
79df40
     _cogl_pipeline_get_layer_state_for_fragment_codegen (ctx);
79df40
 
79df40
   _cogl_pipeline_hash_table_init (&cache->vertex_hash,
79df40
                                   vertex_state,
79df40
                                   layer_vertex_state,
79df40
                                   "vertex shaders");
79df40
   _cogl_pipeline_hash_table_init (&cache->fragment_hash,
79df40
                                   fragment_state,
79df40
                                   layer_fragment_state,
79df40
                                   "fragment shaders");
79df40
   _cogl_pipeline_hash_table_init (&cache->combined_hash,
79df40
                                   vertex_state | fragment_state,
79df40
                                   layer_vertex_state | layer_fragment_state,
79df40
                                   "programs");
79df40
 
79df40
-  return cache;
79df40
+  return g_steal_pointer (&cache);
79df40
 }
79df40
 
79df40
 void
79df40
 _cogl_pipeline_cache_free (CoglPipelineCache *cache)
79df40
 {
79df40
   _cogl_pipeline_hash_table_destroy (&cache->fragment_hash);
79df40
   _cogl_pipeline_hash_table_destroy (&cache->vertex_hash);
79df40
   _cogl_pipeline_hash_table_destroy (&cache->combined_hash);
79df40
   g_free (cache);
79df40
 }
79df40
 
79df40
 CoglPipelineCacheEntry *
79df40
 _cogl_pipeline_cache_get_fragment_template (CoglPipelineCache *cache,
79df40
                                             CoglPipeline *key_pipeline)
79df40
 {
79df40
   return _cogl_pipeline_hash_table_get (&cache->fragment_hash,
79df40
                                         key_pipeline);
79df40
 }
79df40
 
79df40
 CoglPipelineCacheEntry *
79df40
 _cogl_pipeline_cache_get_vertex_template (CoglPipelineCache *cache,
79df40
                                           CoglPipeline *key_pipeline)
79df40
 {
79df40
   return _cogl_pipeline_hash_table_get (&cache->vertex_hash,
79df40
                                         key_pipeline);
79df40
 }
79df40
 
79df40
 CoglPipelineCacheEntry *
79df40
 _cogl_pipeline_cache_get_combined_template (CoglPipelineCache *cache,
79df40
                                             CoglPipeline *key_pipeline)
79df40
-- 
79df40
2.26.2
79df40