--- abiword-2.5.1/src/af/util/unix/ut_unixDirent.cpp~	2007-01-16 15:13:25.000000000 -0800
+++ abiword-2.5.1/src/af/util/unix/ut_unixDirent.cpp	2007-08-08 13:22:23.000000000 -0700
@@ -69,10 +69,11 @@
 	size_t arraysz; 
 	DIR *dirp; 
 	
+	p = names = NULL;
 	if ((dirp = opendir(dirname)) == NULL) 
 		return(-1); 
-	if (fstat(dirp->d_fd, &stb) < 0) 
-		return(-1); 
+	if (fstat(dirp->d_fd, &stb) < 0)
+		goto err_scandir;
 	
 	/* 
 	 * estimate the array size by taking the size of thedirectory file 
@@ -81,7 +82,7 @@
 	arraysz = (stb.st_size / 24); 
 	names = static_cast<struct dirent **>(g_try_malloc(arraysz * sizeof(struct dirent *)));
 	if (names == NULL) 
-		return(-1); 
+		goto err_scandir;
 	
 	nitems = 0; 
 	while ((d = readdir(dirp)) != NULL) { 
@@ -92,8 +93,8 @@
 		 */ 
 		p = static_cast<struct dirent *>(g_try_malloc(DIRSIZ(d)));
 		if (p == NULL) 
-			return(-1); 
-		p->d_ino = d->d_ino; 
+			goto err_scandir;
+		p->d_ino = d->d_ino;
 		p->d_off = d->d_off; 
 		p->d_reclen = d->d_reclen; 
 		memcpy(p->d_name, d->d_name, strlen(d->d_name) +1); 
@@ -103,20 +104,25 @@
 		 */ 
 		if (++nitems >= arraysz) { 
 			if (fstat(dirp->d_fd, &stb) < 0) 
-				return(-1); /* just might have grown */ 
+				goto err_scandir; /* just might have grown */
 			arraysz = stb.st_size / 12; 
 			names = (struct dirent **)(g_try_realloc((char*)(names),
-											   arraysz * sizeof(struct dirent*)));
+							   arraysz * sizeof(struct dirent*)));
 			if (names == NULL) 
-				return(-1); 
-		} 
+				goto err_scandir;
+	} 
 		names[nitems-1] = p; 
 	} 
 	closedir(dirp); 
 	if (nitems && dcomp != NULL) 
 		qsort(names, nitems, sizeof(struct dirent *),dcomp); 
 	*namelist = names; 
-	return(nitems); 
+	return(nitems);
+err_scandir:
+	if (p) free(p);
+	if (names) free(names);
+	closedir(dirp);
+	return(-1);
 } 
 
 /*