develooper Front page | perl.perl5.porters | Postings from November 2005

Re: [PATCH] sort/multicall patch - cleanup f_sort.t failures

Thread Previous | Thread Next
Robin Houston
November 3, 2005 09:37
Re: [PATCH] sort/multicall patch - cleanup f_sort.t failures
Message ID:
Jim Cromie wrote:
> I dunno why the /COMMON flag gets added, nor why it also affects
> B::Concise renderings on non-threaded builds, but it appears real.

It's nothing to do with threads.  If the COMMON flag is set on an
aassign op, it means that a temporary copy of the right-hand side is
made. It makes more sense to think of the OPpASSIGN_COMMON flag as
disabling a dangerous optimisation: in essence, if you have

  ($x, $y) = (1,2)

then you can treat it as if it were

  $x = 1; $y = 2

but if you have

  ($x, $y) = ($y, $x)

then it's no good treating it as

  $x = $y; $y = $x

so the 'no copy' behaviour is wrong in general, but it's faster
and it sometimes works.

The COMMON flag is set if either the two sides have a variable in
common (hence the name), or the statement contains a "dangerous"
(i.e. possibly side-effecting) op. This last is indicated by the
opcode flag OA_DANGEROUS, which is set by using the 'd' flag

In fact this scheme is inadequate, and there are situations where
it will fail. Variable aliasing will obviously cause problems: for
example, the following:

  *x=*a; *y=*b; ($x,$y)=(1,2);
  ($a,$b) = ($y,$x);
  print "\$a=$a, \$b=$b\n"

prints '$a=2, $b=2'.

Tagging sort() as a "dangerous" op does fix a few bugs. For example,
 $ perl -e '@in=(4,1,2,3); *out=\@in; @out = sort@in; print @out'
prints nothing, whereas it works correctly with bleadperl. The other
ops that can call arbitrary code, such as map, grep and entersub,
all have the 'd' flag, so I expect its omission from sort was a
simple oversight.

Incidentally it's still trivial to make sort dump core, even now.
Here's a good one:

 $ ./perl -e '()= sort{*a=*b;1} 1,2'
 Segmentation fault


Thread Previous | Thread Next Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About