Bug when using eval 
Author Message
 Bug when using eval

Running Solaris/x86 2.5.1; perl -v reports:

This is perl, version 5.004_01

Copyright 1987-1997, Larry Wall

Perl may be copied only under the terms of either the Artistic License or the
GNU General Public License, which may be found in the Perl 5.0 source kit.

Eval will fail in the following situation:

- eval'ing a string
- the string represents an anonymous hash
- the first key of the anon hash is single quoted, and contains an
  embedded single quote escaped with a backslash
- using the form `` $ref = eval $string ''

The MLDBM module uses this form of eval all the time, so the above
situation actually has the potential to occur quite often.

This script demonstrates the bug:
------------------------------------------------------------------------
#! /opt/perl5/bin/perl

$string1 = "{'a' => 'foo', 'b' => 'bar', 'c' => 'bat'}";
$string2 = "{'a\\'' => 'foo', 'b' => 'bar', 'c' => 'bat'}";
$string3 = "{'a' => 'foo', 'b\\'' => 'bar', 'c' => 'bat'}";

evalit($string1);
evalit($string2);
evalit($string3);

sub evalit {
  my $string = shift;
  print "String is ", $string, "\n";

  $ref = eval $string;
  print 'First eval form: $ref is a ', ref($ref), "\n";
  print "ref's value is $ref\n" unless ref($ref) eq "HASH";
  print map {" $_: $ref->{$_}\n"} keys %{$ref};

  eval "\$ref = $string";
  print "ref's value is $ref\n" unless ref($ref) eq "HASH";
  print 'Second eval form: $ref is a ', ref($ref), "\n";
  print map {" $_: $ref->{$_}\n"} keys %{$ref};

  print "\n";

Quote:
}

------------------------------------------------------------------------
$ ./showbug
String is {'a' => 'foo', 'b' => 'bar', 'c' => 'bat'}
First eval form: $ref is a HASH
 a: foo
 b: bar
 c: bat
Second eval form: $ref is a HASH
 a: foo
 b: bar
 c: bat

String is {'a\'' => 'foo', 'b' => 'bar', 'c' => 'bat'}
First eval form: $ref is a
ref's value is bat
Second eval form: $ref is a HASH
 a': foo
 b: bar
 c: bat

String is {'a' => 'foo', 'b\'' => 'bar', 'c' => 'bat'}
First eval form: $ref is a HASH
 b': bar
 a: foo
 c: bat
Second eval form: $ref is a HASH
 b': bar
 a: foo
 c: bat

------------------------------------------------------------------------

--

Edmonton, Alberta, Canada                | traditional!



Thu, 06 Jan 2000 03:00:00 GMT  
 
 [ 1 post ] 

 Relevant Pages 

1. Test for eval() without using eval() ??

2. Perl Tk after/eval bug

3. bug in eval for perl4 ?

4. bug in eval for v4.0 patch 36 ?

5. eval bug?

6. BUG/Limitation: Parser stack overflow in eval (4.0 pl 35)

7. Bug with foreach/eval?

8. eval bug in perl 5?

9. eval bug in perl 5.000

10. Lexical scope bug in eval()?

11. eval bug in perl 5.000

12. Possible bug with eval? under 5.001

 

 
Powered by phpBB® Forum Software