bug 2315: no crash if subfield code is a metacharacter
authorGalen Charlton <galen.charlton@liblime.com>
Mon, 7 Jul 2008 22:17:05 +0000 (17:17 -0500)
committerJoshua Ferraro <jmf@liblime.com>
Tue, 8 Jul 2008 14:36:11 +0000 (09:36 -0500)
When generating the display form of a heading that
happens to (invalidly) have a regular expression
metacharacter as a subfield label, do not crash.

An example of such a heading field is:

  <datafield tag="650" ind1=" " ind2="0">
    <subfield code="a">Dalziel, Andrew (Fictitious character</subfield>
    <subfield code=")">xFiction.</subfield>
  </datafield>

The error message associated with the crash is:

  Unmatched ) in regex; marked by <-- HERE in m/) <-- HERE / at
  /home/koha-pro/kohaclone/C4/Heading/MARC21.pm line 220.

Signed-off-by: Joshua Ferraro <jmf@liblime.com>
C4/Heading/MARC21.pm
t/lib/KohaTest/Heading.pm [new file with mode: 0644]
t/lib/KohaTest/Heading/MARC21.pm [new file with mode: 0644]

index 4b32828..f6e712a 100644 (file)
@@ -216,8 +216,9 @@ sub _get_display_heading {
     my $first = 1;
     for (my $i = 0; $i <= $#subfields; $i++) {
         my $code = $subfields[$i]->[0];
+        my $code_re = quotemeta $code;
         my $value = $subfields[$i]->[1];
-        next unless $subfields =~ qr/$code/;
+        next unless $subfields =~ qr/$code_re/;
         if ($first) {
             $first = 0;
             $heading = $value;
diff --git a/t/lib/KohaTest/Heading.pm b/t/lib/KohaTest/Heading.pm
new file mode 100644 (file)
index 0000000..4f781a2
--- /dev/null
@@ -0,0 +1,27 @@
+package KohaTest::Heading;
+use base qw( KohaTest );
+
+use strict;
+use warnings;
+
+use Test::More;
+
+use C4::Heading;
+sub testing_class { 'C4::Heading' };
+
+
+sub methods : Test( 1 ) {
+    my $self = shift;
+    my @methods = qw( 
+                    new_from_bib_field
+                    display_form
+                    authorities
+                    preferred_authorities
+                    _query_limiters
+                    _marc_format_handler
+                );
+    
+    can_ok( $self->testing_class, @methods );    
+}
+
+1;
diff --git a/t/lib/KohaTest/Heading/MARC21.pm b/t/lib/KohaTest/Heading/MARC21.pm
new file mode 100644 (file)
index 0000000..41cd4d3
--- /dev/null
@@ -0,0 +1,41 @@
+package KohaTest::Heading::MARC21;
+use base qw( KohaTest::Heading );
+
+use strict;
+use warnings;
+
+use Test::More;
+
+use C4::Heading;
+use C4::Heading::MARC21;
+
+use MARC::Field;
+
+sub testing_class { 'C4::Heading::MARC21' };
+
+sub methods : Test( 1 ) {
+    my $self = shift;
+    my @methods = qw( 
+                    new
+                    valid_bib_heading_tag
+                    parse_heading
+                    _get_subject_thesaurus
+                    _get_search_heading
+                    _get_display_heading
+                );
+    
+    can_ok( $self->testing_class, @methods );    
+}
+
+sub bug2315 : Test( 1 ) {
+
+    my $subject_heading = MARC::Field->new(650, ' ', '0', 
+                                                a   => "Dalziel, Andrew (Fictitious character",
+                                                ')' => "Fiction."
+                                           );
+    my $display_form = C4::Heading::MARC21::_get_display_heading($subject_heading, 'a');
+    is($display_form, "Dalziel, Andrew (Fictitious character", "bug 2315: no crash if heading subfield has metacharacter");
+
+}
+
+1;