[gtk/gtk-2-24] Protect CGContext calls from null CGContextRefs.



commit 0388a321b483c907db71aec08cc08ab2bc40bf79
Author: John Ralls <jralls ceridwen us>
Date:   Thu Nov 1 13:17:27 2018 -0700

    Protect CGContext calls from null CGContextRefs.
    
    To prevent invalid CGContext warnings.

 gdk/quartz/gdkdrawable-quartz.c |  3 +++
 gdk/quartz/gdkgc-quartz.c       | 24 ++++++++++++++++++------
 gdk/quartz/gdkpixmap-quartz.c   | 13 ++++++++-----
 gdk/quartz/gdkwindow-quartz.c   |  6 ++++++
 4 files changed, 35 insertions(+), 11 deletions(-)
---
diff --git a/gdk/quartz/gdkdrawable-quartz.c b/gdk/quartz/gdkdrawable-quartz.c
index e5b6ace48d..00a53b226c 100644
--- a/gdk/quartz/gdkdrawable-quartz.c
+++ b/gdk/quartz/gdkdrawable-quartz.c
@@ -884,6 +884,9 @@ void
 gdk_quartz_drawable_release_context (GdkDrawable  *drawable, 
                                     CGContextRef  cg_context)
 {
+  if (!context)
+    return;
+  
   if (GDK_IS_WINDOW_IMPL_QUARTZ (drawable))
     {
       GdkWindowImplQuartz *window_impl = GDK_WINDOW_IMPL_QUARTZ (drawable);
diff --git a/gdk/quartz/gdkgc-quartz.c b/gdk/quartz/gdkgc-quartz.c
index 1e64972517..6a8968bdf0 100644
--- a/gdk/quartz/gdkgc-quartz.c
+++ b/gdk/quartz/gdkgc-quartz.c
@@ -112,13 +112,16 @@ create_clip_mask (GdkPixmap *source_pixmap)
                                       CGImageGetColorSpace (source),
                                       CGImageGetBitmapInfo (source));
 
-  CGContextTranslateCTM (cg_context, 0, height);
-  CGContextScaleCTM (cg_context, 1.0, -1.0);
+  if (cg_context)
+    {
+      CGContextTranslateCTM (cg_context, 0, height);
+      CGContextScaleCTM (cg_context, 1.0, -1.0);
 
-  CGContextDrawImage (cg_context,
-                      CGRectMake (0, 0, width, height), source);
+      CGContextDrawImage (cg_context,
+                          CGRectMake (0, 0, width, height), source);
 
-  CGContextRelease (cg_context);
+      CGContextRelease (cg_context);
+    }
 
   return clip_mask;
 }
@@ -366,6 +369,9 @@ gdk_quartz_draw_tiled_pattern (void         *info,
   CGImageRef   pattern_image;
   size_t       width, height;
 
+  if (!context)
+    return;
+  
   pattern_image = _gdk_pixmap_get_cgimage (GDK_PIXMAP (_gdk_gc_get_tile (gc)));
 
   width = CGImageGetWidth (pattern_image);
@@ -387,6 +393,9 @@ gdk_quartz_draw_stippled_pattern (void         *info,
   CGRect      rect;
   CGColorRef  color;
 
+  if (!context)
+    return;
+
   pattern_image = _gdk_pixmap_get_cgimage (GDK_PIXMAP (_gdk_gc_get_stipple (gc)));
   rect = CGRectMake (0, 0,
                     CGImageGetWidth (pattern_image),
@@ -413,6 +422,9 @@ gdk_quartz_draw_opaque_stippled_pattern (void         *info,
   CGRect      rect;
   CGColorRef  color;
 
+  if (!context)
+    return;
+
   pattern_image = _gdk_pixmap_get_cgimage (GDK_PIXMAP (_gdk_gc_get_stipple (gc)));
   rect = CGRectMake (0, 0,
                     CGImageGetWidth (pattern_image),
@@ -448,7 +460,7 @@ _gdk_quartz_gc_update_cg_context (GdkGC                      *gc,
 
   g_return_val_if_fail (gc == NULL || GDK_IS_GC (gc), FALSE);
 
-  if (!gc)
+  if (!gc || !context)
     return FALSE;
 
   private = GDK_GC_QUARTZ (gc);
diff --git a/gdk/quartz/gdkpixmap-quartz.c b/gdk/quartz/gdkpixmap-quartz.c
index b7dcc88849..c8918bfe03 100644
--- a/gdk/quartz/gdkpixmap-quartz.c
+++ b/gdk/quartz/gdkpixmap-quartz.c
@@ -134,14 +134,17 @@ gdk_pixmap_impl_quartz_get_context (GdkDrawable *drawable,
                                       bytes_per_row,
                                       colorspace,
                                       alpha_info);
-  CGContextSetAllowsAntialiasing (cg_context, antialias);
 
-  CGColorSpaceRelease (colorspace);
+  if (cg_context)
+    {
+      CGContextSetAllowsAntialiasing (cg_context, antialias);
 
-  /* convert coordinates from core graphics to gtk+ */
-  CGContextTranslateCTM (cg_context, 0, impl->height);
-  CGContextScaleCTM (cg_context, 1.0, -1.0);
+      CGColorSpaceRelease (colorspace);
 
+      /* convert coordinates from core graphics to gtk+ */
+      CGContextTranslateCTM (cg_context, 0, impl->height);
+      CGContextScaleCTM (cg_context, 1.0, -1.0);
+    }
   return cg_context;
 }
 
diff --git a/gdk/quartz/gdkwindow-quartz.c b/gdk/quartz/gdkwindow-quartz.c
index d745da54de..6701b2d98f 100644
--- a/gdk/quartz/gdkwindow-quartz.c
+++ b/gdk/quartz/gdkwindow-quartz.c
@@ -113,6 +113,8 @@ gdk_window_impl_quartz_get_context (GdkDrawable *drawable,
        cg_context = [[NSGraphicsContext currentContext] graphicsPort];
   else
        cg_context = [[NSGraphicsContext currentContext] CGContext];
+  if (!cg_context)
+    return;
   CGContextSaveGState (cg_context);
   CGContextSetAllowsAntialiasing (cg_context, antialias);
 
@@ -272,6 +274,10 @@ gdk_window_impl_quartz_begin_paint_region (GdkPaintable    *paintable,
       gint i;
 
       cg_context = gdk_quartz_drawable_get_context (GDK_DRAWABLE (impl), FALSE);
+
+      if (!cg_context)
+        goto done;
+
       color = _gdk_quartz_colormap_get_cgcolor_from_pixel (window,
                                                            private->bg_color.pixel);
       CGContextSetFillColorWithColor (cg_context, color);


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