From 65448e8b2af9bec38f86ab45916a9bcc7726ae30 Mon Sep 17 00:00:00 2001 From: Patrick Lam Date: Mon, 10 Apr 2006 16:06:42 +0000 Subject: [PATCH] src/fcdir.c (FcDirScanConfig) Don't leak in error cases (Coverity defects #777, #1826) reviewed by: plam --- ChangeLog | 6 ++++++ src/fcdir.c | 54 ++++++++++++++++++++++++++++++++++++----------------- 2 files changed, 43 insertions(+), 17 deletions(-) diff --git a/ChangeLog b/ChangeLog index c999df4..6d1a348 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,9 @@ +2006-04-10 Frederic Crozat + reviewed by: plam + + * src/fcdir.c (FcDirScanConfig) + Don't leak in error cases (Coverity defects #777, #1826) + 2006-04-10 Frederic Crozat reviewed by: plam diff --git a/src/fcdir.c b/src/fcdir.c index f505bf5..4463b31 100644 --- a/src/fcdir.c +++ b/src/fcdir.c @@ -180,31 +180,44 @@ FcDirScanConfig (FcFontSet *set, } tmpSet = FcFontSetCreate(); - if (!tmpSet) - { - free (file); - return FcFalse; + if (!tmpSet) + { + ret = FcFalse; + goto bail0; } dirlistlen = 0; dirlistalloc = 8; dirlist = malloc(dirlistalloc * sizeof(FcChar8 *)); - if (!dirlist) - return FcFalse; + if (!dirlist) + { + ret = FcFalse; + goto bail1; + } while ((e = readdir (d))) { if (e->d_name[0] != '.' && strlen (e->d_name) < FC_MAX_FILE_LEN) { if (dirlistlen == dirlistalloc) { + FcChar8 **tmp_dirlist; + dirlistalloc *= 2; - dirlist = realloc(dirlist, dirlistalloc * sizeof(FcChar8 *)); - if (!dirlist) - return FcFalse; + tmp_dirlist = realloc(dirlist, + dirlistalloc * sizeof(FcChar8 *)); + if (!tmp_dirlist) + { + ret = FcFalse; + goto bail2; + } + dirlist = tmp_dirlist; } dirlist[dirlistlen] = malloc(strlen (e->d_name) + 1); - if (!dirlist[dirlistlen]) - return FcFalse; + if (!dirlist[dirlistlen]) + { + ret = FcFalse; + goto bail2; + } strcpy((char *)dirlist[dirlistlen], e->d_name); dirlistlen++; } @@ -217,11 +230,6 @@ FcDirScanConfig (FcFontSet *set, ret = FcFileScanConfig (tmpSet, dirs, cache, blanks, file, force, config); i++; } - for (i = 0; i < dirlistlen; i++) - free(dirlist[i]); - free (dirlist); - free (file); - closedir (d); /* * Now that the directory has been scanned, * add the cache entry @@ -238,8 +246,20 @@ FcDirScanConfig (FcFontSet *set, free (tmpSet->fonts); } FcMemFree (FC_MEM_FONTSET, sizeof (FcFontSet)); + + bail2: + for (i = 0; i < dirlistlen; i++) + free(dirlist[i]); + + free (dirlist); + + bail1: free (tmpSet); - + + bail0: + closedir (d); + + free (file); return ret; } -- 2.39.5