[argyllcms] Memory leak in conv.c

  • From: Ian <pb23@xxxxxxxxxxxxx>
  • To: argyllcms@xxxxxxxxxxxxx
  • Date: Sun, 15 Apr 2012 05:01:43 +0100

In conv.c, in list_iccss(), if one of the ccss files generates a read error, there is a memory leak as the cs structure is not freed before the continue.


Ian

iccss *list_iccss(int *no) {
    int i, j;
    iccss *rv;

    char **paths = NULL;
    int npaths = 0;

npaths = xdg_bds(NULL, &paths, xdg_data, xdg_read, xdg_user, "color/*.ccss");

    if ((rv = malloc(sizeof(iccss) * (npaths + 1))) == NULL) {
        xdg_free(paths, npaths);
        if (no != NULL) *no = -1;
        return NULL;
    }

    for (i = j = 0; i < npaths; i++) {
        ccss *cs;
        int len;
        char *pp;
        char *tech, *disp;

        if ((cs = new_ccss()) == NULL) {
            for (--j; j>= 0; j--) {
                free(rv[j].path);
                free(rv[j].desc);
            }
            xdg_free(paths, npaths);
            if (no != NULL) *no = -1;
            return NULL;
        }
if (cs->read_ccss(cs, paths[i])) /* this looks like a memory leak here; need to cs->del(cs) before continue */
            continue;        /* Skip any unreadable ccss's */

        if ((tech = cs->tech) == NULL)
            tech = "";
        if ((disp = cs->disp) == NULL)
            disp = "";
        len = strlen(tech) + strlen(disp) + 4;
        if ((pp = malloc(len)) == NULL) {
            for (--j; j >= 0; j--) {
                free(rv[j].path);
                free(rv[j].desc);
            }
            free(rv);
            xdg_free(paths, npaths);
            if (no != NULL) *no = -1;
            return NULL;
        }
        if ((rv[j].path = strdup(paths[i])) == NULL) {
            for (--j; j >= 0; j--) {
                free(rv[j].path);
                free(rv[j].desc);
            }
            free(rv);
            free(pp);
            xdg_free(paths, npaths);
            if (no != NULL) *no = -1;
            return NULL;
        }
        strcpy(pp, tech);
        strcat(pp, " (");
        strcat(pp, disp);
        strcat(pp, ")");
        rv[j].desc = pp;
        cs->del(cs);
        j++;
    }
    xdg_free(paths, npaths);
    rv[j].path = NULL;
    rv[j].desc = NULL;
    if (no != NULL)
        *no = j;

    /* Sort the list */
#define HEAP_COMPARE(A,B) (strcmp(A.desc, B.desc) < 0)
    HEAPSORT(iccss, rv, j)
#undef HEAP_COMPARE

    return rv;
}

Other related posts:

  • » [argyllcms] Memory leak in conv.c - Ian