[pacman-dev] [PATCH 1/2] Ensure handle is valid and pm_errno is reset when calling into API

Dan McGee dan at archlinux.org
Tue Jun 14 11:10:40 EDT 2011


We didn't do due diligence before and ensure prior pm_errno values
weren't influencing what happened in further ALPM calls. I observed one
case of early setup code setting pm_errno to PM_ERR_WRONG_ARGS and that
flag persisting the entire time we were calling library code.

Add a new CHECK_HANDLE() macro that does two things: 1) ensures the
handle variable passed to it is non-NULL and 2) clears any existing
pm_errno flag set on the handle. This macro can replace many places we
used the ASSERT(handle != NULL, ...) pattern before.

Several other other places only need a simple 'set to zero' of the
pm_errno field.

Signed-off-by: Dan McGee <dan at archlinux.org>
---
 lib/libalpm/add.c        |    5 +-
 lib/libalpm/alpm.c       |    2 +-
 lib/libalpm/be_package.c |    2 +-
 lib/libalpm/be_sync.c    |    1 +
 lib/libalpm/conflict.c   |    1 +
 lib/libalpm/db.c         |   20 ++++++++--
 lib/libalpm/deps.c       |    5 ++-
 lib/libalpm/dload.c      |    2 +
 lib/libalpm/handle.c     |   94 +++++++++++++++++++++++-----------------------
 lib/libalpm/package.c    |   36 +++++++++++++++++-
 lib/libalpm/remove.c     |    2 +-
 lib/libalpm/signing.c    |    2 +
 lib/libalpm/sync.c       |    7 ++-
 lib/libalpm/trans.c      |   16 ++++----
 lib/libalpm/util.h       |    2 +
 15 files changed, 128 insertions(+), 69 deletions(-)

diff --git a/lib/libalpm/add.c b/lib/libalpm/add.c
index 6f93c61..7b394a5 100644
--- a/lib/libalpm/add.c
+++ b/lib/libalpm/add.c
@@ -57,7 +57,7 @@ int SYMEXPORT alpm_add_pkg(pmhandle_t *handle, pmpkg_t *pkg)
 	pmpkg_t *local;
 
 	/* Sanity checks */
-	ASSERT(handle != NULL, return -1);
+	CHECK_HANDLE(handle, return -1);
 	ASSERT(pkg != NULL, RET_ERR(handle, PM_ERR_WRONG_ARGS, -1));
 	ASSERT(handle == pkg->handle, RET_ERR(handle, PM_ERR_WRONG_ARGS, -1));
 	trans = handle->trans;
@@ -715,11 +715,12 @@ int _alpm_upgrade_packages(pmhandle_t *handle)
 
 	/* loop through our package list adding/upgrading one at a time */
 	for(targ = trans->add; targ; targ = targ->next) {
+		pmpkg_t *newpkg = targ->data;
+
 		if(handle->trans->state == STATE_INTERRUPTED) {
 			return ret;
 		}
 
-		pmpkg_t *newpkg = (pmpkg_t *)targ->data;
 		if(commit_single_pkg(handle, newpkg, pkg_current, pkg_count)) {
 			/* something screwed up on the commit, abort the trans */
 			trans->state = STATE_INTERRUPTED;
diff --git a/lib/libalpm/alpm.c b/lib/libalpm/alpm.c
index 5d475ce..9b9719d 100644
--- a/lib/libalpm/alpm.c
+++ b/lib/libalpm/alpm.c
@@ -104,7 +104,7 @@ int SYMEXPORT alpm_release(pmhandle_t *myhandle)
 	int ret = 0;
 	pmdb_t *db;
 
-	ASSERT(myhandle != NULL, return -1);
+	CHECK_HANDLE(myhandle, return -1);
 
 	/* close local database */
 	db = myhandle->db_local;
diff --git a/lib/libalpm/be_package.c b/lib/libalpm/be_package.c
index 1f20989..a0261d0 100644
--- a/lib/libalpm/be_package.c
+++ b/lib/libalpm/be_package.c
@@ -386,7 +386,7 @@ error:
 int SYMEXPORT alpm_pkg_load(pmhandle_t *handle, const char *filename, int full,
 		pgp_verify_t check_sig, pmpkg_t **pkg)
 {
-	ASSERT(handle != NULL, return -1);
+	CHECK_HANDLE(handle, return -1);
 	ASSERT(pkg != NULL, RET_ERR(handle, PM_ERR_WRONG_ARGS, -1));
 
 	*pkg = _alpm_pkg_load_internal(handle, filename, full, NULL, NULL, check_sig);
diff --git a/lib/libalpm/be_sync.c b/lib/libalpm/be_sync.c
index ccc8fdb..bb10928 100644
--- a/lib/libalpm/be_sync.c
+++ b/lib/libalpm/be_sync.c
@@ -116,6 +116,7 @@ int SYMEXPORT alpm_db_update(int force, pmdb_t *db)
 	/* Sanity checks */
 	ASSERT(db != NULL, return -1);
 	handle = db->handle;
+	handle->pm_errno = 0;
 	ASSERT(db != handle->db_local, RET_ERR(handle, PM_ERR_WRONG_ARGS, -1));
 	ASSERT(db->servers != NULL, RET_ERR(handle, PM_ERR_SERVER_NONE, -1));
 
diff --git a/lib/libalpm/conflict.c b/lib/libalpm/conflict.c
index d2734d7..a6bbe09 100644
--- a/lib/libalpm/conflict.c
+++ b/lib/libalpm/conflict.c
@@ -214,6 +214,7 @@ alpm_list_t *_alpm_outerconflicts(pmdb_t *db, alpm_list_t *packages)
 alpm_list_t SYMEXPORT *alpm_checkconflicts(pmhandle_t *handle,
 		alpm_list_t *pkglist)
 {
+	CHECK_HANDLE(handle, return NULL);
 	return _alpm_innerconflicts(handle, pkglist);
 }
 
diff --git a/lib/libalpm/db.c b/lib/libalpm/db.c
index 0584a36..c3a7abd 100644
--- a/lib/libalpm/db.c
+++ b/lib/libalpm/db.c
@@ -48,7 +48,7 @@
 pmdb_t SYMEXPORT *alpm_db_register_sync(pmhandle_t *handle, const char *treename)
 {
 	/* Sanity checks */
-	ASSERT(handle != NULL, return NULL);
+	CHECK_HANDLE(handle, return NULL);
 	ASSERT(treename != NULL && strlen(treename) != 0,
 			RET_ERR(handle, PM_ERR_WRONG_ARGS, NULL));
 	/* Do not register a database if a transaction is on-going */
@@ -75,7 +75,7 @@ int SYMEXPORT alpm_db_unregister_all(pmhandle_t *handle)
 	pmdb_t *db;
 
 	/* Sanity checks */
-	ASSERT(handle != NULL, return -1);
+	CHECK_HANDLE(handle, return -1);
 	/* Do not unregister a database if a transaction is on-going */
 	ASSERT(handle->trans == NULL, RET_ERR(handle, PM_ERR_TRANS_NOT_NULL, -1));
 
@@ -99,6 +99,7 @@ int SYMEXPORT alpm_db_unregister(pmdb_t *db)
 	ASSERT(db != NULL, return -1);
 	/* Do not unregister a database if a transaction is on-going */
 	handle = db->handle;
+	handle->pm_errno = 0;
 	ASSERT(handle->trans == NULL, RET_ERR(handle, PM_ERR_TRANS_NOT_NULL, -1));
 
 	if(db == handle->db_local) {
@@ -165,6 +166,7 @@ int SYMEXPORT alpm_db_add_server(pmdb_t *db, const char *url)
 
 	/* Sanity checks */
 	ASSERT(db != NULL, return -1);
+	db->handle->pm_errno = 0;
 	ASSERT(url != NULL && strlen(url) != 0, RET_ERR(db->handle, PM_ERR_WRONG_ARGS, -1));
 
 	newurl = sanitize_url(url);
@@ -190,6 +192,7 @@ int SYMEXPORT alpm_db_remove_server(pmdb_t *db, const char *url)
 
 	/* Sanity checks */
 	ASSERT(db != NULL, return -1);
+	db->handle->pm_errno = 0;
 	ASSERT(url != NULL && strlen(url) != 0, RET_ERR(db->handle, PM_ERR_WRONG_ARGS, -1));
 
 	newurl = sanitize_url(url);
@@ -216,6 +219,7 @@ int SYMEXPORT alpm_db_set_pgp_verify(pmdb_t *db, pgp_verify_t verify)
 {
 	/* Sanity checks */
 	ASSERT(db != NULL, return -1);
+	db->handle->pm_errno = 0;
 
 	db->pgp_verify = verify;
 	_alpm_log(db->handle, PM_LOG_DEBUG, "adding VerifySig option to database '%s': %d\n",
@@ -235,7 +239,9 @@ const char SYMEXPORT *alpm_db_get_name(const pmdb_t *db)
 pmpkg_t SYMEXPORT *alpm_db_get_pkg(pmdb_t *db, const char *name)
 {
 	ASSERT(db != NULL, return NULL);
-	ASSERT(name != NULL && strlen(name) != 0, return NULL);
+	db->handle->pm_errno = 0;
+	ASSERT(name != NULL && strlen(name) != 0,
+			RET_ERR(db->handle, PM_ERR_WRONG_ARGS, NULL));
 
 	return _alpm_db_get_pkgfromcache(db, name);
 }
@@ -244,6 +250,7 @@ pmpkg_t SYMEXPORT *alpm_db_get_pkg(pmdb_t *db, const char *name)
 alpm_list_t SYMEXPORT *alpm_db_get_pkgcache(pmdb_t *db)
 {
 	ASSERT(db != NULL, return NULL);
+	db->handle->pm_errno = 0;
 	return _alpm_db_get_pkgcache(db);
 }
 
@@ -251,7 +258,9 @@ alpm_list_t SYMEXPORT *alpm_db_get_pkgcache(pmdb_t *db)
 pmgrp_t SYMEXPORT *alpm_db_readgrp(pmdb_t *db, const char *name)
 {
 	ASSERT(db != NULL, return NULL);
-	ASSERT(name != NULL && strlen(name) != 0, return NULL);
+	db->handle->pm_errno = 0;
+	ASSERT(name != NULL && strlen(name) != 0,
+			RET_ERR(db->handle, PM_ERR_WRONG_ARGS, NULL));
 
 	return _alpm_db_get_grpfromcache(db, name);
 }
@@ -260,6 +269,7 @@ pmgrp_t SYMEXPORT *alpm_db_readgrp(pmdb_t *db, const char *name)
 alpm_list_t SYMEXPORT *alpm_db_get_grpcache(pmdb_t *db)
 {
 	ASSERT(db != NULL, return NULL);
+	db->handle->pm_errno = 0;
 
 	return _alpm_db_get_grpcache(db);
 }
@@ -268,6 +278,7 @@ alpm_list_t SYMEXPORT *alpm_db_get_grpcache(pmdb_t *db)
 alpm_list_t SYMEXPORT *alpm_db_search(pmdb_t *db, const alpm_list_t* needles)
 {
 	ASSERT(db != NULL, return NULL);
+	db->handle->pm_errno = 0;
 
 	return _alpm_db_search(db, needles);
 }
@@ -276,6 +287,7 @@ alpm_list_t SYMEXPORT *alpm_db_search(pmdb_t *db, const alpm_list_t* needles)
 int SYMEXPORT alpm_db_set_pkgreason(pmdb_t *db, const char *name, pmpkgreason_t reason)
 {
 	ASSERT(db != NULL, return -1);
+	db->handle->pm_errno = 0;
 	/* TODO assert db == db_local ? shouldn't need a db param at all here... */
 	ASSERT(name != NULL, RET_ERR(db->handle, PM_ERR_WRONG_ARGS, -1));
 
diff --git a/lib/libalpm/deps.c b/lib/libalpm/deps.c
index 2877640..10c0009 100644
--- a/lib/libalpm/deps.c
+++ b/lib/libalpm/deps.c
@@ -273,6 +273,8 @@ alpm_list_t SYMEXPORT *alpm_checkdeps(pmhandle_t *handle, alpm_list_t *pkglist,
 	alpm_list_t *baddeps = NULL;
 	int nodepversion;
 
+	CHECK_HANDLE(handle, return NULL);
+
 	targets = alpm_list_join(alpm_list_copy(remove), alpm_list_copy(upgrade));
 	for(i = pkglist; i; i = i->next) {
 		pmpkg_t *pkg = i->data;
@@ -657,7 +659,8 @@ pmpkg_t SYMEXPORT *alpm_find_dbs_satisfier(pmhandle_t *handle,
 	pmdepend_t *dep;
 	pmpkg_t *pkg;
 
-	ASSERT(dbs, return NULL);
+	CHECK_HANDLE(handle, return NULL);
+	ASSERT(dbs, RET_ERR(handle, PM_ERR_WRONG_ARGS, NULL));
 
 	dep = _alpm_splitdep(depstring);
 	ASSERT(dep, return NULL);
diff --git a/lib/libalpm/dload.c b/lib/libalpm/dload.c
index 46925a9..9354562 100644
--- a/lib/libalpm/dload.c
+++ b/lib/libalpm/dload.c
@@ -337,6 +337,8 @@ char SYMEXPORT *alpm_fetch_pkgurl(pmhandle_t *handle, const char *url)
 	const char *filename, *cachedir;
 	int ret;
 
+	CHECK_HANDLE(handle, return NULL);
+
 	filename = get_filename(url);
 
 	/* find a valid cache dir to download to */
diff --git a/lib/libalpm/handle.c b/lib/libalpm/handle.c
index 3c17e9d..d2bb4f6 100644
--- a/lib/libalpm/handle.c
+++ b/lib/libalpm/handle.c
@@ -88,148 +88,148 @@ void _alpm_handle_free(pmhandle_t *handle)
 
 alpm_cb_log SYMEXPORT alpm_option_get_logcb(pmhandle_t *handle)
 {
-	ASSERT(handle != NULL, return NULL);
+	CHECK_HANDLE(handle, return NULL);
 	return handle->logcb;
 }
 
 alpm_cb_download SYMEXPORT alpm_option_get_dlcb(pmhandle_t *handle)
 {
-	ASSERT(handle != NULL, return NULL);
+	CHECK_HANDLE(handle, return NULL);
 	return handle->dlcb;
 }
 
 alpm_cb_fetch SYMEXPORT alpm_option_get_fetchcb(pmhandle_t *handle)
 {
-	ASSERT(handle != NULL, return NULL);
+	CHECK_HANDLE(handle, return NULL);
 	return handle->fetchcb;
 }
 
 alpm_cb_totaldl SYMEXPORT alpm_option_get_totaldlcb(pmhandle_t *handle)
 {
-	ASSERT(handle != NULL, return NULL);
+	CHECK_HANDLE(handle, return NULL);
 	return handle->totaldlcb;
 }
 
 const char SYMEXPORT *alpm_option_get_root(pmhandle_t *handle)
 {
-	ASSERT(handle != NULL, return NULL);
+	CHECK_HANDLE(handle, return NULL);
 	return handle->root;
 }
 
 const char SYMEXPORT *alpm_option_get_dbpath(pmhandle_t *handle)
 {
-	ASSERT(handle != NULL, return NULL);
+	CHECK_HANDLE(handle, return NULL);
 	return handle->dbpath;
 }
 
 alpm_list_t SYMEXPORT *alpm_option_get_cachedirs(pmhandle_t *handle)
 {
-	ASSERT(handle != NULL, return NULL);
+	CHECK_HANDLE(handle, return NULL);
 	return handle->cachedirs;
 }
 
 const char SYMEXPORT *alpm_option_get_logfile(pmhandle_t *handle)
 {
-	ASSERT(handle != NULL, return NULL);
+	CHECK_HANDLE(handle, return NULL);
 	return handle->logfile;
 }
 
 const char SYMEXPORT *alpm_option_get_lockfile(pmhandle_t *handle)
 {
-	ASSERT(handle != NULL, return NULL);
+	CHECK_HANDLE(handle, return NULL);
 	return handle->lockfile;
 }
 
 const char SYMEXPORT *alpm_option_get_signaturedir(pmhandle_t *handle)
 {
-	ASSERT(handle != NULL, return NULL);
+	CHECK_HANDLE(handle, return NULL);
 	return handle->signaturedir;
 }
 
 int SYMEXPORT alpm_option_get_usesyslog(pmhandle_t *handle)
 {
-	ASSERT(handle != NULL, return -1);
+	CHECK_HANDLE(handle, return -1);
 	return handle->usesyslog;
 }
 
 alpm_list_t SYMEXPORT *alpm_option_get_noupgrades(pmhandle_t *handle)
 {
-	ASSERT(handle != NULL, return NULL);
+	CHECK_HANDLE(handle, return NULL);
 	return handle->noupgrade;
 }
 
 alpm_list_t SYMEXPORT *alpm_option_get_noextracts(pmhandle_t *handle)
 {
-	ASSERT(handle != NULL, return NULL);
+	CHECK_HANDLE(handle, return NULL);
 	return handle->noextract;
 }
 
 alpm_list_t SYMEXPORT *alpm_option_get_ignorepkgs(pmhandle_t *handle)
 {
-	ASSERT(handle != NULL, return NULL);
+	CHECK_HANDLE(handle, return NULL);
 	return handle->ignorepkg;
 }
 
 alpm_list_t SYMEXPORT *alpm_option_get_ignoregrps(pmhandle_t *handle)
 {
-	ASSERT(handle != NULL, return NULL);
+	CHECK_HANDLE(handle, return NULL);
 	return handle->ignoregrp;
 }
 
 const char SYMEXPORT *alpm_option_get_arch(pmhandle_t *handle)
 {
-	ASSERT(handle != NULL, return NULL);
+	CHECK_HANDLE(handle, return NULL);
 	return handle->arch;
 }
 
 int SYMEXPORT alpm_option_get_usedelta(pmhandle_t *handle)
 {
-	ASSERT(handle != NULL, return -1);
+	CHECK_HANDLE(handle, return -1);
 	return handle->usedelta;
 }
 
 int SYMEXPORT alpm_option_get_checkspace(pmhandle_t *handle)
 {
-	ASSERT(handle != NULL, return -1);
+	CHECK_HANDLE(handle, return -1);
 	return handle->checkspace;
 }
 
 pmdb_t SYMEXPORT *alpm_option_get_localdb(pmhandle_t *handle)
 {
-	ASSERT(handle != NULL, return NULL);
+	CHECK_HANDLE(handle, return NULL);
 	return handle->db_local;
 }
 
 alpm_list_t SYMEXPORT *alpm_option_get_syncdbs(pmhandle_t *handle)
 {
-	ASSERT(handle != NULL, return NULL);
+	CHECK_HANDLE(handle, return NULL);
 	return handle->dbs_sync;
 }
 
 int SYMEXPORT alpm_option_set_logcb(pmhandle_t *handle, alpm_cb_log cb)
 {
-	ASSERT(handle != NULL, return -1);
+	CHECK_HANDLE(handle, return -1);
 	handle->logcb = cb;
 	return 0;
 }
 
 int SYMEXPORT alpm_option_set_dlcb(pmhandle_t *handle, alpm_cb_download cb)
 {
-	ASSERT(handle != NULL, return -1);
+	CHECK_HANDLE(handle, return -1);
 	handle->dlcb = cb;
 	return 0;
 }
 
 int SYMEXPORT alpm_option_set_fetchcb(pmhandle_t *handle, alpm_cb_fetch cb)
 {
-	ASSERT(handle != NULL, return -1);
+	CHECK_HANDLE(handle, return -1);
 	handle->fetchcb = cb;
 	return 0;
 }
 
 int SYMEXPORT alpm_option_set_totaldlcb(pmhandle_t *handle, alpm_cb_totaldl cb)
 {
-	ASSERT(handle != NULL, return -1);
+	CHECK_HANDLE(handle, return -1);
 	handle->totaldlcb = cb;
 	return 0;
 }
@@ -284,7 +284,7 @@ int SYMEXPORT alpm_option_add_cachedir(pmhandle_t *handle, const char *cachedir)
 {
 	char *newcachedir;
 
-	ASSERT(handle != NULL, return -1);
+	CHECK_HANDLE(handle, return -1);
 	if(!cachedir) {
 		handle->pm_errno = PM_ERR_WRONG_ARGS;
 		return -1;
@@ -301,7 +301,7 @@ int SYMEXPORT alpm_option_add_cachedir(pmhandle_t *handle, const char *cachedir)
 int SYMEXPORT alpm_option_set_cachedirs(pmhandle_t *handle, alpm_list_t *cachedirs)
 {
 	alpm_list_t *i;
-	ASSERT(handle != NULL, return -1);
+	CHECK_HANDLE(handle, return -1);
 	if(handle->cachedirs) {
 		FREELIST(handle->cachedirs);
 	}
@@ -319,7 +319,7 @@ int SYMEXPORT alpm_option_remove_cachedir(pmhandle_t *handle, const char *cached
 	char *vdata = NULL;
 	char *newcachedir;
 	size_t cachedirlen;
-	ASSERT(handle != NULL, return -1);
+	CHECK_HANDLE(handle, return -1);
 	/* verify cachedir ends in a '/' */
 	cachedirlen = strlen(cachedir);
 	if(cachedir[cachedirlen-1] != '/') {
@@ -341,7 +341,7 @@ int SYMEXPORT alpm_option_set_logfile(pmhandle_t *handle, const char *logfile)
 {
 	char *oldlogfile = handle->logfile;
 
-	ASSERT(handle != NULL, return -1);
+	CHECK_HANDLE(handle, return -1);
 	if(!logfile) {
 		handle->pm_errno = PM_ERR_WRONG_ARGS;
 		return -1;
@@ -364,7 +364,7 @@ int SYMEXPORT alpm_option_set_logfile(pmhandle_t *handle, const char *logfile)
 
 int SYMEXPORT alpm_option_set_signaturedir(pmhandle_t *handle, const char *signaturedir)
 {
-	ASSERT(handle != NULL, return -1);
+	CHECK_HANDLE(handle, return -1);
 	if(!signaturedir) {
 		handle->pm_errno = PM_ERR_WRONG_ARGS;
 		return -1;
@@ -381,21 +381,21 @@ int SYMEXPORT alpm_option_set_signaturedir(pmhandle_t *handle, const char *signa
 
 int SYMEXPORT alpm_option_set_usesyslog(pmhandle_t *handle, int usesyslog)
 {
-	ASSERT(handle != NULL, return -1);
+	CHECK_HANDLE(handle, return -1);
 	handle->usesyslog = usesyslog;
 	return 0;
 }
 
 int SYMEXPORT alpm_option_add_noupgrade(pmhandle_t *handle, const char *pkg)
 {
-	ASSERT(handle != NULL, return -1);
+	CHECK_HANDLE(handle, return -1);
 	handle->noupgrade = alpm_list_add(handle->noupgrade, strdup(pkg));
 	return 0;
 }
 
 int SYMEXPORT alpm_option_set_noupgrades(pmhandle_t *handle, alpm_list_t *noupgrade)
 {
-	ASSERT(handle != NULL, return -1);
+	CHECK_HANDLE(handle, return -1);
 	if(handle->noupgrade) FREELIST(handle->noupgrade);
 	handle->noupgrade = alpm_list_strdup(noupgrade);
 	return 0;
@@ -404,7 +404,7 @@ int SYMEXPORT alpm_option_set_noupgrades(pmhandle_t *handle, alpm_list_t *noupgr
 int SYMEXPORT alpm_option_remove_noupgrade(pmhandle_t *handle, const char *pkg)
 {
 	char *vdata = NULL;
-	ASSERT(handle != NULL, return -1);
+	CHECK_HANDLE(handle, return -1);
 	handle->noupgrade = alpm_list_remove_str(handle->noupgrade, pkg, &vdata);
 	if(vdata != NULL) {
 		FREE(vdata);
@@ -415,14 +415,14 @@ int SYMEXPORT alpm_option_remove_noupgrade(pmhandle_t *handle, const char *pkg)
 
 int SYMEXPORT alpm_option_add_noextract(pmhandle_t *handle, const char *pkg)
 {
-	ASSERT(handle != NULL, return -1);
+	CHECK_HANDLE(handle, return -1);
 	handle->noextract = alpm_list_add(handle->noextract, strdup(pkg));
 	return 0;
 }
 
 int SYMEXPORT alpm_option_set_noextracts(pmhandle_t *handle, alpm_list_t *noextract)
 {
-	ASSERT(handle != NULL, return -1);
+	CHECK_HANDLE(handle, return -1);
 	if(handle->noextract) FREELIST(handle->noextract);
 	handle->noextract = alpm_list_strdup(noextract);
 	return 0;
@@ -431,7 +431,7 @@ int SYMEXPORT alpm_option_set_noextracts(pmhandle_t *handle, alpm_list_t *noextr
 int SYMEXPORT alpm_option_remove_noextract(pmhandle_t *handle, const char *pkg)
 {
 	char *vdata = NULL;
-	ASSERT(handle != NULL, return -1);
+	CHECK_HANDLE(handle, return -1);
 	handle->noextract = alpm_list_remove_str(handle->noextract, pkg, &vdata);
 	if(vdata != NULL) {
 		FREE(vdata);
@@ -442,14 +442,14 @@ int SYMEXPORT alpm_option_remove_noextract(pmhandle_t *handle, const char *pkg)
 
 int SYMEXPORT alpm_option_add_ignorepkg(pmhandle_t *handle, const char *pkg)
 {
-	ASSERT(handle != NULL, return -1);
+	CHECK_HANDLE(handle, return -1);
 	handle->ignorepkg = alpm_list_add(handle->ignorepkg, strdup(pkg));
 	return 0;
 }
 
 int SYMEXPORT alpm_option_set_ignorepkgs(pmhandle_t *handle, alpm_list_t *ignorepkgs)
 {
-	ASSERT(handle != NULL, return -1);
+	CHECK_HANDLE(handle, return -1);
 	if(handle->ignorepkg) FREELIST(handle->ignorepkg);
 	handle->ignorepkg = alpm_list_strdup(ignorepkgs);
 	return 0;
@@ -458,7 +458,7 @@ int SYMEXPORT alpm_option_set_ignorepkgs(pmhandle_t *handle, alpm_list_t *ignore
 int SYMEXPORT alpm_option_remove_ignorepkg(pmhandle_t *handle, const char *pkg)
 {
 	char *vdata = NULL;
-	ASSERT(handle != NULL, return -1);
+	CHECK_HANDLE(handle, return -1);
 	handle->ignorepkg = alpm_list_remove_str(handle->ignorepkg, pkg, &vdata);
 	if(vdata != NULL) {
 		FREE(vdata);
@@ -469,14 +469,14 @@ int SYMEXPORT alpm_option_remove_ignorepkg(pmhandle_t *handle, const char *pkg)
 
 int SYMEXPORT alpm_option_add_ignoregrp(pmhandle_t *handle, const char *grp)
 {
-	ASSERT(handle != NULL, return -1);
+	CHECK_HANDLE(handle, return -1);
 	handle->ignoregrp = alpm_list_add(handle->ignoregrp, strdup(grp));
 	return 0;
 }
 
 int SYMEXPORT alpm_option_set_ignoregrps(pmhandle_t *handle, alpm_list_t *ignoregrps)
 {
-	ASSERT(handle != NULL, return -1);
+	CHECK_HANDLE(handle, return -1);
 	if(handle->ignoregrp) FREELIST(handle->ignoregrp);
 	handle->ignoregrp = alpm_list_strdup(ignoregrps);
 	return 0;
@@ -485,7 +485,7 @@ int SYMEXPORT alpm_option_set_ignoregrps(pmhandle_t *handle, alpm_list_t *ignore
 int SYMEXPORT alpm_option_remove_ignoregrp(pmhandle_t *handle, const char *grp)
 {
 	char *vdata = NULL;
-	ASSERT(handle != NULL, return -1);
+	CHECK_HANDLE(handle, return -1);
 	handle->ignoregrp = alpm_list_remove_str(handle->ignoregrp, grp, &vdata);
 	if(vdata != NULL) {
 		FREE(vdata);
@@ -496,7 +496,7 @@ int SYMEXPORT alpm_option_remove_ignoregrp(pmhandle_t *handle, const char *grp)
 
 int SYMEXPORT alpm_option_set_arch(pmhandle_t *handle, const char *arch)
 {
-	ASSERT(handle != NULL, return -1);
+	CHECK_HANDLE(handle, return -1);
 	if(handle->arch) FREE(handle->arch);
 	if(arch) {
 		handle->arch = strdup(arch);
@@ -508,21 +508,21 @@ int SYMEXPORT alpm_option_set_arch(pmhandle_t *handle, const char *arch)
 
 int SYMEXPORT alpm_option_set_usedelta(pmhandle_t *handle, int usedelta)
 {
-	ASSERT(handle != NULL, return -1);
+	CHECK_HANDLE(handle, return -1);
 	handle->usedelta = usedelta;
 	return 0;
 }
 
 int SYMEXPORT alpm_option_set_checkspace(pmhandle_t *handle, int checkspace)
 {
-	ASSERT(handle != NULL, return -1);
+	CHECK_HANDLE(handle, return -1);
 	handle->checkspace = checkspace;
 	return 0;
 }
 
 int SYMEXPORT alpm_option_set_default_sigverify(pmhandle_t *handle, pgp_verify_t level)
 {
-	ASSERT(handle != NULL, return -1);
+	CHECK_HANDLE(handle, return -1);
 	ASSERT(level != PM_PGP_VERIFY_UNKNOWN, RET_ERR(handle, PM_ERR_WRONG_ARGS, -1));
 	handle->sigverify = level;
 	return 0;
@@ -530,7 +530,7 @@ int SYMEXPORT alpm_option_set_default_sigverify(pmhandle_t *handle, pgp_verify_t
 
 pgp_verify_t SYMEXPORT alpm_option_get_default_sigverify(pmhandle_t *handle)
 {
-	ASSERT(handle != NULL, return PM_PGP_VERIFY_UNKNOWN);
+	CHECK_HANDLE(handle, return PM_PGP_VERIFY_UNKNOWN);
 	return handle->sigverify;
 }
 
diff --git a/lib/libalpm/package.c b/lib/libalpm/package.c
index 014dedc..038c899 100644
--- a/lib/libalpm/package.c
+++ b/lib/libalpm/package.c
@@ -62,8 +62,10 @@ int SYMEXPORT alpm_pkg_checkmd5sum(pmpkg_t *pkg)
 	int retval;
 
 	ASSERT(pkg != NULL, return -1);
+	pkg->handle->pm_errno = 0;
 	/* We only inspect packages from sync repositories */
-	ASSERT(pkg->origin == PKG_FROM_SYNCDB, return -1);
+	ASSERT(pkg->origin == PKG_FROM_SYNCDB,
+			RET_ERR(pkg->handle, PM_ERR_WRONG_ARGS, -1));
 
 	fpath = _alpm_filecache_find(pkg->handle, alpm_pkg_get_filename(pkg));
 
@@ -162,116 +164,139 @@ struct pkg_operations default_pkg_ops = {
  * package, which depend on where the package was loaded from. */
 const char SYMEXPORT *alpm_pkg_get_filename(pmpkg_t *pkg)
 {
+	pkg->handle->pm_errno = 0;
 	return pkg->ops->get_filename(pkg);
 }
 
 const char SYMEXPORT *alpm_pkg_get_name(pmpkg_t *pkg)
 {
+	pkg->handle->pm_errno = 0;
 	return pkg->name;
 }
 
 const char SYMEXPORT *alpm_pkg_get_version(pmpkg_t *pkg)
 {
+	pkg->handle->pm_errno = 0;
 	return pkg->version;
 }
 
 const char SYMEXPORT *alpm_pkg_get_desc(pmpkg_t *pkg)
 {
+	pkg->handle->pm_errno = 0;
 	return pkg->ops->get_desc(pkg);
 }
 
 const char SYMEXPORT *alpm_pkg_get_url(pmpkg_t *pkg)
 {
+	pkg->handle->pm_errno = 0;
 	return pkg->ops->get_url(pkg);
 }
 
 time_t SYMEXPORT alpm_pkg_get_builddate(pmpkg_t *pkg)
 {
+	pkg->handle->pm_errno = 0;
 	return pkg->ops->get_builddate(pkg);
 }
 
 time_t SYMEXPORT alpm_pkg_get_installdate(pmpkg_t *pkg)
 {
+	pkg->handle->pm_errno = 0;
 	return pkg->ops->get_installdate(pkg);
 }
 
 const char SYMEXPORT *alpm_pkg_get_packager(pmpkg_t *pkg)
 {
+	pkg->handle->pm_errno = 0;
 	return pkg->ops->get_packager(pkg);
 }
 
 const char SYMEXPORT *alpm_pkg_get_md5sum(pmpkg_t *pkg)
 {
+	pkg->handle->pm_errno = 0;
 	return pkg->ops->get_md5sum(pkg);
 }
 
 const char SYMEXPORT *alpm_pkg_get_arch(pmpkg_t *pkg)
 {
+	pkg->handle->pm_errno = 0;
 	return pkg->ops->get_arch(pkg);
 }
 
 off_t SYMEXPORT alpm_pkg_get_size(pmpkg_t *pkg)
 {
+	pkg->handle->pm_errno = 0;
 	return pkg->ops->get_size(pkg);
 }
 
 off_t SYMEXPORT alpm_pkg_get_isize(pmpkg_t *pkg)
 {
+	pkg->handle->pm_errno = 0;
 	return pkg->ops->get_isize(pkg);
 }
 
 pmpkgreason_t SYMEXPORT alpm_pkg_get_reason(pmpkg_t *pkg)
 {
+	pkg->handle->pm_errno = 0;
 	return pkg->ops->get_reason(pkg);
 }
 
 alpm_list_t SYMEXPORT *alpm_pkg_get_licenses(pmpkg_t *pkg)
 {
+	pkg->handle->pm_errno = 0;
 	return pkg->ops->get_licenses(pkg);
 }
 
 alpm_list_t SYMEXPORT *alpm_pkg_get_groups(pmpkg_t *pkg)
 {
+	pkg->handle->pm_errno = 0;
 	return pkg->ops->get_groups(pkg);
 }
 
 alpm_list_t SYMEXPORT *alpm_pkg_get_depends(pmpkg_t *pkg)
 {
+	pkg->handle->pm_errno = 0;
 	return pkg->ops->get_depends(pkg);
 }
 
 alpm_list_t SYMEXPORT *alpm_pkg_get_optdepends(pmpkg_t *pkg)
 {
+	pkg->handle->pm_errno = 0;
 	return pkg->ops->get_optdepends(pkg);
 }
 
 alpm_list_t SYMEXPORT *alpm_pkg_get_conflicts(pmpkg_t *pkg)
 {
+	pkg->handle->pm_errno = 0;
 	return pkg->ops->get_conflicts(pkg);
 }
 
 alpm_list_t SYMEXPORT *alpm_pkg_get_provides(pmpkg_t *pkg)
 {
+	pkg->handle->pm_errno = 0;
 	return pkg->ops->get_provides(pkg);
 }
 
 alpm_list_t SYMEXPORT *alpm_pkg_get_replaces(pmpkg_t *pkg)
 {
+	pkg->handle->pm_errno = 0;
 	return pkg->ops->get_replaces(pkg);
 }
 
 alpm_list_t SYMEXPORT *alpm_pkg_get_deltas(pmpkg_t *pkg)
 {
+	pkg->handle->pm_errno = 0;
 	return pkg->ops->get_deltas(pkg);
 }
 
 alpm_list_t SYMEXPORT *alpm_pkg_get_files(pmpkg_t *pkg)
 {
+	pkg->handle->pm_errno = 0;
 	return pkg->ops->get_files(pkg);
 }
 
 alpm_list_t SYMEXPORT *alpm_pkg_get_backup(pmpkg_t *pkg)
 {
+	pkg->handle->pm_errno = 0;
 	return pkg->ops->get_backup(pkg);
 }
 
@@ -280,6 +305,7 @@ pmdb_t SYMEXPORT *alpm_pkg_get_db(pmpkg_t *pkg)
 	/* Sanity checks */
 	ASSERT(pkg != NULL, return NULL);
 	ASSERT(pkg->origin != PKG_FROM_FILE, return NULL);
+	pkg->handle->pm_errno = 0;
 
 	return pkg->origin_data.db;
 }
@@ -287,6 +313,7 @@ pmdb_t SYMEXPORT *alpm_pkg_get_db(pmpkg_t *pkg)
 /** Open a package changelog for reading. */
 void SYMEXPORT *alpm_pkg_changelog_open(pmpkg_t *pkg)
 {
+	pkg->handle->pm_errno = 0;
 	return pkg->ops->changelog_open(pkg);
 }
 
@@ -294,6 +321,7 @@ void SYMEXPORT *alpm_pkg_changelog_open(pmpkg_t *pkg)
 size_t SYMEXPORT alpm_pkg_changelog_read(void *ptr, size_t size,
 		const pmpkg_t *pkg, const void *fp)
 {
+	pkg->handle->pm_errno = 0;
 	return pkg->ops->changelog_read(ptr, size, pkg, fp);
 }
 
@@ -307,17 +335,21 @@ int SYMEXPORT alpm_pkg_changelog_feof(const pmpkg_t *pkg, void *fp)
 /** Close a package changelog for reading. */
 int SYMEXPORT alpm_pkg_changelog_close(const pmpkg_t *pkg, void *fp)
 {
+	pkg->handle->pm_errno = 0;
 	return pkg->ops->changelog_close(pkg, fp);
 }
 
 int SYMEXPORT alpm_pkg_has_scriptlet(pmpkg_t *pkg)
 {
+	pkg->handle->pm_errno = 0;
 	return pkg->ops->has_scriptlet(pkg);
 }
 
 static void find_requiredby(pmpkg_t *pkg, pmdb_t *db, alpm_list_t **reqs)
 {
 	const alpm_list_t *i;
+	pkg->handle->pm_errno = 0;
+
 	for(i = _alpm_db_get_pkgcache(db); i; i = i->next) {
 		pmpkg_t *cachepkg = i->data;
 		alpm_list_t *i;
@@ -339,6 +371,8 @@ alpm_list_t SYMEXPORT *alpm_pkg_compute_requiredby(pmpkg_t *pkg)
 	alpm_list_t *reqs = NULL;
 	pmdb_t *db;
 
+	pkg->handle->pm_errno = 0;
+
 	if(pkg->origin == PKG_FROM_FILE) {
 		/* The sane option; search locally for things that require this. */
 		find_requiredby(pkg, pkg->handle->db_local, &reqs);
diff --git a/lib/libalpm/remove.c b/lib/libalpm/remove.c
index eada9b9..9f07501 100644
--- a/lib/libalpm/remove.c
+++ b/lib/libalpm/remove.c
@@ -50,7 +50,7 @@ int SYMEXPORT alpm_remove_pkg(pmhandle_t *handle, pmpkg_t *pkg)
 	pmtrans_t *trans;
 
 	/* Sanity checks */
-	ASSERT(handle != NULL, return -1);
+	CHECK_HANDLE(handle, return -1);
 	ASSERT(pkg != NULL, RET_ERR(handle, PM_ERR_WRONG_ARGS, -1));
 	ASSERT(handle == pkg->handle, RET_ERR(handle, PM_ERR_WRONG_ARGS, -1));
 	trans = handle->trans;
diff --git a/lib/libalpm/signing.c b/lib/libalpm/signing.c
index 8124e67..62c8925 100644
--- a/lib/libalpm/signing.c
+++ b/lib/libalpm/signing.c
@@ -386,6 +386,7 @@ pgp_verify_t _alpm_db_get_sigverify_level(pmdb_t *db)
 int SYMEXPORT alpm_pkg_check_pgp_signature(pmpkg_t *pkg)
 {
 	ASSERT(pkg != NULL, return 0);
+	pkg->handle->pm_errno = 0;
 
 	return _alpm_gpgme_checksig(pkg->handle, alpm_pkg_get_filename(pkg),
 			pkg->base64_sig);
@@ -399,6 +400,7 @@ int SYMEXPORT alpm_pkg_check_pgp_signature(pmpkg_t *pkg)
 int SYMEXPORT alpm_db_check_pgp_signature(pmdb_t *db)
 {
 	ASSERT(db != NULL, return 0);
+	db->handle->pm_errno = 0;
 
 	return _alpm_gpgme_checksig(db->handle, _alpm_db_path(db), NULL);
 }
diff --git a/lib/libalpm/sync.c b/lib/libalpm/sync.c
index d65f5e2..0573573 100644
--- a/lib/libalpm/sync.c
+++ b/lib/libalpm/sync.c
@@ -55,11 +55,12 @@
  */
 pmpkg_t SYMEXPORT *alpm_sync_newversion(pmpkg_t *pkg, alpm_list_t *dbs_sync)
 {
-	ASSERT(pkg != NULL, return NULL);
-
 	alpm_list_t *i;
 	pmpkg_t *spkg = NULL;
 
+	ASSERT(pkg != NULL, return NULL);
+	pkg->handle->pm_errno = 0;
+
 	for(i = dbs_sync; !spkg && i; i = i->next) {
 		spkg = _alpm_db_get_pkgfromcache(i->data, alpm_pkg_get_name(pkg));
 	}
@@ -89,7 +90,7 @@ int SYMEXPORT alpm_sync_sysupgrade(pmhandle_t *handle, int enable_downgrade)
 	pmdb_t *db_local;
 	alpm_list_t *dbs_sync;
 
-	ASSERT(handle != NULL, return -1);
+	CHECK_HANDLE(handle, return -1);
 	trans = handle->trans;
 	db_local = handle->db_local;
 	dbs_sync = handle->dbs_sync;
diff --git a/lib/libalpm/trans.c b/lib/libalpm/trans.c
index a95280c..f074493 100644
--- a/lib/libalpm/trans.c
+++ b/lib/libalpm/trans.c
@@ -105,7 +105,7 @@ int SYMEXPORT alpm_trans_init(pmhandle_t *handle, pmtransflag_t flags,
 	int db_version;
 
 	/* Sanity checks */
-	ASSERT(handle != NULL, return -1);
+	CHECK_HANDLE(handle, return -1);
 	ASSERT(handle->trans == NULL, RET_ERR(handle, PM_ERR_TRANS_NOT_NULL, -1));
 
 	/* lock db */
@@ -168,7 +168,7 @@ int SYMEXPORT alpm_trans_prepare(pmhandle_t *handle, alpm_list_t **data)
 	pmtrans_t *trans;
 
 	/* Sanity checks */
-	ASSERT(handle != NULL, return -1);
+	CHECK_HANDLE(handle, return -1);
 	ASSERT(data != NULL, RET_ERR(handle, PM_ERR_WRONG_ARGS, -1));
 
 	trans = handle->trans;
@@ -212,7 +212,7 @@ int SYMEXPORT alpm_trans_commit(pmhandle_t *handle, alpm_list_t **data)
 	pmtrans_t *trans;
 
 	/* Sanity checks */
-	ASSERT(handle != NULL, return -1);
+	CHECK_HANDLE(handle, return -1);
 
 	trans = handle->trans;
 
@@ -251,7 +251,7 @@ int SYMEXPORT alpm_trans_interrupt(pmhandle_t *handle)
 	pmtrans_t *trans;
 
 	/* Sanity checks */
-	ASSERT(handle != NULL, return -1);
+	CHECK_HANDLE(handle, return -1);
 
 	trans = handle->trans;
 	ASSERT(trans != NULL, RET_ERR(handle, PM_ERR_TRANS_NULL, -1));
@@ -269,7 +269,7 @@ int SYMEXPORT alpm_trans_release(pmhandle_t *handle)
 	pmtrans_t *trans;
 
 	/* Sanity checks */
-	ASSERT(handle != NULL, return -1);
+	CHECK_HANDLE(handle, return -1);
 
 	trans = handle->trans;
 	ASSERT(trans != NULL, RET_ERR(handle, PM_ERR_TRANS_NULL, -1));
@@ -414,7 +414,7 @@ cleanup:
 pmtransflag_t SYMEXPORT alpm_trans_get_flags(pmhandle_t *handle)
 {
 	/* Sanity checks */
-	ASSERT(handle != NULL, return -1);
+	CHECK_HANDLE(handle, return -1);
 	ASSERT(handle->trans != NULL, RET_ERR(handle, PM_ERR_TRANS_NULL, -1));
 
 	return handle->trans->flags;
@@ -423,7 +423,7 @@ pmtransflag_t SYMEXPORT alpm_trans_get_flags(pmhandle_t *handle)
 alpm_list_t SYMEXPORT *alpm_trans_get_add(pmhandle_t *handle)
 {
 	/* Sanity checks */
-	ASSERT(handle != NULL, return NULL);
+	CHECK_HANDLE(handle, return NULL);
 	ASSERT(handle->trans != NULL, RET_ERR(handle, PM_ERR_TRANS_NULL, NULL));
 
 	return handle->trans->add;
@@ -432,7 +432,7 @@ alpm_list_t SYMEXPORT *alpm_trans_get_add(pmhandle_t *handle)
 alpm_list_t SYMEXPORT *alpm_trans_get_remove(pmhandle_t *handle)
 {
 	/* Sanity checks */
-	ASSERT(handle != NULL, return NULL);
+	CHECK_HANDLE(handle, return NULL);
 	ASSERT(handle->trans != NULL, RET_ERR(handle, PM_ERR_TRANS_NULL, NULL));
 
 	return handle->trans->remove;
diff --git a/lib/libalpm/util.h b/lib/libalpm/util.h
index e5fefe9..0549c81 100644
--- a/lib/libalpm/util.h
+++ b/lib/libalpm/util.h
@@ -71,6 +71,8 @@
 
 #define DOUBLE_EQ(x, y) (fabs((x) - (y)) < DBL_EPSILON)
 
+#define CHECK_HANDLE(handle, action) do { if(!(handle)) { action; } (handle)->pm_errno = 0; } while(0)
+
 /**
  * Used as a buffer/state holder for _alpm_archive_fgets().
  */
-- 
1.7.5.2



More information about the pacman-dev mailing list