commit:     99cc42fbfa03d6c79fa003b8f490f99e29615f8b
Author:     Michał Górny <mgorny <AT> gentoo <DOT> org>
AuthorDate: Thu Sep 21 06:57:19 2017 +0000
Commit:     Michał Górny <mgorny <AT> gentoo <DOT> org>
CommitDate: Thu Sep 21 07:04:28 2017 +0000
URL:        https://gitweb.gentoo.org/repo/gentoo.git/commit/?id=99cc42fb

eapi7-ver.eclass: Verify input for ver_test

 eclass/eapi7-ver.eclass   | 78 +++++++++++++++++++++++++++++++++++++++++++++++
 eclass/tests/eapi7-ver.sh | 18 +++++------
 2 files changed, 87 insertions(+), 9 deletions(-)

diff --git a/eclass/eapi7-ver.eclass b/eclass/eapi7-ver.eclass
index e833835a56e..94184c484b0 100644
--- a/eclass/eapi7-ver.eclass
+++ b/eclass/eapi7-ver.eclass
@@ -176,6 +176,81 @@ ver_rs() {
        echo "${comp[*]}"
 }
 
+# @FUNCTION: _ver_validate
+# @USAGE: <comp[0]>...
+# @DESCRIPTION:
+# Verify that the version component array passed as the argument
+# validates according to the PMS version rules. Returns 0 if it does,
+# 1 otherwise.
+_ver_validate() {
+       local prev=start
+
+       while [[ ${1} || ${2} ]]; do
+               local s=${1}
+               local c=${2}
+
+               if [[ -z ${s} ]]; then
+                       if [[ ${c} == [0-9]* ]]; then
+                               # number without preceding sep may be either:
+                               case ${prev} in
+                                       # a. 1st version number
+                                       start) prev=numeric;;
+                                       # b. _foo suffix number
+                                       suffix) prev=suffix_num;;
+                                       # c. -rN revision number
+                                       revision) prev=revision_num;;
+                                       *) return 1;;
+                               esac
+                       elif [[ -n ${c} ]]; then
+                               # letter without preceding sep = letter after 
version
+                               [[ ${prev} == numeric ]] || return 1
+                               [[ ${#c} -eq 1 ]] || return 1
+                               prev=letter
+                       fi
+               elif [[ -z ${c} ]]; then
+                       # trailing suffix?
+                       return 1
+               elif [[ ${s} == . ]]; then
+                       # number preceded by dot = numeric component
+                       [[ ${prev} == numeric ]] || return 1
+               elif [[ ${s} == _ ]]; then
+                       # _ implies _foo suffix
+                       case ${prev} in
+                               numeric|letter|suffix|suffix_num) ;;
+                               *) return 1;;
+                       esac
+
+                       case ${c} in
+                               alpha) ;;
+                               beta) ;;
+                               rc) ;;
+                               pre) ;;
+                               p) ;;
+                               *) return 1;;
+                       esac
+                       prev=suffix
+               elif [[ ${s} == - ]]; then
+                       # - implies revision
+                       case ${prev} in
+                               numeric|letter|suffix|suffix_num) ;;
+                               *) return 1;;
+                       esac
+
+                       [[ ${c} == r ]] || return 1
+                       prev=revision
+               else
+                       return 1
+               fi
+
+               shift 2
+       done
+
+       # empty version string?
+       [[ ${prev} != start ]] || return 1
+
+       return 0
+}
+
 # @FUNCTION: ver_test
 # @USAGE: [<v1>] <op> <v2>
 # @DESCRIPTION:
@@ -216,6 +291,9 @@ ver_test() {
        compb=( "${comp[@]}" )
        _ver_split "${va}"
 
+       _ver_validate "${comp[@]}" || die "${FUNCNAME}: invalid version: ${va}"
+       _ver_validate "${compb[@]}" || die "${FUNCNAME}: invalid version: ${vb}"
+
        local i sa sb ca cb wa wb result=0
        for (( i = 0;; i += 2 )); do
                sa=${comp[i]}

diff --git a/eclass/tests/eapi7-ver.sh b/eclass/tests/eapi7-ver.sh
index 1ad99a246e1..144bb2bddc3 100755
--- a/eclass/tests/eapi7-ver.sh
+++ b/eclass/tests/eapi7-ver.sh
@@ -161,12 +161,12 @@ txf ver_test 1 lt 2
 txf ver_test 1 -foo 2
 
 # Malformed versions
-#txf ver_test "" -ne 1
-#txf ver_test 1. -ne 1
-#txf ver_test 1ab -ne 1
-#txf ver_test b -ne 1
-#txf ver_test 1-r1_pre -ne 1
-#txf ver_test 1-pre1 -ne 1
-#txf ver_test 1_foo -ne 1
-#txf ver_test 1_pre1.1 -ne 1
-#txf ver_test 1-r1.0 -ne 1
+txf ver_test "" -ne 1
+txf ver_test 1. -ne 1
+txf ver_test 1ab -ne 1
+txf ver_test b -ne 1
+txf ver_test 1-r1_pre -ne 1
+txf ver_test 1-pre1 -ne 1
+txf ver_test 1_foo -ne 1
+txf ver_test 1_pre1.1 -ne 1
+txf ver_test 1-r1.0 -ne 1

Reply via email to