lib/util.c: verify revision in xbps_pkg_{version,revision,name}

This commit is contained in:
Piotr Wójcik 2019-09-26 22:27:15 +02:00 committed by Juan RP
parent 7b4a925302
commit 699b2bdd3b
2 changed files with 47 additions and 20 deletions

View File

@ -50,6 +50,16 @@
#pragma clang diagnostic ignored "-Wformat-nonliteral" #pragma clang diagnostic ignored "-Wformat-nonliteral"
#endif #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 * @file lib/util.c
* @brief Utility routines * @brief Utility routines
@ -117,16 +127,22 @@ xbps_pkg_is_ignored(struct xbps_handle *xhp, const char *pkg)
const char * const char *
xbps_pkg_version(const char *pkg) xbps_pkg_version(const char *pkg)
{ {
const char *p; const char *p, *r;
size_t p_len;
if ((p = strrchr(pkg, '-')) == NULL) if ((p = strrchr(pkg, '-')) == NULL)
return 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] == '_') if (p[i] == '_')
break; break;
if (isdigit((unsigned char)p[i]) && strchr(p, '_')) { if (isdigit((unsigned char)p[i]) && (r = strchr(p + i + 1, '_'))) {
return p + 1; /* skip first '-' */ if (!is_numeric(r + 1)) {
break;
}
return p;
} }
} }
return NULL; return NULL;
@ -208,36 +224,47 @@ xbps_binpkg_arch(const char *pkg)
const char * const char *
xbps_pkg_revision(const char *pkg) xbps_pkg_revision(const char *pkg)
{ {
const char *p; const char *p, *r;
size_t p_len;
assert(pkg != NULL); if ((p = strrchr(pkg, '-')) == NULL)
/* Get the required revision */
if ((p = strrchr(pkg, '_')) == NULL)
return NULL; return NULL;
if (!isdigit((unsigned char)p[1])) ++p; /* skip first '-' */
return NULL; p_len = strlen(p);
for (unsigned int i = 0; i < p_len; i++) {
return p + 1; /* skip first '_' */ 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 * char *
xbps_pkg_name(const char *pkg) xbps_pkg_name(const char *pkg)
{ {
const char *p; const char *p, *r;
char *buf; char *buf;
unsigned int len; unsigned int len;
size_t p_len;
bool valid = false; bool valid = false;
if ((p = strrchr(pkg, '-')) == NULL) if ((p = strrchr(pkg, '-')) == NULL)
return 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] == '_') if (p[i] == '_')
break; break;
if (isdigit((unsigned char)p[i]) && strchr(p, '_')) { if (isdigit((unsigned char)p[i]) && (r = strchr(p + i + 1, '_'))) {
valid = true; valid = is_numeric(r + 1);
break; break;
} }
} }

View File

@ -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("python-e_dbus"), NULL);
ATF_CHECK_EQ(xbps_pkg_name("fs-utils-v1"), 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("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"), NULL);
ATF_CHECK_EQ(xbps_pkg_version("font-adobe-100dpi-7.8"), 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"), NULL);
ATF_CHECK_EQ(xbps_pkg_version("python-e_dbus-1"), 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("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("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_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-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("python-e_dbus-1_1"), "1_1");
ATF_REQUIRE_STREQ(xbps_pkg_version("fs-utils-v1_1"), "v1_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_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");
ATF_REQUIRE_STREQ(xbps_pkgpattern_name("systemd>43"), "systemd"); ATF_REQUIRE_STREQ(xbps_pkgpattern_name("systemd>43"), "systemd");