Opensource Compliance Issue.
[platform/core/graphics/cairo.git] / test / clip-double-free.c
1 /*
2  * Copyright © 2011 Uli Schlachter
3  *
4  * Permission is hereby granted, free of charge, to any person
5  * obtaining a copy of this software and associated documentation
6  * files (the "Software"), to deal in the Software without
7  * restriction, including without limitation the rights to use, copy,
8  * modify, merge, publish, distribute, sublicense, and/or sell copies
9  * of the Software, and to permit persons to whom the Software is
10  * furnished to do so, subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice shall be
13  * included in all copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
19  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
20  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22  * SOFTWARE.
23  *
24  * Author: Uli Schlachter <psychon@znc.in>
25  */
26
27 /*
28  * This test wants to hit the following double free:
29  *
30  * ==10517== Invalid free() / delete / delete[]
31  * ==10517==    at 0x4C268FE: free (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
32  * ==10517==    by 0x87FBE80: _cairo_clip_destroy (cairo-clip.c:136)
33  * ==10517==    by 0x87FE520: _cairo_clip_intersect_boxes.part.1 (cairo-clip-private.h:92)
34  * ==10517==    by 0x87FE79F: _cairo_clip_intersect_rectilinear_path (cairo-clip-boxes.c:266)
35  * ==10517==    by 0x87FC29B: _cairo_clip_intersect_path.part.3 (cairo-clip.c:242)
36  * ==10517==    by 0x8809C3A: _cairo_gstate_clip (cairo-gstate.c:1518)
37  * ==10517==    by 0x8802E40: _cairo_default_context_clip (cairo-default-context.c:1048)
38  * ==10517==    by 0x87FA2C6: cairo_clip (cairo.c:2380)
39  * ==10517==  Address 0x18d44cb0 is 0 bytes inside a block of size 32 free'd
40  * ==10517==    at 0x4C268FE: free (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
41  * ==10517==    by 0x87FE506: _cairo_clip_intersect_boxes.part.1 (cairo-clip-boxes.c:295)
42  * ==10517==    by 0x87FE79F: _cairo_clip_intersect_rectilinear_path (cairo-clip-boxes.c:266)
43  * ==10517==    by 0x87FC29B: _cairo_clip_intersect_path.part.3 (cairo-clip.c:242)
44  * ==10517==    by 0x8809C3A: _cairo_gstate_clip (cairo-gstate.c:1518)
45  * ==10517==    by 0x8802E40: _cairo_default_context_clip (cairo-default-context.c:1048)
46  * ==10517==    by 0x87FA2C6: cairo_clip (cairo.c:2380)
47  *
48  * _cairo_clip_intersect_boxes() is called with clip->num_boxes != 0. It then
49  * calls _cairo_boxes_init_for_array (&clip_boxes, clip->boxes, clip->num_boxes)
50  * and free (clip->boxes), stealing the clip's boxes, but leaving a dangling
51  * pointer behind.
52  * Because this code already intersected the existing boxes and the new ones, we
53  * now have num_boxes == 0. This means that _cairo_clip_set_all_clipped() gets
54  * called and tries to free the clip's boxes again.
55  */
56
57 #include "cairo-test.h"
58
59 static cairo_test_status_t
60 draw (cairo_t *cr, int width, int height)
61 {
62     cairo_set_fill_rule (cr, CAIRO_FILL_RULE_EVEN_ODD);
63
64     /* To hit this bug, we first need a clip with
65      * clip->boxes != clip->embedded_boxes.
66      */
67     cairo_rectangle (cr, 0, 0, 2, 2);
68     cairo_rectangle (cr, 0, 0, 1, 1);
69     cairo_clip (cr);
70
71     /* Then we have to intersect this with a rectilinear path which results in
72      * all clipped. This path must consist of at least two boxes or we will hit
73      * a different code path.
74      */
75     cairo_rectangle (cr, 10, 10, 2, 2);
76     cairo_rectangle (cr, 10, 10, 1, 1);
77     cairo_clip (cr);
78
79     return CAIRO_TEST_SUCCESS;
80 }
81
82 CAIRO_TEST (clip_double_free,
83             "Test a double free bug in the clipping code",
84             "clip", /* keywords */
85             NULL, /* requirements */
86             0, 0,
87             NULL, draw)