[cairo] [PATCH] src: check the surface backend for NULL

Ravi Nanjundappa nravi.n at samsung.com
Tue Sep 23 23:03:45 PDT 2014


This is a follow-up patch on top of 150c1e7044c57443d458e12bfc427d3a019cb60b
As discussed in the mailing list, http://lists.cairographics.org/archives/cairo/2014-September/025647.html,
check if the surfaces are of particular backend type or not, before proceeding further.

These changes are based on _cairo_surface_is_xlib() and _cairo_surface_is_image()

Signed-off-by: Ravi Nanjundappa <nravi.n at samsung.com>
---
 src/cairo-qt-surface.cpp         |   31 +++++++++++++++++++++++++++----
 src/cairo-quartz-image-surface.c |    7 +++++--
 src/cairo-quartz-surface.c       |   14 ++++++++++++++
 src/win32/cairo-win32-surface.c  |   22 ++++++++++++++++++++--
 4 files changed, 66 insertions(+), 8 deletions(-)

diff --git a/src/cairo-qt-surface.cpp b/src/cairo-qt-surface.cpp
index d1c7760..c0719d8 100644
--- a/src/cairo-qt-surface.cpp
+++ b/src/cairo-qt-surface.cpp
@@ -1660,13 +1660,30 @@ cairo_qt_surface_create_with_qpixmap (cairo_content_t content,
     return &qs->base;
 }
 
+/**
+ * _cairo_surface_is_qt:
+ * @surface: a #cairo_surface_t
+ *
+ * Checks if a surface is a #cairo_qt_surface_t
+ *
+ * Return value: True if the surface is an qt surface
+ **/
+static inline cairo_bool_t
+_cairo_surface_is_qt (cairo_surface_t *surface)
+{
+    return surface->backend == &cairo_qt_surface_backend;
+}
+
 QPainter *
 cairo_qt_surface_get_qpainter (cairo_surface_t *surface)
 {
     cairo_qt_surface_t *qs = (cairo_qt_surface_t*) surface;
 
-    if (surface->type != CAIRO_SURFACE_TYPE_QT)
+    /* Throw an error for a non-qt surface */
+    if (! _cairo_surface_is_qt (surface)) {
+        _cairo_error_throw (CAIRO_STATUS_SURFACE_TYPE_MISMATCH);
         return NULL;
+    }
 
     return qs->p;
 }
@@ -1676,8 +1693,11 @@ cairo_qt_surface_get_qimage (cairo_surface_t *surface)
 {
     cairo_qt_surface_t *qs = (cairo_qt_surface_t*) surface;
 
-    if (surface->type != CAIRO_SURFACE_TYPE_QT)
+    /* Throw an error for a non-qt surface */
+    if (! _cairo_surface_is_qt (surface)) {
+        _cairo_error_throw (CAIRO_STATUS_SURFACE_TYPE_MISMATCH);
         return NULL;
+    }
 
     return qs->image;
 }
@@ -1687,8 +1707,11 @@ cairo_qt_surface_get_image (cairo_surface_t *surface)
 {
     cairo_qt_surface_t *qs = (cairo_qt_surface_t*) surface;
 
-    if (surface->type != CAIRO_SURFACE_TYPE_QT)
-        return _cairo_surface_create_in_error (_cairo_error (CAIRO_STATUS_SURFACE_TYPE_MISMATCH));
+    /* Throw an error for a non-qt surface */
+    if (! _cairo_surface_is_qt (surface)) {
+        _cairo_error_throw (CAIRO_STATUS_SURFACE_TYPE_MISMATCH);
+        return NULL;
+    }
 
     return qs->image_equiv;
 }
diff --git a/src/cairo-quartz-image-surface.c b/src/cairo-quartz-image-surface.c
index 511b634..83b3f74 100644
--- a/src/cairo-quartz-image-surface.c
+++ b/src/cairo-quartz-image-surface.c
@@ -378,8 +378,11 @@ cairo_quartz_image_surface_get_image (cairo_surface_t *asurface)
 {
     cairo_quartz_image_surface_t *surface = (cairo_quartz_image_surface_t*) asurface;
 
-    if (asurface->type != CAIRO_SURFACE_TYPE_QUARTZ_IMAGE)
-        return _cairo_surface_create_in_error (_cairo_error (CAIRO_STATUS_SURFACE_TYPE_MISMATCH));
+    /* Throw an error for a non-quartz surface */
+    if (! _cairo_surface_is_quartz (surface)) {
+        _cairo_error_throw (CAIRO_STATUS_SURFACE_TYPE_MISMATCH);
+        return NULL;
+    }
 
     return (cairo_surface_t*) surface->imageSurface;
 }
diff --git a/src/cairo-quartz-surface.c b/src/cairo-quartz-surface.c
index 868c6c8..2aa83be 100644
--- a/src/cairo-quartz-surface.c
+++ b/src/cairo-quartz-surface.c
@@ -2195,6 +2195,20 @@ _cairo_quartz_surface_clipper_intersect_clip_path (cairo_surface_clipper_t *clip
     return CAIRO_STATUS_SUCCESS;
 }
 
+/**
+ * _cairo_surface_is_quartz:
+ * @surface: a #cairo_surface_t
+ *
+ * Checks if a surface is a #cairo_quartz_surface_t
+ *
+ * Return value: True if the surface is an quartz surface
+ **/
+inline cairo_bool_t
+_cairo_surface_is_quartz (cairo_surface_t *surface)
+{
+    return surface->backend == &cairo_quartz_surface_backend;
+}
+
 // XXXtodo implement show_page; need to figure out how to handle begin/end
 
 static const struct _cairo_surface_backend cairo_quartz_surface_backend = {
diff --git a/src/win32/cairo-win32-surface.c b/src/win32/cairo-win32-surface.c
index f11cbd8..e424b15 100644
--- a/src/win32/cairo-win32-surface.c
+++ b/src/win32/cairo-win32-surface.c
@@ -172,6 +172,21 @@ cairo_win32_surface_get_dc (cairo_surface_t *surface)
 }
 
 /**
+ * _cairo_surface_is_win32:
+ * @surface: a #cairo_surface_t
+ *
+ * Checks if a surface is an #cairo_win32_surface_t
+ *
+ * Return value: %TRUE if the surface is an win32 surface
+ **/
+static inline cairo_bool_t
+_cairo_surface_is_win32 (const cairo_surface_t *surface)
+{
+    /* _cairo_surface_nil sets a NULL backend so be safe */
+    return surface->backend && surface->backend->type == CAIRO_SURFACE_TYPE_WIN32;
+}
+
+/**
  * cairo_win32_surface_get_image:
  * @surface: a #cairo_surface_t
  *
@@ -187,8 +202,11 @@ cairo_win32_surface_get_dc (cairo_surface_t *surface)
 cairo_surface_t *
 cairo_win32_surface_get_image (cairo_surface_t *surface)
 {
-    if (surface->backend->type != CAIRO_SURFACE_TYPE_WIN32)
-        return _cairo_surface_create_in_error (_cairo_error (CAIRO_STATUS_SURFACE_TYPE_MISMATCH));
+
+    if (! _cairo_surface_is_win32 (surface)) {
+        _cairo_error_throw (CAIRO_STATUS_SURFACE_TYPE_MISMATCH);
+        return NULL;
+    }
 
     GdiFlush();
     return to_win32_display_surface(surface)->image;
-- 
1.7.9.5



More information about the cairo mailing list