The following commit has been merged in the master branch: commit 0cacb0c3a1d1f837c957f3917a040ace6d60f9e7 Author: Raphaël Hertzog <hert...@debian.org> Date: Sun Apr 3 09:52:33 2011 +0200
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. diff --git a/scripts/Dpkg/Version.pm b/scripts/Dpkg/Version.pm index f0f7cd2..9f0f3a1 100644 --- a/scripts/Dpkg/Version.pm +++ b/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; } diff --git a/scripts/t/100_Dpkg_Version.t b/scripts/t/100_Dpkg_Version.t index 0f4926d..97bc7d2 100644 --- a/scripts/t/100_Dpkg_Version.t +++ b/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'); -- dpkg's main repository -- To UNSUBSCRIBE, email to debian-dpkg-cvs-requ...@lists.debian.org with a subject of "unsubscribe". Trouble? Contact listmas...@lists.debian.org