socsvn commit: r236386 - soc2012/gpf/pefs_kmod/sbin/pefs

gpf at FreeBSD.org gpf at FreeBSD.org
Fri May 25 14:19:38 UTC 2012


Author: gpf
Date: Fri May 25 14:19:35 2012
New Revision: 236386
URL: http://svnweb.FreeBSD.org/socsvn/?view=rev&rev=236386

Log:
  free dynamic memory
  

Modified:
  soc2012/gpf/pefs_kmod/sbin/pefs/pefs_checksum.c
  soc2012/gpf/pefs_kmod/sbin/pefs/pefs_ctl.c

Modified: soc2012/gpf/pefs_kmod/sbin/pefs/pefs_checksum.c
==============================================================================
--- soc2012/gpf/pefs_kmod/sbin/pefs/pefs_checksum.c	Fri May 25 11:14:08 2012	(r236385)
+++ soc2012/gpf/pefs_kmod/sbin/pefs/pefs_checksum.c	Fri May 25 14:19:35 2012	(r236386)
@@ -139,6 +139,7 @@
 		csp->hash = malloc(hash_len);
 		if (csp->hash == NULL) {
 			pefs_warn("memory allocation error");
+			free(csp);
 			close(fd);
 			return (PEFS_ERR_SYS);
 		}
@@ -182,6 +183,14 @@
 	return (0);
 }
 
+static void
+pefs_init_hash_table(struct hash_table *checksum_hash_tablep)
+{
+	checksum_hash_tablep->size = 0;
+	checksum_hash_tablep->nelements = 0;
+	checksum_hash_tablep->buckets = NULL;
+}
+
 static int
 pefs_allocate_hash_table(struct hash_table *checksum_hash_tablep, uint32_t nelements)
 {
@@ -207,6 +216,32 @@
 	return (0);
 }
 
+static void
+pefs_free_hash_table(struct hash_table *checksum_hash_tablep)
+{
+	struct bucket *bp;
+	struct file_header *fhp, *tfhp;
+	struct checksum *csp, *tcsp;
+	uint32_t i;
+
+	if (checksum_hash_tablep->buckets != NULL) {
+		for (i = 0; i < checksum_hash_tablep->size; i++) {
+			bp = &checksum_hash_tablep->buckets[i];
+			LIST_FOREACH_SAFE(fhp, &(bp->file_headers), bucket_entries, tfhp) {				
+				TAILQ_FOREACH_SAFE(csp, &(fhp->checksums), checksum_entries, tcsp) {
+					TAILQ_REMOVE(&(fhp->checksums), csp, checksum_entries);
+					if (csp->hash != NULL)
+						free(csp->hash);
+					free(csp);
+				}
+				LIST_REMOVE(fhp, bucket_entries);
+				free(fhp);
+			}
+		}
+		free(checksum_hash_tablep->buckets);
+	}
+}
+
 static int
 pefs_add_to_bucket(struct bucket *bucketp, struct file_header *fhp)
 {
@@ -665,6 +700,7 @@
 	 * XXXgpf: [TODO] If pefs fs is mounted when .pefs.checksum is created, then it will obtain an
 	 * encrypted filename & encrypted data. I should make sure that checksum file is not being
 	 * opened inside a mounted pefs filesystem.
+	 * pefs_open_checksum_file()
 	 */
 	fdout = open(checksum_path, O_WRONLY | O_CREAT | O_EXCL,  S_IRUSR | S_IWUSR);
 	if (fdout == -1) {
@@ -672,6 +708,8 @@
 		return (PEFS_ERR_IO);
 	}
 
+	pefs_init_hash_table(&checksum_hash_table);
+
 	error = pefs_create_in_memory_db(fpin, md, hash_len,
 		&checksum_hash_table, fsroot);
 	if (error != 0)
@@ -682,10 +720,11 @@
 	error = pefs_write_checksum_file(fdout, &cfh, &checksum_hash_table);
 
 out:
-	close(fdout);
+	if (fdout >= 0)
+		close(fdout);
 	if (error != 0)
 		unlink(checksum_path);
-	/* XXXgpf: [TODO] free dynamic memory */
+	pefs_free_hash_table(&checksum_hash_table);
 
 	return (error);
 }

Modified: soc2012/gpf/pefs_kmod/sbin/pefs/pefs_ctl.c
==============================================================================
--- soc2012/gpf/pefs_kmod/sbin/pefs/pefs_ctl.c	Fri May 25 11:14:08 2012	(r236385)
+++ soc2012/gpf/pefs_kmod/sbin/pefs/pefs_ctl.c	Fri May 25 14:19:35 2012	(r236386)
@@ -1051,18 +1051,23 @@
 
 			if (j == PEFS_SUPPORTED_DIGESTS) {
 				pefs_warn("invalid digestname: %s", optarg);
-				return (PEFS_ERR_INVALID);
+				error = PEFS_ERR_INVALID;
+				goto out;
 			}
 			break;
 		case 'i':
 			fpin = fopen(optarg, "r");
 			if (fpin == NULL) {
 				warn("cannot open inputfile: %s", optarg);
-				return (PEFS_ERR_INVALID);
+				error = PEFS_ERR_INVALID;
+				goto out;
 			}
 			break;
 		default:
+			if (fpin != NULL)
+				fclose(fpin);
 			pefs_usage();
+			break;
 		}
 	argc -= optind;
 	argv += optind;
@@ -1076,7 +1081,9 @@
 
 	error = pefs_create_checksum_file(fpin, fsroot, algo);
 
-	fclose(fpin);
+out:
+	if (fpin != NULL)
+		fclose(fpin);
 
 	return (error);
 }


More information about the svn-soc-all mailing list