Fix multiple memory allocation problems (found with valgrind)
authorJ. Ali Harlow <ali@juiblex.co.uk>
Fri Oct 17 10:10:57 2014 +0100 (2014-10-17)
changeset 4583f841a46eab5
parent 457 51a084acef49
child 459 5576a85e174a
Fix multiple memory allocation problems (found with valgrind)
librazor/atomic-actions.c
librazor/atomic-emulate.c
librazor/importer.c
librazor/iterator.c
librazor/merger.c
librazor/razor-internal.h
librazor/razor.c
librazor/rpm.c
librazor/transaction.c
librazor/types/graph.c
librazor/util.c
src/main.c
     1.1 --- a/librazor/atomic-actions.c	Fri Oct 17 10:08:28 2014 +0100
     1.2 +++ b/librazor/atomic-actions.c	Fri Oct 17 10:10:57 2014 +0100
     1.3 @@ -190,6 +190,8 @@
     1.4  	}
     1.5  	primitives = atomic_action_list_reverse(primitives);
     1.6  
     1.7 +	atomic_action_free(action);
     1.8 +
     1.9  	return atomic_action_do(atomic, primitives);
    1.10  }
    1.11  
     2.1 --- a/librazor/atomic-emulate.c	Fri Oct 17 10:08:28 2014 +0100
     2.2 +++ b/librazor/atomic-emulate.c	Fri Oct 17 10:10:57 2014 +0100
     2.3 @@ -69,6 +69,8 @@
     2.4  		}
     2.5  	}
     2.6  
     2.7 +	closedir(dp);
     2.8 +
     2.9  	rmdir(directory);
    2.10  }
    2.11  
    2.12 @@ -108,6 +110,11 @@
    2.13  
    2.14  RAZOR_EXPORT void razor_atomic_destroy(struct razor_atomic *atomic)
    2.15  {
    2.16 +	if (atomic->actions) {
    2.17 +		atomic_action_free(atomic->actions);
    2.18 +		atomic->actions = NULL;
    2.19 +	}
    2.20 +
    2.21  	if (atomic->toplevel) {
    2.22  		recursive_remove(atomic->toplevel);
    2.23  		free(atomic->toplevel);
    2.24 @@ -117,6 +124,8 @@
    2.25  	if (atomic->error)
    2.26  		razor_error_free(atomic->error);
    2.27  
    2.28 +	free(atomic->description);
    2.29 +
    2.30  	free(atomic);
    2.31  }
    2.32  
     3.1 --- a/librazor/importer.c	Fri Oct 17 10:08:28 2014 +0100
     3.2 +++ b/librazor/importer.c	Fri Oct 17 10:10:57 2014 +0100
     3.3 @@ -415,6 +415,8 @@
     3.4  		serialize_files(set, p, array);
     3.5  		p++;
     3.6  	}
     3.7 +
     3.8 +	array_release(&d->files);
     3.9  }
    3.10  
    3.11  static void
    3.12 @@ -434,6 +436,7 @@
    3.13  	struct import_entry *filenames;
    3.14  	char *f, *end;
    3.15  	uint32_t name, *r, s;
    3.16 +	uint32_t *map;
    3.17  	char dirname[256];
    3.18  	struct import_directory *d, *last_root;
    3.19  	struct array roots;
    3.20 @@ -441,11 +444,12 @@
    3.21  
    3.22  	count = importer->files.size / sizeof (struct import_entry);
    3.23  	filenames = importer->files.data;
    3.24 -	razor_qsort_with_data(filenames,
    3.25 -			      count,
    3.26 -			      sizeof (struct import_entry),
    3.27 -			      compare_filenames,
    3.28 -			      NULL);
    3.29 +	map = razor_qsort_with_data(filenames,
    3.30 +				    count,
    3.31 +				    sizeof (struct import_entry),
    3.32 +				    compare_filenames,
    3.33 +				    NULL);
    3.34 +	free(map);
    3.35  
    3.36  	array_init(&roots);
    3.37  	last_root = NULL;
     4.1 --- a/librazor/iterator.c	Fri Oct 17 10:08:28 2014 +0100
     4.2 +++ b/librazor/iterator.c	Fri Oct 17 10:10:57 2014 +0100
     4.3 @@ -161,8 +161,8 @@
     4.4  {
     4.5  	assert (pi != NULL);
     4.6  
     4.7 -	if (pi->free_index)
     4.8 -		free(pi->index);
     4.9 +	if (pi->alloced_index)
    4.10 +		free(pi->alloced_index);
    4.11  
    4.12  	free(pi);
    4.13  }
    4.14 @@ -399,7 +399,7 @@
    4.15  	free(pq);
    4.16  
    4.17  	pi = razor_package_iterator_create_with_index(set, index);
    4.18 -	pi->free_index = 1;
    4.19 +	pi->alloced_index = index;
    4.20  
    4.21  	return pi;
    4.22  }
     5.1 --- a/librazor/merger.c	Fri Oct 17 10:08:28 2014 +0100
     5.2 +++ b/librazor/merger.c	Fri Oct 17 10:10:57 2014 +0100
     5.3 @@ -318,8 +318,14 @@
     5.4  	set2 = merger->source2.set;
     5.5  	map1 = merger->source1.file_map;
     5.6  	map2 = merger->source2.file_map;
     5.7 -	pool1 = set1->file_string_pool.data;
     5.8 -	pool2 = set2->file_string_pool.data;
     5.9 +	if (set1->file_string_pool.size)
    5.10 +		pool1 = set1->file_string_pool.data;
    5.11 +	else
    5.12 +		pool1 = NULL;
    5.13 +	if (set2->file_string_pool.size)
    5.14 +		pool2 = set2->file_string_pool.data;
    5.15 +	else
    5.16 +		pool2 = NULL;
    5.17  	root1 = (struct razor_entry *) set1->files.data;
    5.18  	root2 = (struct razor_entry *) set2->files.data;
    5.19  
    5.20 @@ -349,9 +355,9 @@
    5.21  			continue;
    5.22  		}
    5.23  
    5.24 -		if (!e1)
    5.25 +		if (!e1 || !pool1)
    5.26  			cmp = 1;
    5.27 -		else if (!e2)
    5.28 +		else if (!e2 || !pool2)
    5.29  			cmp = -1;
    5.30  		else {
    5.31  			cmp = strcmp (&pool1[e1->name],
    5.32 @@ -631,5 +637,9 @@
    5.33  	hashtable_release(&merger->table);
    5.34  	hashtable_release(&merger->file_table);
    5.35  	hashtable_release(&merger->details_table);
    5.36 +	free(merger->source1.property_map);
    5.37 +	free(merger->source1.file_map);
    5.38 +	free(merger->source2.property_map);
    5.39 +	free(merger->source2.file_map);
    5.40  	free(merger);
    5.41  }
     6.1 --- a/librazor/razor-internal.h	Fri Oct 17 10:08:28 2014 +0100
     6.2 +++ b/librazor/razor-internal.h	Fri Oct 17 10:10:57 2014 +0100
     6.3 @@ -154,7 +154,7 @@
     6.4  	struct razor_set *set;
     6.5  	struct razor_package *package, *end;
     6.6  	struct list *index;
     6.7 -	int free_index;
     6.8 +	void *alloced_index;
     6.9  };
    6.10  
    6.11  void
     7.1 --- a/librazor/razor.c	Fri Oct 17 10:08:28 2014 +0100
     7.2 +++ b/librazor/razor.c	Fri Oct 17 10:10:57 2014 +0100
     7.3 @@ -1284,6 +1284,7 @@
     7.4  
     7.5  	set = razor_merger_commit(merger);
     7.6  	razor_merger_destroy(merger);
     7.7 +	deque_free(done);
     7.8  
     7.9  	return set;
    7.10  }
     8.1 --- a/librazor/rpm.c	Fri Oct 17 10:08:28 2014 +0100
     8.2 +++ b/librazor/rpm.c	Fri Oct 17 10:10:57 2014 +0100
     8.3 @@ -381,6 +381,12 @@
     8.4  
     8.5  RAZOR_EXPORT void razor_relocations_destroy(struct razor_relocations *rr)
     8.6  {
     8.7 +	int i;
     8.8 +
     8.9 +	for (i = 0; i < rr->n_relocations; i++) {
    8.10 +	    free(rr->relocations[i].oldpath);
    8.11 +	    free(rr->relocations[i].newpath);
    8.12 +	}
    8.13  	free(rr->path);
    8.14  	free(rr->relocations);
    8.15  	free(rr);
     9.1 --- a/librazor/transaction.c	Fri Oct 17 10:08:28 2014 +0100
     9.2 +++ b/librazor/transaction.c	Fri Oct 17 10:10:57 2014 +0100
     9.3 @@ -753,6 +753,8 @@
     9.4  		fprintf(stderr, "installing %s-%s\n", name, version);
     9.5  #endif
     9.6  	}
     9.7 +
     9.8 +	razor_package_iterator_destroy(pi);
     9.9  }
    9.10  
    9.11  RAZOR_EXPORT int
    10.1 --- a/librazor/types/graph.c	Fri Oct 17 10:08:28 2014 +0100
    10.2 +++ b/librazor/types/graph.c	Fri Oct 17 10:10:57 2014 +0100
    10.3 @@ -383,10 +383,15 @@
    10.4  {
    10.5  	struct bitset active_edges;
    10.6  	int n_edges;
    10.7 +	struct deque *deque;
    10.8  
    10.9  	n_edges = graph->edges.size / sizeof(struct graph_edge);
   10.10  	bitset_init(active_edges);
   10.11  	bitset_set_n_bits(active_edges, 0, n_edges);
   10.12  
   10.13 -	return graph_edges_sort(graph, active_edges);
   10.14 +	deque = graph_edges_sort(graph, active_edges);
   10.15 +
   10.16 +	bitset_release(active_edges);
   10.17 +
   10.18 +	return deque;
   10.19  }
    11.1 --- a/librazor/util.c	Fri Oct 17 10:08:28 2014 +0100
    11.2 +++ b/librazor/util.c	Fri Oct 17 10:10:57 2014 +0100
    11.3 @@ -62,6 +62,19 @@
    11.4  	return p;
    11.5  }
    11.6  
    11.7 +#if HAVE_SYS_MMAN_H
    11.8 +#define OPEN_FILE_USED		(1U<<0)
    11.9 +#define OPEN_FILE_MMAPPED	(1U<<1)
   11.10 +
   11.11 +struct open_file {
   11.12 +	void *addr;
   11.13 +	size_t length;
   11.14 +	uint32_t flags;
   11.15 +};
   11.16 +
   11.17 +struct array open_files = { 0, };
   11.18 +#endif	/* HAVE_SYS_MMAN_H */
   11.19 +
   11.20  void *
   11.21  razor_file_get_contents(const char *filename, size_t *length, int private,
   11.22  			struct razor_error **error)
   11.23 @@ -71,6 +84,9 @@
   11.24  	void *addr = NULL;
   11.25  	size_t nb;
   11.26  	ssize_t res;
   11.27 +#if HAVE_SYS_MMAN_H
   11.28 +	struct open_file *of, *ofend;
   11.29 +#endif
   11.30  
   11.31  	fd = open(filename, O_RDONLY | O_BINARY);
   11.32  	if (fd < 0) {
   11.33 @@ -85,16 +101,31 @@
   11.34  	}
   11.35  
   11.36  	*length = st.st_size;
   11.37 +
   11.38  #if HAVE_SYS_MMAN_H
   11.39 +	ofend = open_files.data + open_files.size;
   11.40 +	for (of = open_files.data; of < ofend; of++)
   11.41 +		if (!(of->flags & OPEN_FILE_USED))
   11.42 +			break;
   11.43 +	if (of == ofend) {
   11.44 +		of = array_add(&open_files, sizeof *of);
   11.45 +		of->flags = 0;
   11.46 +	}
   11.47 +
   11.48  	if (!private) {
   11.49  		addr = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
   11.50  		if (addr == MAP_FAILED)
   11.51  			addr = NULL;
   11.52 +		else
   11.53 +			of->flags = OPEN_FILE_USED | OPEN_FILE_MMAPPED;
   11.54  	}
   11.55 -#endif
   11.56 +#endif	/* HAVE_SYS_MMAN_H */
   11.57  	if (!addr) {
   11.58  		addr = malloc(st.st_size);
   11.59  		if (addr) {
   11.60 +#if HAVE_SYS_MMAN_H
   11.61 +			of->flags = OPEN_FILE_USED;
   11.62 +#endif
   11.63  			nb = 0;
   11.64  			while(nb < st.st_size) {
   11.65  				res = read(fd, addr + nb, st.st_size - nb);
   11.66 @@ -112,17 +143,47 @@
   11.67  	}
   11.68  	close(fd);
   11.69  
   11.70 +#if HAVE_SYS_MMAN_H
   11.71 +	of->addr = addr;
   11.72 +	of->length = st.st_size;
   11.73 +#endif
   11.74 +
   11.75  	return addr;
   11.76  }
   11.77  
   11.78  int razor_file_free_contents(void *addr, size_t length)
   11.79  {
   11.80  #if HAVE_SYS_MMAN_H
   11.81 -	return munmap(addr, length);
   11.82 -#else
   11.83 +	int retval, mmapped;
   11.84 +	struct open_file *of, *ofend;
   11.85 +
   11.86 +	ofend = open_files.data + open_files.size;
   11.87 +	for (of = open_files.data; of < ofend; of++)
   11.88 +		if ((of->flags & OPEN_FILE_USED) && of->addr == addr)
   11.89 +			break;
   11.90 +
   11.91 +	if (of == ofend)
   11.92 +		return 1;
   11.93 +
   11.94 +	length = of->length;
   11.95 +	mmapped = of->flags & OPEN_FILE_MMAPPED;
   11.96 +	of->flags &= ~OPEN_FILE_USED;
   11.97 +
   11.98 +	for (of = open_files.data; of < ofend; of++)
   11.99 +		if (of->flags & OPEN_FILE_USED)
  11.100 +			break;
  11.101 +
  11.102 +	if (of == ofend) {
  11.103 +		array_release(&open_files);
  11.104 +		array_init(&open_files);
  11.105 +	}
  11.106 +
  11.107 +	if (mmapped)
  11.108 +		return munmap(addr, length);
  11.109 +#endif
  11.110 +
  11.111  	free(addr);
  11.112  	return 0;
  11.113 -#endif
  11.114  }
  11.115  
  11.116  struct qsort_context {
    12.1 --- a/src/main.c	Fri Oct 17 10:08:28 2014 +0100
    12.2 +++ b/src/main.c	Fri Oct 17 10:10:57 2014 +0100
    12.3 @@ -1375,6 +1375,8 @@
    12.4  		razor_atomic_destroy(atomic);
    12.5  	} while(!retval && r == 1);
    12.6  
    12.7 +	razor_install_iterator_destroy(ii);
    12.8 +
    12.9  	razor_set_unref(system);
   12.10  
   12.11  	free(description);
   12.12 @@ -1443,6 +1445,8 @@
   12.13  	if (upstream == NULL) {
   12.14  		fprintf(stderr, "%s\n", razor_error_get_msg(error));
   12.15  		razor_error_free(error);
   12.16 +		if (relocations)
   12.17 +			razor_relocations_destroy(relocations);
   12.18  		return 1;
   12.19  	}
   12.20