Kaynağa Gözat

Dpkg::Arch: Add new functions to validate and parse architecture names

Guillem Jover 10 yıl önce
ebeveyn
işleme
b61fee98a7
3 değiştirilmiş dosya ile 65 ekleme ve 2 silme
  1. 1 0
      debian/changelog
  2. 41 1
      scripts/Dpkg/Arch.pm
  3. 23 1
      scripts/t/Dpkg_Arch.t

+ 1 - 0
debian/changelog

@@ -71,6 +71,7 @@ dpkg (1.18.5) UNRELEASED; urgency=medium
     - Check existence of search criteria in Dpkg::Index when checking with a
       regex or a string match. Closes: #780906
       Base on a patch by Daniel Dehennin <daniel.dehennin@baby-gnu.org>.
+    - Add new functions to validate and parse architecture names in Dpkg::Arch.
   * Build system:
     - Fix building development documentation.
     - Remove unused UA_LIBS variable.

+ 41 - 1
scripts/Dpkg/Arch.pm

@@ -32,7 +32,7 @@ use strict;
 use warnings;
 use feature qw(state);
 
-our $VERSION = '1.00';
+our $VERSION = '1.01';
 our @EXPORT_OK = qw(
     get_raw_build_arch
     get_raw_host_arch
@@ -43,11 +43,13 @@ our @EXPORT_OK = qw(
     debarch_eq
     debarch_is
     debarch_is_wildcard
+    debarch_is_illegal
     debarch_is_concerned
     debarch_to_cpuattrs
     debarch_to_gnutriplet
     debarch_to_debtriplet
     debarch_to_multiarch
+    debarch_list_parse
     debtriplet_to_debarch
     debtriplet_to_gnutriplet
     gnutriplet_to_debarch
@@ -533,6 +535,19 @@ sub debarch_is_wildcard($)
     return 0;
 }
 
+=item $bool = debarch_is_illegal($arch)
+
+Validate an architecture name.
+
+=cut
+
+sub debarch_is_illegal
+{
+    my ($arch) = @_;
+
+    return $arch !~ m/^(!?[a-zA-Z0-9][a-zA-Z0-9-]*)$/;
+}
+
 =item $bool = debarch_is_concerned($arch, @arches)
 
 Evaluate whether a Debian architecture applies to the list of architecture
@@ -568,6 +583,27 @@ sub debarch_is_concerned
     return $seen_arch;
 }
 
+=item @array = debarch_list_parse($arch_list, %options)
+
+Parse an architecture list.
+
+=cut
+
+sub debarch_list_parse
+{
+    my $arch_list = shift;
+    my @arch_list = split /\s+/, $arch_list;
+
+    foreach my $arch (@arch_list) {
+        if (debarch_is_illegal($arch)) {
+            error(g_("'%s' is not a legal architecture in list '%s'"),
+                  $arch, $arch_list);
+        }
+    }
+
+    return @arch_list;
+}
+
 1;
 
 __END__
@@ -576,6 +612,10 @@ __END__
 
 =head1 CHANGES
 
+=head2 Version 1.01 (dpkg 1.18.5)
+
+New functions: debarch_is_illegal(), debarch_list_parse().
+
 =head2 Version 1.00 (dpkg 1.18.2)
 
 Mark the module as public.

+ 23 - 1
scripts/t/Dpkg_Arch.t

@@ -16,11 +16,13 @@
 use strict;
 use warnings;
 
-use Test::More tests => 53;
+use Test::More tests => 64;
 
 use_ok('Dpkg::Arch', qw(debarch_to_debtriplet debarch_to_multiarch
                         debarch_eq debarch_is debarch_is_wildcard
+                        debarch_is_illegal
                         debarch_to_cpuattrs
+                        debarch_list_parse
                         debtriplet_to_debarch gnutriplet_to_debarch
                         get_host_gnu_type
                         get_valid_arches));
@@ -87,6 +89,26 @@ ok(debarch_is_wildcard('gnu-any-any'), '<abi>-any-any is a wildcard');
 ok(debarch_is_wildcard('any-linux-any'), 'any-<os>-any is a wildcard');
 ok(debarch_is_wildcard('any-any-amd64'), 'any-any-<cpu> is a wildcard');
 
+ok(!debarch_is_illegal('0'), '');
+ok(!debarch_is_illegal('a'), '');
+ok(!debarch_is_illegal('amd64'), '');
+ok(!debarch_is_illegal('!arm64'), '');
+ok(!debarch_is_illegal('kfreebsd-any'), '');
+ok(debarch_is_illegal('!amd64!arm'), '');
+ok(debarch_is_illegal('arch%name'), '');
+ok(debarch_is_illegal('-any'), '');
+ok(debarch_is_illegal('!'), '');
+
+my @arch_new;
+my @arch_ref;
+
+@arch_ref = qw(amd64 !arm64 linux-i386 !kfreebsd-any);
+@arch_new = debarch_list_parse('amd64  !arm64   linux-i386 !kfreebsd-any');
+is_deeply(\@arch_new, \@arch_ref, 'parse valid arch list');
+
+eval { @arch_new = debarch_list_parse('!amd64!arm64') };
+ok($@, 'parse concatenated arches failed');
+
 is(debarch_to_cpuattrs(undef), undef, 'undef cpu attrs');
 is_deeply([ debarch_to_cpuattrs('amd64') ], [ qw(64 little) ], 'amd64 cpu attrs');