Bug 12102

Summary: uninitialized var
Product: cairo Reporter: Behdad Esfahbod <freedesktop>
Component: generalAssignee: Carl Worth <cworth>
Status: RESOLVED FIXED QA Contact: cairo-bugs mailing list <cairo-bugs>
Severity: normal    
Priority: medium CC: chris
Version: 1.2.0   
Hardware: Other   
OS: All   
Whiteboard:
i915 platform: i915 features:

Description Behdad Esfahbod 2007-08-22 11:54:00 UTC
I'm getting:

cairo-glitz-surface.c:300: warning: 'n' is used uninitialized in this function

That code is:

    /* restore the clip, if any */
    if (surface->has_clip) {
        glitz_box_t *box;
        int n;

        box = _cairo_glitz_get_boxes_from_region (&surface->clip, &n);
        if (box == NULL && n != 0) {
            free (pixels);
            return CAIRO_STATUS_NO_MEMORY;
        }

        glitz_surface_set_clip_region (surface->surface, 0, 0, box, n);

        free (box);
    }

So let's make _cairo_glitz_get_boxes_from_region() always set n.  That function is:

static glitz_box_t *
_cairo_glitz_get_boxes_from_region (cairo_region_t *region, int *nboxes)
{
    cairo_box_int_t *cboxes;
    glitz_box_t *gboxes;
    int n, i;

    if (_cairo_region_get_boxes (region, &n, &cboxes) != CAIRO_STATUS_SUCCESS)
        return NULL;

    *nboxes = n;
    if (n == 0)
        return NULL;

    ...

So if _cairo_region_get_boxes() returns NULL, n is indeed left uninitialized.  That's one thing to fix, but also should ensure that _cairo_region_get_boxes() sets num_boxes for sure.  That function is looking:

cairo_int_status_t
_cairo_region_get_boxes (cairo_region_t *region, int *num_boxes, cairo_box_int_t **boxes)
{
    int nboxes;
    pixman_box16_t *pboxes;
    cairo_box_int_t *cboxes;
    int i;
    
    pboxes = pixman_region_rectangles (&region->rgn, &nboxes);

    if (nboxes == 0) {
        *num_boxes = 0;
        *boxes = NULL;
        return CAIRO_STATUS_SUCCESS;
    }
    
    cboxes = _cairo_malloc_ab (nboxes, sizeof(cairo_box_int_t));
    if (cboxes == NULL)
        return CAIRO_STATUS_NO_MEMORY;
    ...


So again, on out-of-memory, num_boxes is not set.  Easy fix to this one is to move the *num_boxes = nboxes to the top.  Or, simply pass num_boxes to pixman_region_rectangles().
Comment 1 Chris Wilson 2008-01-17 04:10:43 UTC
I clobbered this warning with commit f638e5ea355cf0268a4b099ce7b8b98c69df6b67.

Use of freedesktop.org services, including Bugzilla, is subject to our Code of Conduct. How we collect and use information is described in our Privacy Policy.