[PATCH] [cairo-pattern] Use cairo_pattern_status() in preference over open coding.

Chris Wilson chris at chris-wilson.co.uk
Fri Feb 22 10:59:54 PST 2008


Use cairo_pattern_status() in guards in preference to pattern->status
as this can prevent potential NULL dereferences.
---
 src/cairo-gstate.c  |   38 ++++++++++------
 src/cairo-pattern.c |  122 ++++++++++++++++++++++++++++++++++++++------------
 src/cairo-surface.c |    5 +-
 src/cairo.c         |   26 ++++-------
 4 files changed, 128 insertions(+), 63 deletions(-)

diff --git a/src/cairo-gstate.c b/src/cairo-gstate.c
index 5049399..549d37c 100644
--- a/src/cairo-gstate.c
+++ b/src/cairo-gstate.c
@@ -431,8 +431,11 @@ cairo_status_t
 _cairo_gstate_set_source (cairo_gstate_t  *gstate,
 			  cairo_pattern_t *source)
 {
-    if (source->status)
-	return source->status;
+    cairo_status_t status;
+
+    status = cairo_pattern_status (source);
+    if (status)
+	return status;
 
     source = cairo_pattern_reference (source);
     cairo_pattern_destroy (gstate->source);
@@ -880,8 +883,9 @@ _cairo_gstate_paint (cairo_gstate_t *gstate)
     cairo_status_t status;
     cairo_pattern_union_t pattern;
 
-    if (gstate->source->status)
-	return gstate->source->status;
+    status = gstate->source->status;
+    if (status)
+	return status;
 
     status = _cairo_surface_set_clip (gstate->target, &gstate->clip);
     if (status)
@@ -907,11 +911,13 @@ _cairo_gstate_mask (cairo_gstate_t  *gstate,
     cairo_status_t status;
     cairo_pattern_union_t source_pattern, mask_pattern;
 
-    if (mask->status)
-	return mask->status;
+    status = mask->status;
+    if (status)
+	return status;
 
-    if (gstate->source->status)
-	return gstate->source->status;
+    status = gstate->source->status;
+    if (status)
+	return status;
 
     status = _cairo_surface_set_clip (gstate->target, &gstate->clip);
     if (status)
@@ -943,8 +949,9 @@ _cairo_gstate_stroke (cairo_gstate_t *gstate, cairo_path_fixed_t *path)
     cairo_status_t status;
     cairo_pattern_union_t source_pattern;
 
-    if (gstate->source->status)
-	return gstate->source->status;
+    status = gstate->source->status;
+    if (status)
+	return status;
 
     if (gstate->stroke_style.line_width <= 0.0)
 	return CAIRO_STATUS_SUCCESS;
@@ -1016,8 +1023,9 @@ _cairo_gstate_fill (cairo_gstate_t *gstate, cairo_path_fixed_t *path)
     cairo_status_t status;
     cairo_pattern_union_t pattern;
 
-    if (gstate->source->status)
-	return gstate->source->status;
+    status = gstate->source->status;
+    if (status)
+	return status;
 
     status = _cairo_surface_set_clip (gstate->target, &gstate->clip);
     if (status)
@@ -1424,7 +1432,6 @@ _cairo_gstate_ensure_font_face (cairo_gstate_t *gstate)
     if (gstate->font_face != NULL)
 	return gstate->font_face->status;
 
-
     font_face = _cairo_toy_font_face_create (CAIRO_FONT_FAMILY_DEFAULT,
 					     CAIRO_FONT_SLANT_DEFAULT,
 					     CAIRO_FONT_WEIGHT_DEFAULT);
@@ -1544,8 +1551,9 @@ _cairo_gstate_show_glyphs (cairo_gstate_t *gstate,
     cairo_glyph_t *transformed_glyphs;
     cairo_glyph_t stack_transformed_glyphs[CAIRO_STACK_ARRAY_LENGTH (cairo_glyph_t)];
 
-    if (gstate->source->status)
-	return gstate->source->status;
+    status = gstate->source->status;
+    if (status)
+	return status;
 
     status = _cairo_surface_set_clip (gstate->target, &gstate->clip);
     if (status)
diff --git a/src/cairo-pattern.c b/src/cairo-pattern.c
index 22a1a9e..f196c47 100644
--- a/src/cairo-pattern.c
+++ b/src/cairo-pattern.c
@@ -31,7 +31,7 @@
 #include "cairoint.h"
 
 const cairo_solid_pattern_t _cairo_pattern_nil = {
-    { CAIRO_PATTERN_TYPE_SOLID, 	/* type */
+    { CAIRO_PATTERN_TYPE_SOLID,		/* type */
       CAIRO_REFERENCE_COUNT_INVALID,	/* ref_count */
       CAIRO_STATUS_NO_MEMORY,		/* status */
       { 0, 0, 0, NULL },		/* user_data */
@@ -41,7 +41,7 @@ const cairo_solid_pattern_t _cairo_pattern_nil = {
 };
 
 static const cairo_solid_pattern_t _cairo_pattern_nil_null_pointer = {
-    { CAIRO_PATTERN_TYPE_SOLID, 	/* type */
+    { CAIRO_PATTERN_TYPE_SOLID,		/* type */
       CAIRO_REFERENCE_COUNT_INVALID,	/* ref_count */
       CAIRO_STATUS_NULL_POINTER,	/* status */
       { 0, 0, 0, NULL },		/* user_data */
@@ -51,7 +51,7 @@ static const cairo_solid_pattern_t _cairo_pattern_nil_null_pointer = {
 };
 
 const cairo_solid_pattern_t cairo_pattern_none = {
-    { CAIRO_PATTERN_TYPE_SOLID, 	/* type */
+    { CAIRO_PATTERN_TYPE_SOLID,		/* type */
       CAIRO_REFERENCE_COUNT_INVALID,	/* ref_count */
       CAIRO_STATUS_SUCCESS,		/* status */
       { 0, 0, 0, NULL },		/* user_data */
@@ -149,8 +149,11 @@ cairo_status_t
 _cairo_pattern_init_copy (cairo_pattern_t	*pattern,
 			  const cairo_pattern_t *other)
 {
-    if (other->status)
-	return _cairo_pattern_set_error (pattern, other->status);
+    cairo_status_t status;
+
+    status = cairo_pattern_status ((cairo_pattern_t *) other);
+    if (status)
+	return _cairo_pattern_set_error (pattern, status);
 
     switch (other->type) {
     case CAIRO_PATTERN_TYPE_SOLID: {
@@ -217,8 +220,9 @@ _cairo_pattern_create_copy (cairo_pattern_t	  **pattern,
 {
     cairo_status_t status;
 
-    if (other->status)
-	return other->status;
+    status = cairo_pattern_status ((cairo_pattern_t *) other);
+    if (status)
+	return status;
 
     switch (other->type) {
     case CAIRO_PATTERN_TYPE_SOLID:
@@ -620,7 +624,7 @@ cairo_pattern_t *
 cairo_pattern_reference (cairo_pattern_t *pattern)
 {
     if (pattern == NULL ||
-	    CAIRO_REFERENCE_COUNT_IS_INVALID (&pattern->ref_count))
+	CAIRO_REFERENCE_COUNT_IS_INVALID (&pattern->ref_count))
 	return pattern;
 
     assert (CAIRO_REFERENCE_COUNT_HAS_REFERENCE (&pattern->ref_count));
@@ -645,6 +649,9 @@ slim_hidden_def (cairo_pattern_reference);
 cairo_pattern_type_t
 cairo_pattern_get_type (cairo_pattern_t *pattern)
 {
+    if (cairo_pattern_status (pattern))
+	return CAIRO_PATTERN_TYPE_SOLID;
+
     return pattern->type;
 }
 slim_hidden_def (cairo_pattern_get_type);
@@ -662,6 +669,9 @@ slim_hidden_def (cairo_pattern_get_type);
 cairo_status_t
 cairo_pattern_status (cairo_pattern_t *pattern)
 {
+    if (pattern == NULL)
+	return CAIRO_STATUS_NULL_POINTER;
+
     return pattern->status;
 }
 slim_hidden_def (cairo_pattern_status);
@@ -678,8 +688,10 @@ void
 cairo_pattern_destroy (cairo_pattern_t *pattern)
 {
     if (pattern == NULL ||
-	    CAIRO_REFERENCE_COUNT_IS_INVALID (&pattern->ref_count))
+	CAIRO_REFERENCE_COUNT_IS_INVALID (&pattern->ref_count))
+    {
 	return;
+    }
 
     assert (CAIRO_REFERENCE_COUNT_HAS_REFERENCE (&pattern->ref_count));
 
@@ -724,7 +736,7 @@ unsigned int
 cairo_pattern_get_reference_count (cairo_pattern_t *pattern)
 {
     if (pattern == NULL ||
-	    CAIRO_REFERENCE_COUNT_IS_INVALID (&pattern->ref_count))
+	CAIRO_REFERENCE_COUNT_IS_INVALID (&pattern->ref_count))
 	return 0;
 
     return CAIRO_REFERENCE_COUNT_GET_VALUE (&pattern->ref_count);
@@ -748,8 +760,7 @@ void *
 cairo_pattern_get_user_data (cairo_pattern_t		 *pattern,
 			     const cairo_user_data_key_t *key)
 {
-    return _cairo_user_data_array_get_data (&pattern->user_data,
-					    key);
+    return _cairo_user_data_array_get_data (&pattern->user_data, key);
 }
 
 /**
@@ -808,7 +819,7 @@ _cairo_pattern_gradient_grow (cairo_gradient_pattern_t *pattern)
 	    memcpy (new_stops, pattern->stops, old_size * sizeof (cairo_gradient_stop_t));
     } else {
 	new_stops = _cairo_realloc_ab (pattern->stops,
-	       	                       new_size,
+				       new_size,
 				       sizeof (cairo_gradient_stop_t));
     }
 
@@ -904,7 +915,7 @@ cairo_pattern_add_color_stop_rgb (cairo_pattern_t *pattern,
 				  double	   green,
 				  double	   blue)
 {
-    if (pattern->status)
+    if (cairo_pattern_status (pattern))
 	return;
 
     if (pattern->type != CAIRO_PATTERN_TYPE_LINEAR &&
@@ -958,7 +969,7 @@ cairo_pattern_add_color_stop_rgba (cairo_pattern_t *pattern,
 				   double	   blue,
 				   double	   alpha)
 {
-    if (pattern->status)
+    if (cairo_pattern_status (pattern))
 	return;
 
     if (pattern->type != CAIRO_PATTERN_TYPE_LINEAR &&
@@ -1017,7 +1028,7 @@ cairo_pattern_set_matrix (cairo_pattern_t      *pattern,
     cairo_matrix_t inverse;
     cairo_status_t status;
 
-    if (pattern->status)
+    if (cairo_pattern_status (pattern))
 	return;
 
     pattern->matrix = *matrix;
@@ -1039,6 +1050,11 @@ slim_hidden_def (cairo_pattern_set_matrix);
 void
 cairo_pattern_get_matrix (cairo_pattern_t *pattern, cairo_matrix_t *matrix)
 {
+    if (cairo_pattern_status (pattern)) {
+	cairo_matrix_init_identity (matrix);
+	return;
+    }
+
     *matrix = pattern->matrix;
 }
 
@@ -1065,7 +1081,7 @@ cairo_pattern_get_matrix (cairo_pattern_t *pattern, cairo_matrix_t *matrix)
 void
 cairo_pattern_set_filter (cairo_pattern_t *pattern, cairo_filter_t filter)
 {
-    if (pattern->status)
+    if (cairo_pattern_status (pattern))
 	return;
 
     pattern->filter = filter;
@@ -1083,6 +1099,9 @@ cairo_pattern_set_filter (cairo_pattern_t *pattern, cairo_filter_t filter)
 cairo_filter_t
 cairo_pattern_get_filter (cairo_pattern_t *pattern)
 {
+    if (cairo_pattern_status (pattern))
+	return (cairo_filter_t) 0;
+
     return pattern->filter;
 }
 
@@ -1099,7 +1118,7 @@ cairo_pattern_get_filter (cairo_pattern_t *pattern)
 void
 cairo_pattern_set_extend (cairo_pattern_t *pattern, cairo_extend_t extend)
 {
-    if (pattern->status)
+    if (cairo_pattern_status (pattern))
 	return;
 
     pattern->extend = extend;
@@ -1118,6 +1137,9 @@ cairo_pattern_set_extend (cairo_pattern_t *pattern, cairo_extend_t extend)
 cairo_extend_t
 cairo_pattern_get_extend (cairo_pattern_t *pattern)
 {
+    if (cairo_pattern_status (pattern))
+	return (cairo_extend_t) 0;
+
     return pattern->extend;
 }
 slim_hidden_def (cairo_pattern_get_extend);
@@ -1126,7 +1148,7 @@ void
 _cairo_pattern_transform (cairo_pattern_t	*pattern,
 			  const cairo_matrix_t  *ctm_inverse)
 {
-    if (pattern->status)
+    if (cairo_pattern_status (pattern))
 	return;
 
     cairo_matrix_multiply (&pattern->matrix, ctm_inverse, &pattern->matrix);
@@ -1767,10 +1789,11 @@ _cairo_pattern_acquire_surface (cairo_pattern_t		   *pattern,
 {
     cairo_status_t status;
 
-    if (pattern->status) {
+    status = cairo_pattern_status (pattern);
+    if (status) {
 	*surface_out = NULL;
 	attributes->acquired = FALSE;
-	return pattern->status;
+	return status;
     }
 
     switch (pattern->type) {
@@ -1894,10 +1917,15 @@ _cairo_pattern_acquire_surfaces (cairo_pattern_t	    *src,
     cairo_int_status_t	  status;
     cairo_pattern_union_t src_tmp, mask_tmp;
 
-    if (src->status)
-	return src->status;
-    if (mask && mask->status)
-	return mask->status;
+    status = cairo_pattern_status (src);
+    if (status)
+	return status;
+
+    if (mask != NULL) {
+	status = cairo_pattern_status (mask);
+	if (status)
+	    return status;
+    }
 
     /* If src and mask are both solid, then the mask alpha can be
      * combined into src and mask can be ignored. */
@@ -1938,8 +1966,7 @@ _cairo_pattern_acquire_surfaces (cairo_pattern_t	    *src,
 	return status;
     }
 
-    if (mask == NULL)
-    {
+    if (mask == NULL) {
 	_cairo_pattern_fini (&src_tmp.base);
 	*mask_out = NULL;
 	return CAIRO_STATUS_SUCCESS;
@@ -1982,10 +2009,15 @@ cairo_status_t
 _cairo_pattern_get_extents (cairo_pattern_t         *pattern,
 			    cairo_rectangle_int_t   *extents)
 {
+    cairo_status_t status;
+
+    status = cairo_pattern_status (pattern);
+    if (status)
+	return status;
+
     if (pattern->extend == CAIRO_EXTEND_NONE &&
 	pattern->type == CAIRO_PATTERN_TYPE_SURFACE)
     {
-	cairo_status_t status;
 	cairo_rectangle_int_t surface_extents;
 	cairo_surface_pattern_t *surface_pattern =
 	    (cairo_surface_pattern_t *) pattern;
@@ -2050,7 +2082,7 @@ _cairo_pattern_get_extents (cairo_pattern_t         *pattern,
 
     extents->x = 0;
     extents->y = 0;
-    extents->width = CAIRO_RECT_INT_MAX;
+    extents->width  = CAIRO_RECT_INT_MAX;
     extents->height = CAIRO_RECT_INT_MAX;
 
     return CAIRO_STATUS_SUCCESS;
@@ -2079,6 +2111,11 @@ cairo_pattern_get_rgba (cairo_pattern_t *pattern,
 {
     cairo_solid_pattern_t *solid = (cairo_solid_pattern_t*) pattern;
     double r0, g0, b0, a0;
+    cairo_status_t status;
+
+    status = cairo_pattern_status (pattern);
+    if (status)
+	return status;
 
     if (pattern->type != CAIRO_PATTERN_TYPE_SOLID)
 	return _cairo_error (CAIRO_STATUS_PATTERN_TYPE_MISMATCH);
@@ -2101,7 +2138,7 @@ cairo_pattern_get_rgba (cairo_pattern_t *pattern,
  * cairo_pattern_get_surface
  * @pattern: a #cairo_pattern_t
  * @surface: return value for surface of pattern, or %NULL
- * 
+ *
  * Gets the surface of a surface pattern.  The reference returned in
  * @surface is owned by the pattern; the caller should call
  * cairo_surface_reference() if the surface is to be retained.
@@ -2117,6 +2154,11 @@ cairo_pattern_get_surface (cairo_pattern_t *pattern,
 			   cairo_surface_t **surface)
 {
     cairo_surface_pattern_t *spat = (cairo_surface_pattern_t*) pattern;
+    cairo_status_t status;
+
+    status = cairo_pattern_status (pattern);
+    if (status)
+	return status;
 
     if (pattern->type != CAIRO_PATTERN_TYPE_SURFACE)
 	return _cairo_error (CAIRO_STATUS_PATTERN_TYPE_MISMATCH);
@@ -2155,6 +2197,11 @@ cairo_pattern_get_color_stop_rgba (cairo_pattern_t *pattern,
 				   double *blue, double *alpha)
 {
     cairo_gradient_pattern_t *gradient = (cairo_gradient_pattern_t*) pattern;
+    cairo_status_t status;
+
+    status = cairo_pattern_status (pattern);
+    if (status)
+	return status;
 
     if (pattern->type != CAIRO_PATTERN_TYPE_LINEAR &&
 	pattern->type != CAIRO_PATTERN_TYPE_RADIAL)
@@ -2196,6 +2243,11 @@ cairo_pattern_get_color_stop_count (cairo_pattern_t *pattern,
 				    int *count)
 {
     cairo_gradient_pattern_t *gradient = (cairo_gradient_pattern_t*) pattern;
+    cairo_status_t status;
+
+    status = cairo_pattern_status (pattern);
+    if (status)
+	return status;
 
     if (pattern->type != CAIRO_PATTERN_TYPE_LINEAR &&
 	pattern->type != CAIRO_PATTERN_TYPE_RADIAL)
@@ -2229,6 +2281,11 @@ cairo_pattern_get_linear_points (cairo_pattern_t *pattern,
 				 double *x1, double *y1)
 {
     cairo_linear_pattern_t *linear = (cairo_linear_pattern_t*) pattern;
+    cairo_status_t status;
+
+    status = cairo_pattern_status (pattern);
+    if (status)
+	return status;
 
     if (pattern->type != CAIRO_PATTERN_TYPE_LINEAR)
 	return _cairo_error (CAIRO_STATUS_PATTERN_TYPE_MISMATCH);
@@ -2270,6 +2327,11 @@ cairo_pattern_get_radial_circles (cairo_pattern_t *pattern,
 				  double *x1, double *y1, double *r1)
 {
     cairo_radial_pattern_t *radial = (cairo_radial_pattern_t*) pattern;
+    cairo_status_t status;
+
+    status = cairo_pattern_status (pattern);
+    if (status)
+	return status;
 
     if (pattern->type != CAIRO_PATTERN_TYPE_RADIAL)
 	return _cairo_error (CAIRO_STATUS_PATTERN_TYPE_MISMATCH);
diff --git a/src/cairo-surface.c b/src/cairo-surface.c
index cb20b2b..5f96d1a 100644
--- a/src/cairo-surface.c
+++ b/src/cairo-surface.c
@@ -322,9 +322,10 @@ _cairo_surface_create_similar_solid (cairo_surface_t	 *other,
 
     if (pattern == NULL) {
 	source = _cairo_pattern_create_solid (color, content);
-	if (source->status) {
+	status = source->status;
+	if (status) {
 	    cairo_surface_destroy (surface);
-	    return _cairo_surface_create_in_error (source->status);
+	    return _cairo_surface_create_in_error (status);
 	}
     } else
 	source = pattern;
diff --git a/src/cairo.c b/src/cairo.c
index 98a5d4f..3ccfe1e 100644
--- a/src/cairo.c
+++ b/src/cairo.c
@@ -546,6 +546,7 @@ cairo_pop_group (cairo_t *cr)
     cairo_surface_t *group_surface, *parent_target;
     cairo_pattern_t *group_pattern = (cairo_pattern_t*) &_cairo_pattern_nil.base;
     cairo_matrix_t group_matrix;
+    cairo_status_t status;
 
     if (cairo_status (cr))
 	return group_pattern;
@@ -571,8 +572,9 @@ cairo_pop_group (cairo_t *cr)
 	goto done;
 
     group_pattern = cairo_pattern_create_for_surface (group_surface);
-    if (cairo_pattern_status (group_pattern)) {
-	_cairo_set_error (cr, cairo_pattern_status (group_pattern));
+    status = cairo_pattern_status (group_pattern);
+    if (status) {
+	_cairo_set_error (cr, status);
         goto done;
     }
 
@@ -790,13 +792,9 @@ cairo_set_source (cairo_t *cr, cairo_pattern_t *source)
     if (cairo_status (cr))
 	return;
 
-    if (source == NULL) {
-	_cairo_set_error (cr, CAIRO_STATUS_NULL_POINTER);
-	return;
-    }
-
-    if (source->status) {
-	_cairo_set_error (cr, source->status);
+    status = cairo_pattern_status (source);
+    if (status) {
+	_cairo_set_error (cr, status);
 	return;
     }
 
@@ -1999,13 +1997,9 @@ cairo_mask (cairo_t         *cr,
     if (cairo_status (cr))
 	return;
 
-    if (pattern == NULL) {
-	_cairo_set_error (cr, CAIRO_STATUS_NULL_POINTER);
-	return;
-    }
-
-    if (pattern->status) {
-	_cairo_set_error (cr, pattern->status);
+    status = cairo_pattern_status (pattern);
+    if (status) {
+	_cairo_set_error (cr, status);
 	return;
     }
 
-- 
1.5.3.8


--=-BzpsUfdHqkE+TbB0ojYn
Content-Disposition: attachment; filename=0053-cairo-surface-Use-cairo_surface_status-instead-o.patch
Content-Type: application/mbox; name=0053-cairo-surface-Use-cairo_surface_status-instead-o.patch
Content-Transfer-Encoding: 7bit



More information about the cairo mailing list