diff options
author | Paul Marquess <paul.marquess@btinternet.com> | 2001-10-21 22:11:15 +0100 |
---|---|---|
committer | Jarkko Hietaniemi <jhi@iki.fi> | 2001-10-22 12:17:00 +0000 |
commit | 0bf2e7072c2c1360a32d348a7c800f40c1108f8a (patch) | |
tree | bcc20ad90d2634a789df217845b7a8923775ae2c /ext/DB_File | |
parent | b309b8ae430d85542056be4d1a80055ed0cb7b0e (diff) | |
download | perl-0bf2e7072c2c1360a32d348a7c800f40c1108f8a.tar.gz |
Fix for FETCH/NEXTKEY problem in all *DB*_File modules
Message-ID: <AIEAJICLCBDNAAOLLOKLAEOMDCAA.paul.marquess@openwave.com>
p4raw-id: //depot/perl@12564
Diffstat (limited to 'ext/DB_File')
-rw-r--r-- | ext/DB_File/DB_File.xs | 3 | ||||
-rwxr-xr-x | ext/DB_File/t/db-btree.t | 44 | ||||
-rwxr-xr-x | ext/DB_File/t/db-hash.t | 44 |
3 files changed, 88 insertions, 3 deletions
diff --git a/ext/DB_File/DB_File.xs b/ext/DB_File/DB_File.xs index db4382be8f..05e5319b92 100644 --- a/ext/DB_File/DB_File.xs +++ b/ext/DB_File/DB_File.xs @@ -1777,13 +1777,14 @@ db_FIRSTKEY(db) void db_NEXTKEY(db, key) DB_File db - DBTKEY key + DBTKEY key = NO_INIT PREINIT: int RETVAL; CODE: { DBT value ; + DBT_clear(key) ; DBT_clear(value) ; CurrentDB = db ; RETVAL = do_SEQ(db, key, value, R_NEXT) ; diff --git a/ext/DB_File/t/db-btree.t b/ext/DB_File/t/db-btree.t index 905cbe1fdf..a380496b53 100755 --- a/ext/DB_File/t/db-btree.t +++ b/ext/DB_File/t/db-btree.t @@ -15,7 +15,7 @@ use strict; use DB_File; use Fcntl; -print "1..157\n"; +print "1..163\n"; sub ok { @@ -1295,4 +1295,46 @@ EOM unlink $Dfile; } +{ + # When iterating over a tied hash using "each", the key passed to FETCH + # will be recycled and passed to NEXTKEY. If a Source Filter modifies the + # key in FETCH via a filter_fetch_key method we need to check that the + # modified key doesn't get passed to NEXTKEY. + # Also Test "keys" & "values" while we are at it. + + use warnings ; + use strict ; + use DB_File ; + + unlink $Dfile; + my $bad_key = 0 ; + my %h = () ; + my $db ; + ok(158, $db = tie(%h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0640, $DB_BTREE ) ); + $db->filter_fetch_key (sub { $_ =~ s/^Beta_/Alpha_/ if defined $_}) ; + $db->filter_store_key (sub { $bad_key = 1 if /^Beta_/ ; $_ =~ s/^Alpha_/Beta_/}) ; + + $h{'Alpha_ABC'} = 2 ; + $h{'Alpha_DEF'} = 5 ; + + ok(159, $h{'Alpha_ABC'} == 2); + ok(160, $h{'Alpha_DEF'} == 5); + + my ($k, $v) = ("",""); + while (($k, $v) = each %h) {} + ok(161, $bad_key == 0); + + $bad_key = 0 ; + foreach $k (keys %h) {} + ok(162, $bad_key == 0); + + $bad_key = 0 ; + foreach $v (values %h) {} + ok(163, $bad_key == 0); + + undef $db ; + untie %h ; + unlink $Dfile; +} + exit ; diff --git a/ext/DB_File/t/db-hash.t b/ext/DB_File/t/db-hash.t index 12b0848fa2..1d13dc0941 100755 --- a/ext/DB_File/t/db-hash.t +++ b/ext/DB_File/t/db-hash.t @@ -15,7 +15,7 @@ use warnings; use DB_File; use Fcntl; -print "1..111\n"; +print "1..117\n"; sub ok { @@ -742,4 +742,46 @@ EOM unlink $Dfile; } +{ + # When iterating over a tied hash using "each", the key passed to FETCH + # will be recycled and passed to NEXTKEY. If a Source Filter modifies the + # key in FETCH via a filter_fetch_key method we need to check that the + # modified key doesn't get passed to NEXTKEY. + # Also Test "keys" & "values" while we are at it. + + use warnings ; + use strict ; + use DB_File ; + + unlink $Dfile; + my $bad_key = 0 ; + my %h = () ; + my $db ; + ok(112, $db = tie(%h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0640, $DB_HASH ) ); + $db->filter_fetch_key (sub { $_ =~ s/^Beta_/Alpha_/ if defined $_}) ; + $db->filter_store_key (sub { $bad_key = 1 if /^Beta_/ ; $_ =~ s/^Alpha_/Beta_/}) ; + + $h{'Alpha_ABC'} = 2 ; + $h{'Alpha_DEF'} = 5 ; + + ok(113, $h{'Alpha_ABC'} == 2); + ok(114, $h{'Alpha_DEF'} == 5); + + my ($k, $v) = ("",""); + while (($k, $v) = each %h) {} + ok(115, $bad_key == 0); + + $bad_key = 0 ; + foreach $k (keys %h) {} + ok(116, $bad_key == 0); + + $bad_key = 0 ; + foreach $v (values %h) {} + ok(117, $bad_key == 0); + + undef $db ; + untie %h ; + unlink $Dfile; +} + exit ; |