[pacman-dev] [PATCH 1/3] Don't realloc a 0-length files array when loading packages

Dan McGee dan at archlinux.org
Wed Oct 26 18:42:08 EDT 2011


There is some pecular behavior going on here when a package is loaded
that has no files, as is very common in our test suite. When we enter
the realloc/sort code, a package without files will call the following:

    files = realloc(NULL, 0);

One would assume this is a no-op, returning a NULL pointer, but that is
not the case and valgrind later reports we are leaking memory. Fix the
whole thing by skipping the reallocation and sort steps if the pointer
is NULL, as we have nothing to do.

Note that the package still gets marked as 'files loaded', becuase
although there were none, we tried and were successful.

Signed-off-by: Dan McGee <dan at archlinux.org>
---

For maint; so valgrind messages can stay cleaner and it is a pretty low-risk,
obvious fix.

 lib/libalpm/be_package.c |   18 ++++++++++--------
 1 files changed, 10 insertions(+), 8 deletions(-)

diff --git a/lib/libalpm/be_package.c b/lib/libalpm/be_package.c
index c20c703..c8e08e2 100644
--- a/lib/libalpm/be_package.c
+++ b/lib/libalpm/be_package.c
@@ -482,17 +482,19 @@ alpm_pkg_t *_alpm_pkg_load_internal(alpm_handle_t *handle,
 	newpkg->origin_data.file = strdup(pkgfile);
 	newpkg->ops = get_file_pkg_ops();
 	newpkg->handle = handle;
+	newpkg->infolevel = INFRQ_BASE | INFRQ_DESC | INFRQ_SCRIPTLET;
 
 	if(full) {
-		/* attempt to hand back any memory we don't need */
-		files = realloc(files, sizeof(alpm_file_t) * files_count);
-		/* "checking for conflicts" requires a sorted list, ensure that here */
-		_alpm_log(handle, ALPM_LOG_DEBUG, "sorting package filelist for %s\n", pkgfile);
-		newpkg->files.files = files_msort(files, files_count);
+		if(files) {
+			/* attempt to hand back any memory we don't need */
+			files = realloc(files, sizeof(alpm_file_t) * files_count);
+			/* "checking for conflicts" requires a sorted list, ensure that here */
+			_alpm_log(handle, ALPM_LOG_DEBUG,
+					"sorting package filelist for %s\n", pkgfile);
+			newpkg->files.files = files_msort(files, files_count);
+		}
 		newpkg->files.count = files_count;
-		newpkg->infolevel = INFRQ_BASE | INFRQ_DESC | INFRQ_FILES | INFRQ_SCRIPTLET;
-	} else {
-		newpkg->infolevel = INFRQ_BASE | INFRQ_DESC | INFRQ_SCRIPTLET;
+		newpkg->infolevel |= INFRQ_FILES;
 	}
 
 	return newpkg;
-- 
1.7.7



More information about the pacman-dev mailing list