[pacman-dev] [PATCH] alpm/handle.c: ensure handle is not NULL before proceeding

Rémy Oudompheng remyoudompheng at gmail.com
Mon Mar 28 15:16:24 EDT 2011


Many alpm_option_get/set_*() functions already check this
and set pm_errno to the right value, but not all, so
this improves consistency.

Signed-off-by: Rémy Oudompheng <remy at archlinux.org>
---
 lib/libalpm/handle.c |   80 ++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 80 insertions(+), 0 deletions(-)

diff --git a/lib/libalpm/handle.c b/lib/libalpm/handle.c
index d4ebe82..c37a0f9 100644
--- a/lib/libalpm/handle.c
+++ b/lib/libalpm/handle.c
@@ -302,6 +302,10 @@ int SYMEXPORT alpm_option_set_root(const char *root)
 
 	ALPM_LOG_FUNC;
 
+	if (handle == NULL) {
+		pm_errno = PM_ERR_HANDLE_NULL;
+		return(-1);
+	}
 	if(!root) {
 		pm_errno = PM_ERR_WRONG_ARGS;
 		return(-1);
@@ -342,6 +346,10 @@ int SYMEXPORT alpm_option_set_dbpath(const char *dbpath)
 
 	ALPM_LOG_FUNC;
 
+	if (handle == NULL) {
+		pm_errno = PM_ERR_HANDLE_NULL;
+		return(-1);
+	}
 	if(!dbpath) {
 		pm_errno = PM_ERR_WRONG_ARGS;
 		return(-1);
@@ -380,6 +388,10 @@ int SYMEXPORT alpm_option_add_cachedir(const char *cachedir)
 
 	ALPM_LOG_FUNC;
 
+	if (handle == NULL) {
+		pm_errno = PM_ERR_HANDLE_NULL;
+		return(-1);
+	}
 	if(!cachedir) {
 		pm_errno = PM_ERR_WRONG_ARGS;
 		return(-1);
@@ -402,6 +414,10 @@ int SYMEXPORT alpm_option_add_cachedir(const char *cachedir)
 
 void SYMEXPORT alpm_option_set_cachedirs(alpm_list_t *cachedirs)
 {
+	if (handle == NULL) {
+		pm_errno = PM_ERR_HANDLE_NULL;
+		return;
+	}
 	if(handle->cachedirs) FREELIST(handle->cachedirs);
 	if(cachedirs) handle->cachedirs = cachedirs;
 }
@@ -411,6 +427,10 @@ int SYMEXPORT alpm_option_remove_cachedir(const char *cachedir)
 	char *vdata = NULL;
 	char *newcachedir;
 	size_t cachedirlen;
+	if (handle == NULL) {
+		pm_errno = PM_ERR_HANDLE_NULL;
+		return(-1);
+	}
 	/* verify cachedir ends in a '/' */
 	cachedirlen = strlen(cachedir);
 	if(cachedir[cachedirlen-1] != '/') {
@@ -434,6 +454,10 @@ int SYMEXPORT alpm_option_set_logfile(const char *logfile)
 
 	ALPM_LOG_FUNC;
 
+	if (handle == NULL) {
+		pm_errno = PM_ERR_HANDLE_NULL;
+		return(-1);
+	}
 	if(!logfile) {
 		pm_errno = PM_ERR_WRONG_ARGS;
 		return(-1);
@@ -456,16 +480,28 @@ int SYMEXPORT alpm_option_set_logfile(const char *logfile)
 
 void SYMEXPORT alpm_option_set_usesyslog(int usesyslog)
 {
+	if (handle == NULL) {
+		pm_errno = PM_ERR_HANDLE_NULL;
+		return;
+	}
 	handle->usesyslog = usesyslog;
 }
 
 void SYMEXPORT alpm_option_add_noupgrade(const char *pkg)
 {
+	if (handle == NULL) {
+		pm_errno = PM_ERR_HANDLE_NULL;
+		return;
+	}
 	handle->noupgrade = alpm_list_add(handle->noupgrade, strdup(pkg));
 }
 
 void SYMEXPORT alpm_option_set_noupgrades(alpm_list_t *noupgrade)
 {
+	if (handle == NULL) {
+		pm_errno = PM_ERR_HANDLE_NULL;
+		return;
+	}
 	if(handle->noupgrade) FREELIST(handle->noupgrade);
 	if(noupgrade) handle->noupgrade = noupgrade;
 }
@@ -473,6 +509,10 @@ void SYMEXPORT alpm_option_set_noupgrades(alpm_list_t *noupgrade)
 int SYMEXPORT alpm_option_remove_noupgrade(const char *pkg)
 {
 	char *vdata = NULL;
+	if (handle == NULL) {
+		pm_errno = PM_ERR_HANDLE_NULL;
+		return(-1);
+	}
 	handle->noupgrade = alpm_list_remove_str(handle->noupgrade, pkg, &vdata);
 	if(vdata != NULL) {
 		FREE(vdata);
@@ -495,6 +535,10 @@ void SYMEXPORT alpm_option_set_noextracts(alpm_list_t *noextract)
 int SYMEXPORT alpm_option_remove_noextract(const char *pkg)
 {
 	char *vdata = NULL;
+	if (handle == NULL) {
+		pm_errno = PM_ERR_HANDLE_NULL;
+		return(-1);
+	}
 	handle->noextract = alpm_list_remove_str(handle->noextract, pkg, &vdata);
 	if(vdata != NULL) {
 		FREE(vdata);
@@ -505,11 +549,19 @@ int SYMEXPORT alpm_option_remove_noextract(const char *pkg)
 
 void SYMEXPORT alpm_option_add_ignorepkg(const char *pkg)
 {
+	if (handle == NULL) {
+		pm_errno = PM_ERR_HANDLE_NULL;
+		return;
+	}
 	handle->ignorepkg = alpm_list_add(handle->ignorepkg, strdup(pkg));
 }
 
 void SYMEXPORT alpm_option_set_ignorepkgs(alpm_list_t *ignorepkgs)
 {
+	if (handle == NULL) {
+		pm_errno = PM_ERR_HANDLE_NULL;
+		return;
+	}
 	if(handle->ignorepkg) FREELIST(handle->ignorepkg);
 	if(ignorepkgs) handle->ignorepkg = ignorepkgs;
 }
@@ -517,6 +569,10 @@ void SYMEXPORT alpm_option_set_ignorepkgs(alpm_list_t *ignorepkgs)
 int SYMEXPORT alpm_option_remove_ignorepkg(const char *pkg)
 {
 	char *vdata = NULL;
+	if (handle == NULL) {
+		pm_errno = PM_ERR_HANDLE_NULL;
+		return(-1);
+	}
 	handle->ignorepkg = alpm_list_remove_str(handle->ignorepkg, pkg, &vdata);
 	if(vdata != NULL) {
 		FREE(vdata);
@@ -527,11 +583,19 @@ int SYMEXPORT alpm_option_remove_ignorepkg(const char *pkg)
 
 void SYMEXPORT alpm_option_add_ignoregrp(const char *grp)
 {
+	if (handle == NULL) {
+		pm_errno = PM_ERR_HANDLE_NULL;
+		return;
+	}
 	handle->ignoregrp = alpm_list_add(handle->ignoregrp, strdup(grp));
 }
 
 void SYMEXPORT alpm_option_set_ignoregrps(alpm_list_t *ignoregrps)
 {
+	if (handle == NULL) {
+		pm_errno = PM_ERR_HANDLE_NULL;
+		return;
+	}
 	if(handle->ignoregrp) FREELIST(handle->ignoregrp);
 	if(ignoregrps) handle->ignoregrp = ignoregrps;
 }
@@ -539,6 +603,10 @@ void SYMEXPORT alpm_option_set_ignoregrps(alpm_list_t *ignoregrps)
 int SYMEXPORT alpm_option_remove_ignoregrp(const char *grp)
 {
 	char *vdata = NULL;
+	if (handle == NULL) {
+		pm_errno = PM_ERR_HANDLE_NULL;
+		return(-1);
+	}
 	handle->ignoregrp = alpm_list_remove_str(handle->ignoregrp, grp, &vdata);
 	if(vdata != NULL) {
 		FREE(vdata);
@@ -549,17 +617,29 @@ int SYMEXPORT alpm_option_remove_ignoregrp(const char *grp)
 
 void SYMEXPORT alpm_option_set_arch(const char *arch)
 {
+	if (handle == NULL) {
+		pm_errno = PM_ERR_HANDLE_NULL;
+		return;
+	}
 	if(handle->arch) FREE(handle->arch);
 	if(arch) handle->arch = strdup(arch);
 }
 
 void SYMEXPORT alpm_option_set_usedelta(int usedelta)
 {
+	if (handle == NULL) {
+		pm_errno = PM_ERR_HANDLE_NULL;
+		return;
+	}
 	handle->usedelta = usedelta;
 }
 
 void SYMEXPORT alpm_option_set_checkspace(int checkspace)
 {
+	if (handle == NULL) {
+		pm_errno = PM_ERR_HANDLE_NULL;
+		return;
+	}
 	handle->checkspace = checkspace;
 }
 
-- 
1.7.4.2



More information about the pacman-dev mailing list