From 699b2bdd3b98b086ac989af3cf034135a7a6f570 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Piotr=20W=C3=B3jcik?= Date: Thu, 26 Sep 2019 22:27:15 +0200 Subject: [PATCH] lib/util.c: verify revision in xbps_pkg_{version,revision,name} --- lib/util.c | 61 ++++++++++++++++++++++++---------- tests/xbps/libxbps/util/main.c | 6 ++-- 2 files changed, 47 insertions(+), 20 deletions(-) diff --git a/lib/util.c b/lib/util.c index 41e7f389..059e1a03 100644 --- a/lib/util.c +++ b/lib/util.c @@ -50,6 +50,16 @@ #pragma clang diagnostic ignored "-Wformat-nonliteral" #endif +static bool is_numeric(const char *str) { + if (str == NULL || str[0] == '\0'){ + return false; + } + while (isdigit(str[0])) { + ++str; + } + return str[0] == '\0'; +} + /** * @file lib/util.c * @brief Utility routines @@ -117,16 +127,22 @@ xbps_pkg_is_ignored(struct xbps_handle *xhp, const char *pkg) const char * xbps_pkg_version(const char *pkg) { - const char *p; + const char *p, *r; + size_t p_len; if ((p = strrchr(pkg, '-')) == NULL) return NULL; - for (unsigned int i = 0; i < strlen(p); i++) { + ++p; /* skip first '-' */ + p_len = strlen(p); + for (unsigned int i = 0; i < p_len; i++) { if (p[i] == '_') break; - if (isdigit((unsigned char)p[i]) && strchr(p, '_')) { - return p + 1; /* skip first '-' */ + if (isdigit((unsigned char)p[i]) && (r = strchr(p + i + 1, '_'))) { + if (!is_numeric(r + 1)) { + break; + } + return p; } } return NULL; @@ -208,36 +224,47 @@ xbps_binpkg_arch(const char *pkg) const char * xbps_pkg_revision(const char *pkg) { - const char *p; + const char *p, *r; + size_t p_len; - assert(pkg != NULL); - - /* Get the required revision */ - if ((p = strrchr(pkg, '_')) == NULL) + if ((p = strrchr(pkg, '-')) == NULL) return NULL; - if (!isdigit((unsigned char)p[1])) - return NULL; - - return p + 1; /* skip first '_' */ + ++p; /* skip first '-' */ + p_len = strlen(p); + for (unsigned int i = 0; i < p_len; i++) { + if (p[i] == '_') + break; + if (isdigit((unsigned char)p[i]) && (r = strchr(p + i + 1, '_'))) { + ++r; /* skip first '_' */ + if (!is_numeric(r)) { + break; + } + return r; + } + } + return NULL; } char * xbps_pkg_name(const char *pkg) { - const char *p; + const char *p, *r; char *buf; unsigned int len; + size_t p_len; bool valid = false; if ((p = strrchr(pkg, '-')) == NULL) return NULL; - for (unsigned int i = 0; i < strlen(p); i++) { + p_len = strlen(p); + /* i = 1 skips first '-' */ + for (unsigned int i = 1; i < p_len; i++) { if (p[i] == '_') break; - if (isdigit((unsigned char)p[i]) && strchr(p, '_')) { - valid = true; + if (isdigit((unsigned char)p[i]) && (r = strchr(p + i + 1, '_'))) { + valid = is_numeric(r + 1); break; } } diff --git a/tests/xbps/libxbps/util/main.c b/tests/xbps/libxbps/util/main.c index 9ae88f1c..41aac6ee 100644 --- a/tests/xbps/libxbps/util/main.c +++ b/tests/xbps/libxbps/util/main.c @@ -43,19 +43,19 @@ ATF_TC_BODY(util_test, tc) ATF_CHECK_EQ(xbps_pkg_name("python-e_dbus"), NULL); ATF_CHECK_EQ(xbps_pkg_name("fs-utils-v1"), NULL); ATF_CHECK_EQ(xbps_pkg_name("fs-utils-v_1"), NULL); + ATF_CHECK_EQ(xbps_pkg_name("font-adobe-100dpi-1.8_blah"), NULL); ATF_CHECK_EQ(xbps_pkg_version("font-adobe-100dpi"), NULL); ATF_CHECK_EQ(xbps_pkg_version("font-adobe-100dpi-7.8"), NULL); ATF_CHECK_EQ(xbps_pkg_version("python-e_dbus"), NULL); ATF_CHECK_EQ(xbps_pkg_version("python-e_dbus-1"), NULL); + ATF_CHECK_EQ(xbps_pkg_version("font-adobe-100dpi-1.8_blah"), NULL); + ATF_CHECK_EQ(xbps_pkg_revision("systemd-43_1_0"), NULL); ATF_REQUIRE_STREQ(xbps_pkg_name("font-adobe-100dpi-7.8_2"), "font-adobe-100dpi"); ATF_REQUIRE_STREQ(xbps_pkg_name("systemd-43_1"), "systemd"); - ATF_REQUIRE_STREQ(xbps_pkg_name("font-adobe-100dpi-1.8_blah"), "font-adobe-100dpi"); ATF_REQUIRE_STREQ(xbps_pkg_name("python-e_dbus-1.0_1"), "python-e_dbus"); ATF_REQUIRE_STREQ(xbps_pkg_version("font-adobe-100dpi-7.8_2"), "7.8_2"); - ATF_REQUIRE_STREQ(xbps_pkg_version("font-adobe-100dpi-1.8_blah"), "1.8_blah"); ATF_REQUIRE_STREQ(xbps_pkg_version("python-e_dbus-1_1"), "1_1"); ATF_REQUIRE_STREQ(xbps_pkg_version("fs-utils-v1_1"), "v1_1"); - ATF_REQUIRE_STREQ(xbps_pkg_revision("systemd-43_1_0"), "0"); ATF_REQUIRE_STREQ(xbps_pkg_revision("systemd_21-43_0"), "0"); ATF_REQUIRE_STREQ(xbps_pkgpattern_name("systemd>=43"), "systemd"); ATF_REQUIRE_STREQ(xbps_pkgpattern_name("systemd>43"), "systemd");