util.c: add stricter checks for pkgver conformance.

Make xbps_pkg_{name,version} return NULL if next character
after the last '-' character is not a digit, and if there's a digit
make sure that there exists a '_' character too.

Added more tests to the testsuite to catch this. With these changes
'python-e_dbus' is properly detected and validated.
This commit is contained in:
Juan RP 2013-11-17 11:34:14 +01:00
parent d0380d5cd2
commit c44d7070a4
2 changed files with 21 additions and 3 deletions

View File

@ -38,6 +38,7 @@
#include <string.h> #include <string.h>
#include <errno.h> #include <errno.h>
#include <fnmatch.h> #include <fnmatch.h>
#include <ctype.h>
#include <sys/utsname.h> #include <sys/utsname.h>
#include "xbps_api_impl.h" #include "xbps_api_impl.h"
@ -96,7 +97,10 @@ xbps_pkg_version(const char *pkg)
if ((p = strrchr(pkg, '-')) == NULL) if ((p = strrchr(pkg, '-')) == NULL)
return NULL; return NULL;
if (strrchr(p, '_') == NULL) if (!isdigit((unsigned char)p[1]))
return NULL;
if (strchr(p, '_') == NULL)
return NULL; return NULL;
return p + 1; /* skip first '_' */ return p + 1; /* skip first '_' */
@ -113,6 +117,9 @@ xbps_pkg_revision(const char *pkg)
if ((p = strrchr(pkg, '_')) == NULL) if ((p = strrchr(pkg, '_')) == NULL)
return NULL; return NULL;
if (!isdigit((unsigned char)p[1]))
return NULL;
return p + 1; /* skip first '_' */ return p + 1; /* skip first '_' */
} }
@ -126,7 +133,10 @@ xbps_pkg_name(const char *pkg)
if ((p = strrchr(pkg, '-')) == NULL) if ((p = strrchr(pkg, '-')) == NULL)
return NULL; return NULL;
if (strrchr(p, '_') == NULL) if (!isdigit((unsigned char)p[1]))
return NULL;
if (strchr(p, '_') == NULL)
return NULL; return NULL;
len = strlen(pkg) - strlen(p) + 1; len = strlen(pkg) - strlen(p) + 1;

View File

@ -1,5 +1,5 @@
/*- /*-
* Copyright (c) 2012 Juan Romero Pardines. * Copyright (c) 2012-2013 Juan Romero Pardines.
* All rights reserved. * All rights reserved.
* *
* Redistribution and use in source and binary forms, with or without * Redistribution and use in source and binary forms, with or without
@ -36,14 +36,22 @@ ATF_TC_HEAD(util_test, tc)
ATF_TC_BODY(util_test, tc) ATF_TC_BODY(util_test, tc)
{ {
ATF_CHECK_EQ(xbps_pkg_name("font-adobe-a"), NULL);
ATF_CHECK_EQ(xbps_pkg_name("font-adobe-1"), NULL);
ATF_CHECK_EQ(xbps_pkg_name("font-adobe-100dpi"), NULL);
ATF_CHECK_EQ(xbps_pkg_name("font-adobe-100dpi-7.8"), NULL); ATF_CHECK_EQ(xbps_pkg_name("font-adobe-100dpi-7.8"), NULL);
ATF_CHECK_EQ(xbps_pkg_name("python-e_dbus"), 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-1"), 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("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-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("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_revision("systemd-43_1_0"), "0"); 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");