Skip to content

Commit

Permalink
[rectangle] Fix unsigned promotion whilst computing intersect.
Browse files Browse the repository at this point in the history
_cairo_rectangle_intersect() incorrectly allows unsigned promotion during
its arithmetic.
  • Loading branch information
Chris Wilson committed Oct 30, 2008
1 parent 2464b8a commit 0e41561
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 15 deletions.
31 changes: 17 additions & 14 deletions src/cairo-rectangle.c
Original file line number Diff line number Diff line change
Expand Up @@ -96,29 +96,32 @@ _cairo_box_round_to_rectangle (const cairo_box_t *box,
}

void
_cairo_rectangle_intersect (cairo_rectangle_int_t *dest, cairo_rectangle_int_t *src)
_cairo_rectangle_intersect (cairo_rectangle_int_t *dst,
const cairo_rectangle_int_t *src)
{
int x1, y1, x2, y2;

x1 = MAX (dest->x, src->x);
y1 = MAX (dest->y, src->y);
x2 = MIN (dest->x + dest->width, src->x + src->width);
y2 = MIN (dest->y + dest->height, src->y + src->height);
x1 = MAX (dst->x, src->x);
y1 = MAX (dst->y, src->y);
/* Beware the unsigned promotion, fortunately we have bits to spare
* as (CAIRO_RECT_INT_MAX - CAIRO_RECT_INT_MIN) < UINT_MAX
*/
x2 = MIN (dst->x + (int) dst->width, src->x + (int) src->width);
y2 = MIN (dst->y + (int) dst->height, src->y + (int) src->height);

if (x1 >= x2 || y1 >= y2) {
dest->x = 0;
dest->y = 0;
dest->width = 0;
dest->height = 0;
dst->x = 0;
dst->y = 0;
dst->width = 0;
dst->height = 0;
} else {
dest->x = x1;
dest->y = y1;
dest->width = x2 - x1;
dest->height = y2 - y1;
dst->x = x1;
dst->y = y1;
dst->width = x2 - x1;
dst->height = y2 - y1;
}
}


#define P1x (line->p1.x)
#define P1y (line->p1.y)
#define P2x (line->p2.x)
Expand Down
3 changes: 2 additions & 1 deletion src/cairoint.h
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,8 @@ _cairo_box_round_to_rectangle (const cairo_box_t *box,
cairo_rectangle_int_t *rectangle);

cairo_private void
_cairo_rectangle_intersect (cairo_rectangle_int_t *dest, cairo_rectangle_int_t *src);
_cairo_rectangle_intersect (cairo_rectangle_int_t *dst,
const cairo_rectangle_int_t *src);

cairo_private cairo_bool_t
_cairo_box_intersects_line_segment (cairo_box_t *box, cairo_line_t *line);
Expand Down

0 comments on commit 0e41561

Please sign in to comment.