From b466e068b99c71acd26ca10b8e70ad34c7e34881 Mon Sep 17 00:00:00 2001 From: Carl Worth Date: Wed, 1 Oct 2003 17:34:19 +0000 Subject: [PATCH] Fix to gracefully handle a NULL pointer for gstate->font --- ChangeLog | 15 +++++++++++++++ src/cairo-font.c | 44 +++++++++++++++++++++++++++++++++++--------- src/cairo-gstate.c | 12 +++++++----- src/cairo_font.c | 44 +++++++++++++++++++++++++++++++++++--------- src/cairo_gstate.c | 12 +++++++----- src/cairoint.h | 17 +++++++++-------- 6 files changed, 108 insertions(+), 36 deletions(-) diff --git a/ChangeLog b/ChangeLog index 71cc76e6a..edca9550e 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,18 @@ +2003-10-01 Carl Worth + + * src/cairo_gstate.c (_cairo_gstate_init_copy): Don't choke if + asked to copy a gstate with a NULL font. + + * src/cairo_font.c (_cairo_font_init): + (_cairo_font_init_copy): + (_cairo_font_copy): + (_cairo_font_fini): + (_cairo_font_select): + (_cairo_font_scale): + (_cairo_font_transform): + (_cairo_font_text_extents): + (_cairo_font_show_text): Return immediately if passed a NULL pointer. + 2003-09-30 Jamey Sharp * src/Makefile.am, src/cairo.c, src/cairo.h, src/cairo_font.c, diff --git a/src/cairo-font.c b/src/cairo-font.c index 66255fd6c..cdbe37548 100644 --- a/src/cairo-font.c +++ b/src/cairo-font.c @@ -27,18 +27,26 @@ #include "cairoint.h" -void +cairo_int_status_t _cairo_font_init (cairo_font_t *font, const struct cairo_font_backend *backend) { + if (font == NULL) + return CAIRO_INT_STATUS_NULL_POINTER; + font->key = (unsigned char *) strdup (CAIRO_FONT_KEY_DEFAULT); cairo_matrix_set_identity (&font->matrix); font->backend = backend; + + return CAIRO_STATUS_SUCCESS; } -cairo_status_t +cairo_int_status_t _cairo_font_init_copy (cairo_font_t *font, cairo_font_t *other) { + if (other == NULL) + return CAIRO_INT_STATUS_NULL_POINTER; + if (other->key) { font->key = (unsigned char *) strdup ((char *) other->key); if (font->key == NULL) @@ -54,8 +62,8 @@ _cairo_font_init_copy (cairo_font_t *font, cairo_font_t *other) cairo_font_t * _cairo_font_copy (cairo_font_t *font) { - if (!font->backend->copy) - return 0; + if (font == NULL || font->backend->copy == NULL) + return NULL; return font->backend->copy (font); } @@ -63,6 +71,9 @@ _cairo_font_copy (cairo_font_t *font) void _cairo_font_fini (cairo_font_t *font) { + if (font == NULL) + return; + if (font->key) free (font->key); font->key = NULL; @@ -73,9 +84,12 @@ _cairo_font_fini (cairo_font_t *font) font->backend->close (font); } -cairo_status_t +cairo_int_status_t _cairo_font_select (cairo_font_t *font, const char *key) { + if (font == NULL) + return CAIRO_INT_STATUS_NULL_POINTER; + if (font->backend->close) font->backend->close (font); @@ -89,28 +103,34 @@ _cairo_font_select (cairo_font_t *font, const char *key) return CAIRO_STATUS_SUCCESS; } -cairo_status_t +cairo_int_status_t _cairo_font_scale (cairo_font_t *font, double scale) { + if (font == NULL) + return CAIRO_INT_STATUS_NULL_POINTER; + cairo_matrix_scale (&font->matrix, scale, scale); return CAIRO_STATUS_SUCCESS; } -cairo_status_t +cairo_int_status_t _cairo_font_transform (cairo_font_t *font, double a, double b, double c, double d) { cairo_matrix_t m; + if (font == NULL) + return CAIRO_INT_STATUS_NULL_POINTER; + cairo_matrix_set_affine (&m, a, b, c, d, 0, 0); cairo_matrix_multiply (&font->matrix, &m, &font->matrix); return CAIRO_STATUS_SUCCESS; } -cairo_status_t +cairo_int_status_t _cairo_font_text_extents (cairo_font_t *font, cairo_matrix_t *ctm, const unsigned char *utf8, @@ -118,13 +138,16 @@ _cairo_font_text_extents (cairo_font_t *font, double *width, double *height, double *dx, double *dy) { + if (font == NULL) + return CAIRO_INT_STATUS_NULL_POINTER; + if (!font->backend->text_extents) return CAIRO_STATUS_SUCCESS; return font->backend->text_extents (font, ctm, utf8, x, y, width, height, dx, dy); } -cairo_status_t +cairo_int_status_t _cairo_font_show_text (cairo_font_t *font, cairo_matrix_t *ctm, cairo_operator_t operator, @@ -134,6 +157,9 @@ _cairo_font_show_text (cairo_font_t *font, double y, const unsigned char *utf8) { + if (font == NULL) + return CAIRO_INT_STATUS_NULL_POINTER; + if (!font->backend->show_text) return CAIRO_STATUS_SUCCESS; diff --git a/src/cairo-gstate.c b/src/cairo-gstate.c index 42d8f9874..73d95e440 100644 --- a/src/cairo-gstate.c +++ b/src/cairo-gstate.c @@ -120,11 +120,13 @@ _cairo_gstate_init_copy (cairo_gstate_t *gstate, cairo_gstate_t *other) return CAIRO_STATUS_NO_MEMORY; memcpy (gstate->dash, other->dash, other->num_dashes * sizeof (double)); } - - gstate->font = _cairo_font_copy (other->font); - if (!gstate->font) { - status = CAIRO_STATUS_NO_MEMORY; - goto CLEANUP_DASHES; + + if (other->font) { + gstate->font = _cairo_font_copy (other->font); + if (!gstate->font) { + status = CAIRO_STATUS_NO_MEMORY; + goto CLEANUP_DASHES; + } } cairo_surface_reference (gstate->surface); diff --git a/src/cairo_font.c b/src/cairo_font.c index 66255fd6c..cdbe37548 100644 --- a/src/cairo_font.c +++ b/src/cairo_font.c @@ -27,18 +27,26 @@ #include "cairoint.h" -void +cairo_int_status_t _cairo_font_init (cairo_font_t *font, const struct cairo_font_backend *backend) { + if (font == NULL) + return CAIRO_INT_STATUS_NULL_POINTER; + font->key = (unsigned char *) strdup (CAIRO_FONT_KEY_DEFAULT); cairo_matrix_set_identity (&font->matrix); font->backend = backend; + + return CAIRO_STATUS_SUCCESS; } -cairo_status_t +cairo_int_status_t _cairo_font_init_copy (cairo_font_t *font, cairo_font_t *other) { + if (other == NULL) + return CAIRO_INT_STATUS_NULL_POINTER; + if (other->key) { font->key = (unsigned char *) strdup ((char *) other->key); if (font->key == NULL) @@ -54,8 +62,8 @@ _cairo_font_init_copy (cairo_font_t *font, cairo_font_t *other) cairo_font_t * _cairo_font_copy (cairo_font_t *font) { - if (!font->backend->copy) - return 0; + if (font == NULL || font->backend->copy == NULL) + return NULL; return font->backend->copy (font); } @@ -63,6 +71,9 @@ _cairo_font_copy (cairo_font_t *font) void _cairo_font_fini (cairo_font_t *font) { + if (font == NULL) + return; + if (font->key) free (font->key); font->key = NULL; @@ -73,9 +84,12 @@ _cairo_font_fini (cairo_font_t *font) font->backend->close (font); } -cairo_status_t +cairo_int_status_t _cairo_font_select (cairo_font_t *font, const char *key) { + if (font == NULL) + return CAIRO_INT_STATUS_NULL_POINTER; + if (font->backend->close) font->backend->close (font); @@ -89,28 +103,34 @@ _cairo_font_select (cairo_font_t *font, const char *key) return CAIRO_STATUS_SUCCESS; } -cairo_status_t +cairo_int_status_t _cairo_font_scale (cairo_font_t *font, double scale) { + if (font == NULL) + return CAIRO_INT_STATUS_NULL_POINTER; + cairo_matrix_scale (&font->matrix, scale, scale); return CAIRO_STATUS_SUCCESS; } -cairo_status_t +cairo_int_status_t _cairo_font_transform (cairo_font_t *font, double a, double b, double c, double d) { cairo_matrix_t m; + if (font == NULL) + return CAIRO_INT_STATUS_NULL_POINTER; + cairo_matrix_set_affine (&m, a, b, c, d, 0, 0); cairo_matrix_multiply (&font->matrix, &m, &font->matrix); return CAIRO_STATUS_SUCCESS; } -cairo_status_t +cairo_int_status_t _cairo_font_text_extents (cairo_font_t *font, cairo_matrix_t *ctm, const unsigned char *utf8, @@ -118,13 +138,16 @@ _cairo_font_text_extents (cairo_font_t *font, double *width, double *height, double *dx, double *dy) { + if (font == NULL) + return CAIRO_INT_STATUS_NULL_POINTER; + if (!font->backend->text_extents) return CAIRO_STATUS_SUCCESS; return font->backend->text_extents (font, ctm, utf8, x, y, width, height, dx, dy); } -cairo_status_t +cairo_int_status_t _cairo_font_show_text (cairo_font_t *font, cairo_matrix_t *ctm, cairo_operator_t operator, @@ -134,6 +157,9 @@ _cairo_font_show_text (cairo_font_t *font, double y, const unsigned char *utf8) { + if (font == NULL) + return CAIRO_INT_STATUS_NULL_POINTER; + if (!font->backend->show_text) return CAIRO_STATUS_SUCCESS; diff --git a/src/cairo_gstate.c b/src/cairo_gstate.c index 42d8f9874..73d95e440 100644 --- a/src/cairo_gstate.c +++ b/src/cairo_gstate.c @@ -120,11 +120,13 @@ _cairo_gstate_init_copy (cairo_gstate_t *gstate, cairo_gstate_t *other) return CAIRO_STATUS_NO_MEMORY; memcpy (gstate->dash, other->dash, other->num_dashes * sizeof (double)); } - - gstate->font = _cairo_font_copy (other->font); - if (!gstate->font) { - status = CAIRO_STATUS_NO_MEMORY; - goto CLEANUP_DASHES; + + if (other->font) { + gstate->font = _cairo_font_copy (other->font); + if (!gstate->font) { + status = CAIRO_STATUS_NO_MEMORY; + goto CLEANUP_DASHES; + } } cairo_surface_reference (gstate->surface); diff --git a/src/cairoint.h b/src/cairoint.h index d2eacb4cd..e9da4471a 100644 --- a/src/cairoint.h +++ b/src/cairoint.h @@ -128,7 +128,8 @@ typedef struct cairo_rectangle_int { from cairo_status_t. Oh well, without that, I'll use this bogus 1000 offset */ typedef enum cairo_int_status { - CAIRO_INT_STATUS_DEGENERATE = 1000 + CAIRO_INT_STATUS_DEGENERATE = 1000, + CAIRO_INT_STATUS_NULL_POINTER } cairo_int_status_t; typedef enum cairo_path_op { @@ -673,10 +674,10 @@ extern void __internal_linkage _cairo_color_set_alpha (cairo_color_t *color, double alpha); /* cairo_font.c */ -extern void __internal_linkage +extern cairo_int_status_t __internal_linkage _cairo_font_init (cairo_font_t *font, const struct cairo_font_backend *backend); -extern cairo_status_t __internal_linkage +extern cairo_int_status_t __internal_linkage _cairo_font_init_copy (cairo_font_t *font, cairo_font_t *other); extern cairo_font_t * __internal_linkage @@ -685,18 +686,18 @@ _cairo_font_copy (cairo_font_t *font); extern void __internal_linkage _cairo_font_fini (cairo_font_t *font); -extern cairo_status_t __internal_linkage +extern cairo_int_status_t __internal_linkage _cairo_font_select (cairo_font_t *font, const char *key); -extern cairo_status_t __internal_linkage +extern cairo_int_status_t __internal_linkage _cairo_font_scale (cairo_font_t *font, double scale); -extern cairo_status_t __internal_linkage +extern cairo_int_status_t __internal_linkage _cairo_font_transform (cairo_font_t *font, double a, double b, double c, double d); -extern cairo_status_t __internal_linkage +extern cairo_int_status_t __internal_linkage _cairo_font_text_extents (cairo_font_t *font, cairo_matrix_t *ctm, const unsigned char *utf8, @@ -704,7 +705,7 @@ _cairo_font_text_extents (cairo_font_t *font, double *width, double *height, double *dx, double *dy); -extern cairo_status_t __internal_linkage +extern cairo_int_status_t __internal_linkage _cairo_font_show_text (cairo_font_t *font, cairo_matrix_t *ctm, cairo_operator_t operator,