瀏覽代碼

Dpkg::Version: update version_check to forbid versions starting with non digits

Adapted the code to rely on the parsing done by the constructor to split
the version number properly instead of redoing similar regexes in
version_check().

Updated the test suite accordingly.
Raphaël Hertzog 15 年之前
父節點
當前提交
0cacb0c3a1
共有 2 個文件被更改,包括 21 次插入10 次删除
  1. 16 9
      scripts/Dpkg/Version.pm
  2. 5 1
      scripts/t/100_Dpkg_Version.t

+ 16 - 9
scripts/Dpkg/Version.pm

@@ -88,7 +88,7 @@ sub new {
     }
 
     my $self = {};
-    if ($ver =~ /^(\d*):(.+)$/) {
+    if ($ver =~ /^([^:]*):(.+)$/) {
 	$self->{'epoch'} = $1;
 	$ver = $2;
     } else {
@@ -159,7 +159,7 @@ sub comparison {
         $b = Dpkg::Version->new($b);
     }
     ($a, $b) = ($b, $a) if $inverted;
-    my $r = $a->epoch() <=> $b->epoch();
+    my $r = version_compare_part($a->epoch(), $b->epoch());
     return $r if $r;
     $r = version_compare_part($a->version(), $b->version());
     return $r if $r;
@@ -362,22 +362,29 @@ contains a description of the problem with the $version scalar.
 
 sub version_check($) {
     my $version = shift;
-    $version = "$version" if ref($version);
-
-    if (not defined($version) or not length($version)) {
+    my $str;
+    if (defined $version) {
+        $str = "$version";
+        $version = Dpkg::Version->new($str) unless ref($version);
+    }
+    if (not defined($str) or not length($str)) {
         my $msg = _g("version number cannot be empty");
         return (0, $msg) if wantarray;
         return 0;
     }
-    if ($version =~ m/([^-+:.0-9a-zA-Z~])/o) {
+    if ($version->version() =~ m/^[^\d]/) {
+        my $msg = _g("version number does not start with digit");
+        return (0, $msg) if wantarray;
+        return 0;
+    }
+    if ($str =~ m/([^-+:.0-9a-zA-Z~])/o) {
         my $msg = sprintf(_g("version number contains illegal character `%s'"), $1);
         return (0, $msg) if wantarray;
         return 0;
     }
-    if ($version =~ /:/ and $version !~ /^\d*:/) {
-        $version =~ /^([^:]*):/;
+    if ($version->epoch() !~ /^\d*$/) {
         my $msg = sprintf(_g("epoch part of the version number " .
-                             "is not a number: '%s'"), $1);
+                             "is not a number: '%s'"), $version->epoch());
         return (0, $msg) if wantarray;
         return 0;
     }

+ 5 - 1
scripts/t/100_Dpkg_Version.t

@@ -28,7 +28,7 @@ my @ops = ("<", "<<", "lt",
 	   ">=", "ge",
 	   ">", ">>", "gt");
 
-plan tests => scalar(@tests) * (3 * scalar(@ops) + 4) + 11;
+plan tests => scalar(@tests) * (3 * scalar(@ops) + 4) + 13;
 
 sub dpkg_vercmp {
      my ($a, $cmp, $b) = @_;
@@ -87,6 +87,10 @@ ok($ver eq '10a:5.2', "invalid still same string 1/2");
 $ver = Dpkg::Version->new('5.2@3-2');
 ok($ver eq '5.2@3-2', "invalid still same string 2/2");
 ok(!$ver->is_valid(), "illegal character is invalid");
+$ver = Dpkg::Version->new('foo5.2');
+ok(!$ver->is_valid(), "version does not start with digit 1/2");
+$ver = Dpkg::Version->new('0:foo5.2');
+ok(!$ver->is_valid(), "version does not start with digit 2/2");
 
 # Other tests
 $ver = Dpkg::Version->new('1.2.3-4');