From 36d98b3919d37c7f376c9f0f787a3b4b660da73e Mon Sep 17 00:00:00 2001 From: Dan McGee Date: Mon, 20 Jun 2011 00:18:29 -0500 Subject: Improve cachedir removal and error handling * Check the return value of canonicalize_path() for non-NULL * Use ASSERT and RET_ERR as appropriate * Make remove_cachedir() use same path munge logic as add_cachedir() Signed-off-by: Dan McGee --- lib/libalpm/handle.c | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) (limited to 'lib/libalpm/handle.c') diff --git a/lib/libalpm/handle.c b/lib/libalpm/handle.c index 71a526e6..b535e0f3 100644 --- a/lib/libalpm/handle.c +++ b/lib/libalpm/handle.c @@ -276,6 +276,9 @@ enum _pmerrno_t _alpm_set_directory_option(const char *value, FREE(*storage); } *storage = canonicalize_path(path); + if(!*storage) { + return PM_ERR_MEMORY; + } free(real); return 0; } @@ -285,14 +288,14 @@ int SYMEXPORT alpm_option_add_cachedir(pmhandle_t *handle, const char *cachedir) char *newcachedir; CHECK_HANDLE(handle, return -1); - if(!cachedir) { - handle->pm_errno = PM_ERR_WRONG_ARGS; - return -1; - } + ASSERT(cachedir != NULL, RET_ERR(handle, PM_ERR_WRONG_ARGS, -1)); /* don't stat the cachedir yet, as it may not even be needed. we can * fail later if it is needed and the path is invalid. */ newcachedir = canonicalize_path(cachedir); + if(!newcachedir) { + RET_ERR(handle, PM_ERR_MEMORY, -1); + } handle->cachedirs = alpm_list_add(handle->cachedirs, newcachedir); _alpm_log(handle, PM_LOG_DEBUG, "option 'cachedir' = %s\n", newcachedir); return 0; @@ -318,16 +321,13 @@ int SYMEXPORT alpm_option_remove_cachedir(pmhandle_t *handle, const char *cached { char *vdata = NULL; char *newcachedir; - size_t cachedirlen; CHECK_HANDLE(handle, return -1); - /* verify cachedir ends in a '/' */ - cachedirlen = strlen(cachedir); - if(cachedir[cachedirlen-1] != '/') { - cachedirlen += 1; + ASSERT(cachedir != NULL, RET_ERR(handle, PM_ERR_WRONG_ARGS, -1)); + + newcachedir = canonicalize_path(cachedir); + if(!newcachedir) { + RET_ERR(handle, PM_ERR_MEMORY, -1); } - CALLOC(newcachedir, cachedirlen + 1, sizeof(char), return 0); - strncpy(newcachedir, cachedir, cachedirlen); - newcachedir[cachedirlen-1] = '/'; handle->cachedirs = alpm_list_remove_str(handle->cachedirs, newcachedir, &vdata); FREE(newcachedir); if(vdata != NULL) { -- cgit v1.2.3-24-g4f1b