1
0
mirror of https://github.com/moparisthebest/pacman synced 2024-12-22 07:48:50 -05:00

Always use proper error code in alpm_initialize.

In out of memory conditions, an undefined error value is written
into *err, because myerr is never explicitly set in these cases.

I have also converted a calloc into a MALLOC call, because the memory
will be properly filled by the snprintf call right after it.

Signed-off-by: Tobias Stoeckmann <tobias@stoeckmann.org>
Signed-off-by: Allan McRae <allan@archlinux.org>
This commit is contained in:
Tobias Stoeckmann 2016-06-05 19:23:31 +02:00 committed by Allan McRae
parent 0f0b192d8a
commit 80d97fcf75

View File

@ -55,8 +55,7 @@ alpm_handle_t SYMEXPORT *alpm_initialize(const char *root, const char *dbpath,
alpm_handle_t *myhandle = _alpm_handle_new(); alpm_handle_t *myhandle = _alpm_handle_new();
if(myhandle == NULL) { if(myhandle == NULL) {
myerr = ALPM_ERR_MEMORY; goto nomem;
goto cleanup;
} }
if((myerr = _alpm_set_directory_option(root, &(myhandle->root), 1))) { if((myerr = _alpm_set_directory_option(root, &(myhandle->root), 1))) {
goto cleanup; goto cleanup;
@ -68,15 +67,15 @@ alpm_handle_t SYMEXPORT *alpm_initialize(const char *root, const char *dbpath,
/* to contatenate myhandle->root (ends with a slash) with SYSHOOKDIR (starts /* to contatenate myhandle->root (ends with a slash) with SYSHOOKDIR (starts
* with a slash) correctly, we skip SYSHOOKDIR[0]; the regular +1 therefore * with a slash) correctly, we skip SYSHOOKDIR[0]; the regular +1 therefore
* disappears from the allocation */ * disappears from the allocation */
MALLOC(hookdir, strlen(myhandle->root) + strlen(SYSHOOKDIR), goto cleanup); MALLOC(hookdir, strlen(myhandle->root) + strlen(SYSHOOKDIR), goto nomem);
sprintf(hookdir, "%s%s", myhandle->root, SYSHOOKDIR + 1); sprintf(hookdir, "%s%s", myhandle->root, SYSHOOKDIR + 1);
myhandle->hookdirs = alpm_list_add(NULL, hookdir); myhandle->hookdirs = alpm_list_add(NULL, hookdir);
/* set default database extension */ /* set default database extension */
STRDUP(myhandle->dbext, ".db", goto cleanup); STRDUP(myhandle->dbext, ".db", goto nomem);
lockfilelen = strlen(myhandle->dbpath) + strlen(lf) + 1; lockfilelen = strlen(myhandle->dbpath) + strlen(lf) + 1;
myhandle->lockfile = calloc(lockfilelen, sizeof(char)); MALLOC(myhandle->lockfile, lockfilelen, goto nomem);
snprintf(myhandle->lockfile, lockfilelen, "%s%s", myhandle->dbpath, lf); snprintf(myhandle->lockfile, lockfilelen, "%s%s", myhandle->dbpath, lf);
if(_alpm_db_register_local(myhandle) == NULL) { if(_alpm_db_register_local(myhandle) == NULL) {
@ -90,9 +89,11 @@ alpm_handle_t SYMEXPORT *alpm_initialize(const char *root, const char *dbpath,
return myhandle; return myhandle;
nomem:
myerr = ALPM_ERR_MEMORY;
cleanup: cleanup:
_alpm_handle_free(myhandle); _alpm_handle_free(myhandle);
if(err && myerr) { if(err) {
*err = myerr; *err = myerr;
} }
return NULL; return NULL;