From 6e6d3f18e3a8d4cd4376c0922fdcaad354d35359 Mon Sep 17 00:00:00 2001 From: morganamilo Date: Sat, 3 Feb 2024 16:25:09 +0000 Subject: [PATCH] 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 --- lib/libalpm/be_local.c | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/lib/libalpm/be_local.c b/lib/libalpm/be_local.c index 349e84b7..3516587d 100644 --- a/lib/libalpm/be_local.c +++ b/lib/libalpm/be_local.c @@ -654,6 +654,17 @@ static int local_db_populate(alpm_db_t *db) 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 */ char *_alpm_local_db_pkgpath(alpm_db_t *db, alpm_pkg_t *info, const char *filename) @@ -776,7 +787,7 @@ static int local_db_read(alpm_pkg_t *info, int inforeq) READ_AND_STORE(info->packager); } else if(strcmp(line, "%REASON%") == 0) { READ_NEXT(); - info->reason = (alpm_pkgreason_t)atoi(line); + info->reason = _read_pkgreason(db->handle, info->name, line); } else if(strcmp(line, "%VALIDATION%") == 0) { alpm_list_t *i, *v = NULL; READ_AND_STORE_ALL(v);