From: rkinyon Date: Thu, 18 Jan 2007 15:16:00 +0000 (+0000) Subject: r14848@rob-kinyons-computer: rob | 2007-01-17 22:15:40 -0500 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=a0c2cf7be7fa43b0a8e73acc7acb48ce79b1fb51;p=dbsrgits%2FDBM-Deep.git r14848@rob-kinyons-computer: rob | 2007-01-17 22:15:40 -0500 Added tests for undefined array indices --- diff --git a/lib/DBM/Deep/Array.pm b/lib/DBM/Deep/Array.pm index fe96af0..d9ea66e 100644 --- a/lib/DBM/Deep/Array.pm +++ b/lib/DBM/Deep/Array.pm @@ -46,7 +46,10 @@ sub FETCH { $self->lock( $self->LOCK_SH ); - if ( $key =~ /^-?\d+$/ ) { + if ( !defined $key ) { + DBM::Deep->_throw_error( "Cannot use an undefined array index." ); + } + elsif ( $key =~ /^-?\d+$/ ) { if ( $key < 0 ) { $key += $self->FETCHSIZE; unless ( $key >= 0 ) { @@ -75,7 +78,10 @@ sub STORE { my $size; my $idx_is_numeric; - if ( $key =~ /^\-?\d+$/ ) { + if ( !defined $key ) { + DBM::Deep->_throw_error( "Cannot use an undefined array index." ); + } + elsif ( $key =~ /^-?\d+$/ ) { $idx_is_numeric = 1; if ( $key < 0 ) { $size = $self->FETCHSIZE; @@ -110,7 +116,10 @@ sub EXISTS { $self->lock( $self->LOCK_SH ); - if ( $key =~ /^\-?\d+$/ ) { + if ( !defined $key ) { + DBM::Deep->_throw_error( "Cannot use an undefined array index." ); + } + elsif ( $key =~ /^-?\d+$/ ) { if ( $key < 0 ) { $key += $self->FETCHSIZE; unless ( $key >= 0 ) { @@ -138,7 +147,10 @@ sub DELETE { $self->lock( $self->LOCK_EX ); my $size = $self->FETCHSIZE; - if ( $key =~ /^-?\d+$/ ) { + if ( !defined $key ) { + DBM::Deep->_throw_error( "Cannot use an undefined array index." ); + } + elsif ( $key =~ /^-?\d+$/ ) { if ( $key < 0 ) { $key += $size; unless ( $key >= 0 ) { diff --git a/t/04_array.t b/t/04_array.t index a77fd70..341e53e 100644 --- a/t/04_array.t +++ b/t/04_array.t @@ -2,7 +2,7 @@ # DBM::Deep Test ## use strict; -use Test::More tests => 112; +use Test::More tests => 116; use Test::Exception; use t::common qw( new_fh ); @@ -212,13 +212,30 @@ throws_ok { } qr/Cannot use 'foo' as an array index/, "FETCH fails on an illegal key"; throws_ok { + $db->fetch(); +} qr/Cannot use an undefined array index/, "FETCH fails on an undefined key"; + +throws_ok { $db->store( 'foo', 'bar' ); } qr/Cannot use 'foo' as an array index/, "STORE fails on an illegal key"; throws_ok { + $db->store(); +} qr/Cannot use an undefined array index/, "FETCH fails on an undefined key"; + +throws_ok { $db->delete( 'foo' ); } qr/Cannot use 'foo' as an array index/, "STORE fails on an illegal key"; throws_ok { + $db->delete(); +} qr/Cannot use an undefined array index/, "FETCH fails on an undefined key"; + +throws_ok { $db->exists( 'foo' ); } qr/Cannot use 'foo' as an array index/, "STORE fails on an illegal key"; + +throws_ok { + $db->exists(); +} qr/Cannot use an undefined array index/, "FETCH fails on an undefined key"; +