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

Bryce Harrington bryce at osg.samsung.com
Mon Sep 29 16:00:08 PDT 2014


On Thu, Sep 25, 2014 at 08:38:50AM +0530, Ravi Nanjundappa wrote:
> 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()

Thanks, applied.
 
> Signed-off-by: Ravi Nanjundappa <nravi.n at samsung.com>
> ---
>  src/cairo-qt-surface.cpp         |   28 +++++++++++++++++++++++++---
>  src/cairo-quartz-image-surface.c |    4 +++-
>  src/cairo-quartz-surface.c       |   14 ++++++++++++++
>  src/win32/cairo-win32-surface.c  |   19 ++++++++++++++++++-
>  4 files changed, 60 insertions(+), 5 deletions(-)
> 
> diff --git a/src/cairo-qt-surface.cpp b/src/cairo-qt-surface.cpp
> index d1c7760..7ddad77 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,10 @@ 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)
> +    /* Throw an error for a non-qt surface */
> +    if (! _cairo_surface_is_qt (surface)) {
>          return _cairo_surface_create_in_error (_cairo_error (CAIRO_STATUS_SURFACE_TYPE_MISMATCH));
> +    }
>  
>      return qs->image_equiv;
>  }
> diff --git a/src/cairo-quartz-image-surface.c b/src/cairo-quartz-image-surface.c
> index 511b634..b4bc8b9 100644
> --- a/src/cairo-quartz-image-surface.c
> +++ b/src/cairo-quartz-image-surface.c
> @@ -378,8 +378,10 @@ 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)
> +    /* Throw an error for a non-quartz surface */
> +    if (! _cairo_surface_is_quartz (surface)) {
>          return _cairo_surface_create_in_error (_cairo_error (CAIRO_STATUS_SURFACE_TYPE_MISMATCH));
> +    }
>  
>      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..e6862bd 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,10 @@ 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)
> +
> +    if (! _cairo_surface_is_win32 (surface)) {
>          return _cairo_surface_create_in_error (_cairo_error (CAIRO_STATUS_SURFACE_TYPE_MISMATCH));
> +    }
>  
>      GdiFlush();
>      return to_win32_display_surface(surface)->image;
> -- 
> 1.7.9.5
> 
> -- 
> cairo mailing list
> cairo at cairographics.org
> http://lists.cairographics.org/mailman/listinfo/cairo


More information about the cairo mailing list