# HG changeset patch # User J. Ali Harlow # Date 1413537057 -3600 # Node ID 3f841a46eab555313940bd14b5ce1a9215180665 # Parent 51a084acef49001614a2a6f08cd7ad9ea2b4bbdb Fix multiple memory allocation problems (found with valgrind) diff -r 51a084acef49 -r 3f841a46eab5 librazor/atomic-actions.c --- a/librazor/atomic-actions.c Fri Oct 17 10:08:28 2014 +0100 +++ b/librazor/atomic-actions.c Fri Oct 17 10:10:57 2014 +0100 @@ -190,6 +190,8 @@ } primitives = atomic_action_list_reverse(primitives); + atomic_action_free(action); + return atomic_action_do(atomic, primitives); } diff -r 51a084acef49 -r 3f841a46eab5 librazor/atomic-emulate.c --- a/librazor/atomic-emulate.c Fri Oct 17 10:08:28 2014 +0100 +++ b/librazor/atomic-emulate.c Fri Oct 17 10:10:57 2014 +0100 @@ -69,6 +69,8 @@ } } + closedir(dp); + rmdir(directory); } @@ -108,6 +110,11 @@ RAZOR_EXPORT void razor_atomic_destroy(struct razor_atomic *atomic) { + if (atomic->actions) { + atomic_action_free(atomic->actions); + atomic->actions = NULL; + } + if (atomic->toplevel) { recursive_remove(atomic->toplevel); free(atomic->toplevel); @@ -117,6 +124,8 @@ if (atomic->error) razor_error_free(atomic->error); + free(atomic->description); + free(atomic); } diff -r 51a084acef49 -r 3f841a46eab5 librazor/importer.c --- a/librazor/importer.c Fri Oct 17 10:08:28 2014 +0100 +++ b/librazor/importer.c Fri Oct 17 10:10:57 2014 +0100 @@ -415,6 +415,8 @@ serialize_files(set, p, array); p++; } + + array_release(&d->files); } static void @@ -434,6 +436,7 @@ struct import_entry *filenames; char *f, *end; uint32_t name, *r, s; + uint32_t *map; char dirname[256]; struct import_directory *d, *last_root; struct array roots; @@ -441,11 +444,12 @@ count = importer->files.size / sizeof (struct import_entry); filenames = importer->files.data; - razor_qsort_with_data(filenames, - count, - sizeof (struct import_entry), - compare_filenames, - NULL); + map = razor_qsort_with_data(filenames, + count, + sizeof (struct import_entry), + compare_filenames, + NULL); + free(map); array_init(&roots); last_root = NULL; diff -r 51a084acef49 -r 3f841a46eab5 librazor/iterator.c --- a/librazor/iterator.c Fri Oct 17 10:08:28 2014 +0100 +++ b/librazor/iterator.c Fri Oct 17 10:10:57 2014 +0100 @@ -161,8 +161,8 @@ { assert (pi != NULL); - if (pi->free_index) - free(pi->index); + if (pi->alloced_index) + free(pi->alloced_index); free(pi); } @@ -399,7 +399,7 @@ free(pq); pi = razor_package_iterator_create_with_index(set, index); - pi->free_index = 1; + pi->alloced_index = index; return pi; } diff -r 51a084acef49 -r 3f841a46eab5 librazor/merger.c --- a/librazor/merger.c Fri Oct 17 10:08:28 2014 +0100 +++ b/librazor/merger.c Fri Oct 17 10:10:57 2014 +0100 @@ -318,8 +318,14 @@ set2 = merger->source2.set; map1 = merger->source1.file_map; map2 = merger->source2.file_map; - pool1 = set1->file_string_pool.data; - pool2 = set2->file_string_pool.data; + if (set1->file_string_pool.size) + pool1 = set1->file_string_pool.data; + else + pool1 = NULL; + if (set2->file_string_pool.size) + pool2 = set2->file_string_pool.data; + else + pool2 = NULL; root1 = (struct razor_entry *) set1->files.data; root2 = (struct razor_entry *) set2->files.data; @@ -349,9 +355,9 @@ continue; } - if (!e1) + if (!e1 || !pool1) cmp = 1; - else if (!e2) + else if (!e2 || !pool2) cmp = -1; else { cmp = strcmp (&pool1[e1->name], @@ -631,5 +637,9 @@ hashtable_release(&merger->table); hashtable_release(&merger->file_table); hashtable_release(&merger->details_table); + free(merger->source1.property_map); + free(merger->source1.file_map); + free(merger->source2.property_map); + free(merger->source2.file_map); free(merger); } diff -r 51a084acef49 -r 3f841a46eab5 librazor/razor-internal.h --- a/librazor/razor-internal.h Fri Oct 17 10:08:28 2014 +0100 +++ b/librazor/razor-internal.h Fri Oct 17 10:10:57 2014 +0100 @@ -154,7 +154,7 @@ struct razor_set *set; struct razor_package *package, *end; struct list *index; - int free_index; + void *alloced_index; }; void diff -r 51a084acef49 -r 3f841a46eab5 librazor/razor.c --- a/librazor/razor.c Fri Oct 17 10:08:28 2014 +0100 +++ b/librazor/razor.c Fri Oct 17 10:10:57 2014 +0100 @@ -1284,6 +1284,7 @@ set = razor_merger_commit(merger); razor_merger_destroy(merger); + deque_free(done); return set; } diff -r 51a084acef49 -r 3f841a46eab5 librazor/rpm.c --- a/librazor/rpm.c Fri Oct 17 10:08:28 2014 +0100 +++ b/librazor/rpm.c Fri Oct 17 10:10:57 2014 +0100 @@ -381,6 +381,12 @@ RAZOR_EXPORT void razor_relocations_destroy(struct razor_relocations *rr) { + int i; + + for (i = 0; i < rr->n_relocations; i++) { + free(rr->relocations[i].oldpath); + free(rr->relocations[i].newpath); + } free(rr->path); free(rr->relocations); free(rr); diff -r 51a084acef49 -r 3f841a46eab5 librazor/transaction.c --- a/librazor/transaction.c Fri Oct 17 10:08:28 2014 +0100 +++ b/librazor/transaction.c Fri Oct 17 10:10:57 2014 +0100 @@ -753,6 +753,8 @@ fprintf(stderr, "installing %s-%s\n", name, version); #endif } + + razor_package_iterator_destroy(pi); } RAZOR_EXPORT int diff -r 51a084acef49 -r 3f841a46eab5 librazor/types/graph.c --- a/librazor/types/graph.c Fri Oct 17 10:08:28 2014 +0100 +++ b/librazor/types/graph.c Fri Oct 17 10:10:57 2014 +0100 @@ -383,10 +383,15 @@ { struct bitset active_edges; int n_edges; + struct deque *deque; n_edges = graph->edges.size / sizeof(struct graph_edge); bitset_init(active_edges); bitset_set_n_bits(active_edges, 0, n_edges); - return graph_edges_sort(graph, active_edges); + deque = graph_edges_sort(graph, active_edges); + + bitset_release(active_edges); + + return deque; } diff -r 51a084acef49 -r 3f841a46eab5 librazor/util.c --- a/librazor/util.c Fri Oct 17 10:08:28 2014 +0100 +++ b/librazor/util.c Fri Oct 17 10:10:57 2014 +0100 @@ -62,6 +62,19 @@ return p; } +#if HAVE_SYS_MMAN_H +#define OPEN_FILE_USED (1U<<0) +#define OPEN_FILE_MMAPPED (1U<<1) + +struct open_file { + void *addr; + size_t length; + uint32_t flags; +}; + +struct array open_files = { 0, }; +#endif /* HAVE_SYS_MMAN_H */ + void * razor_file_get_contents(const char *filename, size_t *length, int private, struct razor_error **error) @@ -71,6 +84,9 @@ void *addr = NULL; size_t nb; ssize_t res; +#if HAVE_SYS_MMAN_H + struct open_file *of, *ofend; +#endif fd = open(filename, O_RDONLY | O_BINARY); if (fd < 0) { @@ -85,16 +101,31 @@ } *length = st.st_size; + #if HAVE_SYS_MMAN_H + ofend = open_files.data + open_files.size; + for (of = open_files.data; of < ofend; of++) + if (!(of->flags & OPEN_FILE_USED)) + break; + if (of == ofend) { + of = array_add(&open_files, sizeof *of); + of->flags = 0; + } + if (!private) { addr = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0); if (addr == MAP_FAILED) addr = NULL; + else + of->flags = OPEN_FILE_USED | OPEN_FILE_MMAPPED; } -#endif +#endif /* HAVE_SYS_MMAN_H */ if (!addr) { addr = malloc(st.st_size); if (addr) { +#if HAVE_SYS_MMAN_H + of->flags = OPEN_FILE_USED; +#endif nb = 0; while(nb < st.st_size) { res = read(fd, addr + nb, st.st_size - nb); @@ -112,17 +143,47 @@ } close(fd); +#if HAVE_SYS_MMAN_H + of->addr = addr; + of->length = st.st_size; +#endif + return addr; } int razor_file_free_contents(void *addr, size_t length) { #if HAVE_SYS_MMAN_H - return munmap(addr, length); -#else + int retval, mmapped; + struct open_file *of, *ofend; + + ofend = open_files.data + open_files.size; + for (of = open_files.data; of < ofend; of++) + if ((of->flags & OPEN_FILE_USED) && of->addr == addr) + break; + + if (of == ofend) + return 1; + + length = of->length; + mmapped = of->flags & OPEN_FILE_MMAPPED; + of->flags &= ~OPEN_FILE_USED; + + for (of = open_files.data; of < ofend; of++) + if (of->flags & OPEN_FILE_USED) + break; + + if (of == ofend) { + array_release(&open_files); + array_init(&open_files); + } + + if (mmapped) + return munmap(addr, length); +#endif + free(addr); return 0; -#endif } struct qsort_context { diff -r 51a084acef49 -r 3f841a46eab5 src/main.c --- a/src/main.c Fri Oct 17 10:08:28 2014 +0100 +++ b/src/main.c Fri Oct 17 10:10:57 2014 +0100 @@ -1375,6 +1375,8 @@ razor_atomic_destroy(atomic); } while(!retval && r == 1); + razor_install_iterator_destroy(ii); + razor_set_unref(system); free(description); @@ -1443,6 +1445,8 @@ if (upstream == NULL) { fprintf(stderr, "%s\n", razor_error_get_msg(error)); razor_error_free(error); + if (relocations) + razor_relocations_destroy(relocations); return 1; }