[pacman-dev] [PATCH 01/11] handle: factor out string list option handling

Andrew Gregory andrew.gregory.8 at gmail.com
Fri Aug 1 17:19:44 EDT 2014


Consolidates repeated code and replaces dangerous
strdup calls with STRDUP.

Also fix a couple variables named "pkg" that
refer to file paths.

Signed-off-by: Andrew Gregory <andrew.gregory.8 at gmail.com>
---
 lib/libalpm/alpm.h   |  8 ++---
 lib/libalpm/handle.c | 89 +++++++++++++++++++++-------------------------------
 2 files changed, 39 insertions(+), 58 deletions(-)

diff --git a/lib/libalpm/alpm.h b/lib/libalpm/alpm.h
index db1e0cd..29a2dda 100644
--- a/lib/libalpm/alpm.h
+++ b/lib/libalpm/alpm.h
@@ -818,9 +818,9 @@ int alpm_option_set_usesyslog(alpm_handle_t *handle, int usesyslog);
  * @{
  */
 alpm_list_t *alpm_option_get_noupgrades(alpm_handle_t *handle);
-int alpm_option_add_noupgrade(alpm_handle_t *handle, const char *pkg);
+int alpm_option_add_noupgrade(alpm_handle_t *handle, const char *path);
 int alpm_option_set_noupgrades(alpm_handle_t *handle, alpm_list_t *noupgrade);
-int alpm_option_remove_noupgrade(alpm_handle_t *handle, const char *pkg);
+int alpm_option_remove_noupgrade(alpm_handle_t *handle, const char *path);
 /** @} */
 
 /** @name Accessors to the list of no-extract files.
@@ -830,9 +830,9 @@ int alpm_option_remove_noupgrade(alpm_handle_t *handle, const char *pkg);
  * @{
  */
 alpm_list_t *alpm_option_get_noextracts(alpm_handle_t *handle);
-int alpm_option_add_noextract(alpm_handle_t *handle, const char *pkg);
+int alpm_option_add_noextract(alpm_handle_t *handle, const char *path);
 int alpm_option_set_noextracts(alpm_handle_t *handle, alpm_list_t *noextract);
-int alpm_option_remove_noextract(alpm_handle_t *handle, const char *pkg);
+int alpm_option_remove_noextract(alpm_handle_t *handle, const char *path);
 /** @} */
 
 /** @name Accessors to the list of ignored packages.
diff --git a/lib/libalpm/handle.c b/lib/libalpm/handle.c
index 0842d51..b218a68 100644
--- a/lib/libalpm/handle.c
+++ b/lib/libalpm/handle.c
@@ -444,26 +444,28 @@ int SYMEXPORT alpm_option_set_usesyslog(alpm_handle_t *handle, int usesyslog)
 	return 0;
 }
 
-int SYMEXPORT alpm_option_add_noupgrade(alpm_handle_t *handle, const char *pkg)
+static int _alpm_option_strlist_add(alpm_handle_t *handle, alpm_list_t **list, const char *str)
 {
+	char *dup;
 	CHECK_HANDLE(handle, return -1);
-	handle->noupgrade = alpm_list_add(handle->noupgrade, strdup(pkg));
+	STRDUP(dup, str, RET_ERR(handle, ALPM_ERR_MEMORY, -1));
+	*list = alpm_list_add(*list, dup);
 	return 0;
 }
 
-int SYMEXPORT alpm_option_set_noupgrades(alpm_handle_t *handle, alpm_list_t *noupgrade)
+static int _alpm_option_strlist_set(alpm_handle_t *handle, alpm_list_t **list, alpm_list_t *newlist)
 {
 	CHECK_HANDLE(handle, return -1);
-	if(handle->noupgrade) FREELIST(handle->noupgrade);
-	handle->noupgrade = alpm_list_strdup(noupgrade);
+	FREELIST(*list);
+	*list = alpm_list_strdup(newlist);
 	return 0;
 }
 
-int SYMEXPORT alpm_option_remove_noupgrade(alpm_handle_t *handle, const char *pkg)
+static int _alpm_option_strlist_rem(alpm_handle_t *handle, alpm_list_t **list, const char *str)
 {
 	char *vdata = NULL;
 	CHECK_HANDLE(handle, return -1);
-	handle->noupgrade = alpm_list_remove_str(handle->noupgrade, pkg, &vdata);
+	*list = alpm_list_remove_str(*list, str, &vdata);
 	if(vdata != NULL) {
 		FREE(vdata);
 		return 1;
@@ -471,85 +473,64 @@ int SYMEXPORT alpm_option_remove_noupgrade(alpm_handle_t *handle, const char *pk
 	return 0;
 }
 
-int SYMEXPORT alpm_option_add_noextract(alpm_handle_t *handle, const char *pkg)
+int SYMEXPORT alpm_option_add_noupgrade(alpm_handle_t *handle, const char *pkg)
 {
-	CHECK_HANDLE(handle, return -1);
-	handle->noextract = alpm_list_add(handle->noextract, strdup(pkg));
-	return 0;
+	return _alpm_option_strlist_add(handle, &(handle->noupgrade), pkg);
+}
+
+int SYMEXPORT alpm_option_set_noupgrades(alpm_handle_t *handle, alpm_list_t *noupgrade)
+{
+	return _alpm_option_strlist_set(handle, &(handle->noupgrade), noupgrade);
+}
+
+int SYMEXPORT alpm_option_remove_noupgrade(alpm_handle_t *handle, const char *pkg)
+{
+	return _alpm_option_strlist_rem(handle, &(handle->noupgrade), pkg);
+}
+
+int SYMEXPORT alpm_option_add_noextract(alpm_handle_t *handle, const char *path)
+{
+	return _alpm_option_strlist_add(handle, &(handle->noextract), path);
 }
 
 int SYMEXPORT alpm_option_set_noextracts(alpm_handle_t *handle, alpm_list_t *noextract)
 {
-	CHECK_HANDLE(handle, return -1);
-	if(handle->noextract) FREELIST(handle->noextract);
-	handle->noextract = alpm_list_strdup(noextract);
-	return 0;
+	return _alpm_option_strlist_set(handle, &(handle->noextract), noextract);
 }
 
-int SYMEXPORT alpm_option_remove_noextract(alpm_handle_t *handle, const char *pkg)
+int SYMEXPORT alpm_option_remove_noextract(alpm_handle_t *handle, const char *path)
 {
-	char *vdata = NULL;
-	CHECK_HANDLE(handle, return -1);
-	handle->noextract = alpm_list_remove_str(handle->noextract, pkg, &vdata);
-	if(vdata != NULL) {
-		FREE(vdata);
-		return 1;
-	}
-	return 0;
+	return _alpm_option_strlist_rem(handle, &(handle->noextract), path);
 }
 
 int SYMEXPORT alpm_option_add_ignorepkg(alpm_handle_t *handle, const char *pkg)
 {
-	CHECK_HANDLE(handle, return -1);
-	handle->ignorepkg = alpm_list_add(handle->ignorepkg, strdup(pkg));
-	return 0;
+	return _alpm_option_strlist_add(handle, &(handle->ignorepkg), pkg);
 }
 
 int SYMEXPORT alpm_option_set_ignorepkgs(alpm_handle_t *handle, alpm_list_t *ignorepkgs)
 {
-	CHECK_HANDLE(handle, return -1);
-	if(handle->ignorepkg) FREELIST(handle->ignorepkg);
-	handle->ignorepkg = alpm_list_strdup(ignorepkgs);
-	return 0;
+	return _alpm_option_strlist_set(handle, &(handle->ignorepkg), ignorepkgs);
 }
 
 int SYMEXPORT alpm_option_remove_ignorepkg(alpm_handle_t *handle, const char *pkg)
 {
-	char *vdata = NULL;
-	CHECK_HANDLE(handle, return -1);
-	handle->ignorepkg = alpm_list_remove_str(handle->ignorepkg, pkg, &vdata);
-	if(vdata != NULL) {
-		FREE(vdata);
-		return 1;
-	}
-	return 0;
+	return _alpm_option_strlist_rem(handle, &(handle->ignorepkg), pkg);
 }
 
 int SYMEXPORT alpm_option_add_ignoregroup(alpm_handle_t *handle, const char *grp)
 {
-	CHECK_HANDLE(handle, return -1);
-	handle->ignoregroup = alpm_list_add(handle->ignoregroup, strdup(grp));
-	return 0;
+	return _alpm_option_strlist_add(handle, &(handle->ignoregroup), grp);
 }
 
 int SYMEXPORT alpm_option_set_ignoregroups(alpm_handle_t *handle, alpm_list_t *ignoregrps)
 {
-	CHECK_HANDLE(handle, return -1);
-	if(handle->ignoregroup) FREELIST(handle->ignoregroup);
-	handle->ignoregroup = alpm_list_strdup(ignoregrps);
-	return 0;
+	return _alpm_option_strlist_set(handle, &(handle->ignoregroup), ignoregrps);
 }
 
 int SYMEXPORT alpm_option_remove_ignoregroup(alpm_handle_t *handle, const char *grp)
 {
-	char *vdata = NULL;
-	CHECK_HANDLE(handle, return -1);
-	handle->ignoregroup = alpm_list_remove_str(handle->ignoregroup, grp, &vdata);
-	if(vdata != NULL) {
-		FREE(vdata);
-		return 1;
-	}
-	return 0;
+	return _alpm_option_strlist_rem(handle, &(handle->ignoregroup), grp);
 }
 
 int SYMEXPORT alpm_option_set_arch(alpm_handle_t *handle, const char *arch)
-- 
2.0.2


More information about the pacman-dev mailing list