libalpm: don't use atio for pkgreason

atio's behaviour is undefined if the input is not valid. Also it does
all sorts of whitespace and prefix handling which we don't need for
pkgreason.

Instead of going into UB on invalid input we now return EXPLICIT as the
fallback and print an error. However we don't actually error out as the
DB parsing tries to be error tolerant.

Signed-off-by: Allan McRae <allan@archlinux.org>
This commit is contained in:
morganamilo 2024-02-03 16:25:09 +00:00 committed by Allan McRae
parent 0a394144b2
commit 6e6d3f18e3

View file

@ -654,6 +654,17 @@ static int local_db_populate(alpm_db_t *db)
return 0; return 0;
} }
static alpm_pkgreason_t _read_pkgreason(alpm_handle_t *handle, const char *pkgname, const char *line) {
if(strcmp(line, "0") == 0) {
return ALPM_PKG_REASON_EXPLICIT;
} else if(strcmp(line, "1") == 0) {
return ALPM_PKG_REASON_DEPEND;
} else {
_alpm_log(handle, ALPM_LOG_ERROR, _("unknown install reason for package %s: %s\n"), pkgname, line);
return ALPM_PKG_REASON_EXPLICIT;
}
}
/* Note: the return value must be freed by the caller */ /* Note: the return value must be freed by the caller */
char *_alpm_local_db_pkgpath(alpm_db_t *db, alpm_pkg_t *info, char *_alpm_local_db_pkgpath(alpm_db_t *db, alpm_pkg_t *info,
const char *filename) const char *filename)
@ -776,7 +787,7 @@ static int local_db_read(alpm_pkg_t *info, int inforeq)
READ_AND_STORE(info->packager); READ_AND_STORE(info->packager);
} else if(strcmp(line, "%REASON%") == 0) { } else if(strcmp(line, "%REASON%") == 0) {
READ_NEXT(); READ_NEXT();
info->reason = (alpm_pkgreason_t)atoi(line); info->reason = _read_pkgreason(db->handle, info->name, line);
} else if(strcmp(line, "%VALIDATION%") == 0) { } else if(strcmp(line, "%VALIDATION%") == 0) {
alpm_list_t *i, *v = NULL; alpm_list_t *i, *v = NULL;
READ_AND_STORE_ALL(v); READ_AND_STORE_ALL(v);