diff --git a/gdk/gdkwindow.c b/gdk/gdkwindow.c
index 71405f3..b593ba3 100644
--- a/gdk/gdkwindow.c
+++ b/gdk/gdkwindow.c
@@ -3538,6 +3538,9 @@ gdk_window_create_cairo_surface (GdkWindow *window,
   if (gdk_window_has_impl (window))
     return surface;
 
+  if (!surface)
+      return NULL;
+
   subsurface = cairo_surface_create_for_rectangle (surface,
                                                    window->abs_x,
                                                    window->abs_y,
diff --git a/gdk/quartz/gdkwindow-quartz.c b/gdk/quartz/gdkwindow-quartz.c
index ebfd795..df0e9e5 100644
--- a/gdk/quartz/gdkwindow-quartz.c
+++ b/gdk/quartz/gdkwindow-quartz.c
@@ -148,6 +148,8 @@ gdk_window_impl_quartz_get_context (GdkWindowImplQuartz *window_impl,
 
       n_rects = cairo_region_num_rectangles (window_impl->paint_clip_region);
 
+      if (!n_rects)
+	return NULL;
       if (n_rects == 1)
 	cg_rects = &rect;
       else
