[Rd] Strange assignment bug (PR#1450)

Luke Tierney luke@stat.umn.edu
Wed, 10 Apr 2002 11:50:14 -0500


It's a problem in subset.c.  Should be fixed now.

A further simplification with annotation:

x <- list(p=1)

f <- function(A) {
  A$p <- 2       # makes copy of A with NAMED 1
  E <- A$p       # no copy, A$p gets NAMED 2
  A$p            # is setting NAMED back to 1
  E[1] <- 0.1    # modifies shared value in place
  return(A)
}

f(x)

The problem was in code that looks like this:

	y = VECTOR_ELT(x, i);
        SET_NAMED(y, NAMED(x));

which ignores the possibility that NAMED(y) may already be larger than
NAMED(x).  I've changed these bits to

	y = VECTOR_ELT(x, i);
	if (NAMED(x) > NAMED(y))
	    SET_NAMED(y, NAMED(x));

This occurred in a few other places but mostly in pairlist code so was
less likely to bite, but I changed those as well.

The problem did not occur with [[ because that does more copying.

luke

On Wed, Apr 10, 2002 at 09:29:07AM -0700, Thomas Lumley wrote:
> On 10 Apr 2002, Peter Dalgaard BSA wrote:
> 
> > G.Innocent@vet.gla.ac.uk writes:
> >
> > > 6) The above code appears to be the minimum of code required to reproduce
> > > the results on my machine.
> >
> > You can do better than that:
> >
> > > f
> > function(A) {
> >   A$p[1] <- 2
> >   E <- A$p
> >   E[1:2] <- 0.1*A$p[2:3]
> >   return(A)
> > }
> <snip>
> > Notice that the last line changing E is effectively changing A$p, so
> > something is up with our duplication-avoidance scheme. Luke?
> 
> and it matters that the assignment to E is coming from A$p. If it comes
> from A$q or from some other vector then the bug doesn't bite.
> 
> 	-thomas
> 
> 
> -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
> r-devel mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
> Send "info", "help", or "[un]subscribe"
> (in the "body", not the subject !)  To: r-devel-request@stat.math.ethz.ch
> _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._

-- 
Luke Tierney
University of Minnesota                      Phone:           612-625-7843
School of Statistics                         Fax:             612-624-8868
313 Ford Hall, 224 Church St. S.E.           email:      luke@stat.umn.edu
Minneapolis, MN 55455 USA                    WWW:  http://www.stat.umn.edu
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-devel mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-devel-request@stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._