From b4f86638cc4b87bfaf10568ae9beb89626e26613 Mon Sep 17 00:00:00 2001 From: Chris Wilson Date: Mon, 1 Oct 2007 17:59:57 +0100 Subject: [PATCH] [cairo-path] Don't raise an error when attempting to create an empty path. Generate a real empty path structure instead of returning _cairo_path_nil, if we have been asked to create an empty path. (Also add a couple of missing _cairo_error()s and an appropriate test case.) Spotted by Fred Kiefer. --- src/cairo-path.c | 36 ++++++++++++++++++++++++------------ test/copy-path.c | 17 +++++++++++++++++ 2 files changed, 41 insertions(+), 12 deletions(-) diff --git a/src/cairo-path.c b/src/cairo-path.c index 0740ebc4c..b1ef44e27 100644 --- a/src/cairo-path.c +++ b/src/cairo-path.c @@ -352,8 +352,10 @@ _cairo_path_create_in_error (cairo_status_t status) return (cairo_path_t*) &_cairo_path_nil; path = malloc (sizeof (cairo_path_t)); - if (path == NULL) + if (path == NULL) { + _cairo_error (CAIRO_STATUS_NO_MEMORY); return (cairo_path_t*) &_cairo_path_nil; + } path->num_data = 0; path->data = NULL; @@ -370,25 +372,34 @@ _cairo_path_create_internal (cairo_path_fixed_t *path_fixed, cairo_path_t *path; path = malloc (sizeof (cairo_path_t)); - if (path == NULL) + if (path == NULL) { + _cairo_error (CAIRO_STATUS_NO_MEMORY); return (cairo_path_t*) &_cairo_path_nil; + } path->num_data = _cairo_path_count (path, path_fixed, _cairo_gstate_get_tolerance (gstate), flatten); - if (path->num_data <= 0) { + if (path->num_data < 0) { free (path); return (cairo_path_t*) &_cairo_path_nil; } - path->data = _cairo_malloc_ab (path->num_data, sizeof (cairo_path_data_t)); - if (path->data == NULL) { - free (path); - return (cairo_path_t*) &_cairo_path_nil; - } + if (path->num_data) { + path->data = _cairo_malloc_ab (path->num_data, + sizeof (cairo_path_data_t)); + if (path->data == NULL) { + free (path); + _cairo_error (CAIRO_STATUS_NO_MEMORY); + return (cairo_path_t*) &_cairo_path_nil; + } - path->status = _cairo_path_populate (path, path_fixed, - gstate, flatten); + path->status = _cairo_path_populate (path, path_fixed, + gstate, flatten); + } else { + path->data = NULL; + path->status = CAIRO_STATUS_SUCCESS; + } return path; } @@ -413,8 +424,9 @@ cairo_path_destroy (cairo_path_t *path) if (path == NULL || path == &_cairo_path_nil) return; - free (path->data); - path->num_data = 0; + if (path->data) + free (path->data); + free (path); } diff --git a/test/copy-path.c b/test/copy-path.c index 142bfa57d..256f46123 100644 --- a/test/copy-path.c +++ b/test/copy-path.c @@ -125,6 +125,23 @@ draw (cairo_t *cr, int width, int height) cairo_path_destroy (path); cairo_destroy (cr_error); + + /* first check that we can copy an empty path */ + cairo_new_path (cr); + path = cairo_copy_path (cr); + if (path->status != CAIRO_STATUS_SUCCESS) { + cairo_test_log ("Error: cairo_copy_path returned status of %s\n", + cairo_status_to_string (path->status)); + cairo_path_destroy (path); + return CAIRO_TEST_FAILURE; + } + if (path->num_data != 0) { + cairo_test_log ("Error: cairo_copy_path did not copy an empty path, returned path contains %d elements\n", + path->num_data); + cairo_path_destroy (path); + return CAIRO_TEST_FAILURE; + } + cairo_path_destroy (path); /* We draw in the default black, so paint white first. */ cairo_save (cr);