From p.dalgaard at biostat.ku.dk  Sun Feb 21 09:36:22 2010
From: p.dalgaard at biostat.ku.dk (Peter Dalgaard)
Date: Sun, 21 Feb 2010 09:36:22 +0100
Subject: [Rd] Rubbish values written with zero-length vectors (PR#14217)
In-Reply-To: <20100220222523.C9068282EF58@mail.pubhealth.ku.dk>
References: <20100220222523.C9068282EF58@mail.pubhealth.ku.dk>
Message-ID: <4B80F086.9090103@biostat.ku.dk>

seth at userprimary.net wrote:

Thanks, Seth. Martin Morgan sent a patch for a few lines above yours, 
which I didn't have a chance to review until now:

-   if (!isVectorList(x) && LENGTH(y) > 1)
-       error(_("more elements supplied than there are to replace"));
+   if (!isVectorList(x) && LENGTH(y) != 1)
+       if (LENGTH(y) == 0)
+           error(_("fewer elements supplied than there are to replace"));
+       else
+           error(_("more elements supplied than there are to replace"));

I _think_ that you are both right that there is no way for a zero-length 
RHS not to be an error. E.g.,

 > x[[0]] <- real(0)
Error in x[[0]] <- real(0) : attempt to select less than one element

The difference between Seth's solution and Martin's is whether to 
pre-check for nsubs==1, and I don't think we want that because of

 > x <- matrix(1:4,2,2)
 > x[[2,2]]
[1] 4
 > x[[2,2]] <- integer(0)
 > x
      [,1]      [,2]
[1,]    1         3
[2,]    2 142000760


> On 2/20/10 7:50 AM, Peter Dalgaard wrote:
>> You don't want to understand, believe me! ;-)
>>
>> It's a bug, probably not the very worst kind, but accessing memory that
>> isn't yours is potentially harmful (but writing to it is considerably
>> worse).
>>
>> Looks like the issue only concerns the right hand side; nothing to do
>> with the auto-expansion of v. I also get
>>
>>> v <- integer(0)
>>> u <- integer(1)
>>> u[[2]] <-v
>>> u
>> [1]         0 142000760
>>> u[[1]] <-v
>>> u
>> [1] 142000760 142000760
>>> a <- 1
>>> a[[1]] <-v
>>> a
>> [1] 142000760
> 
> I'm thinking this should be an error.  Similar to:
> 
>> v = 1
>> v[[1]] = integer(3)
> Error in v[[1]] = integer(3) :
>   more elements supplied than there are to replace
> 
> But instead not enough elements supplied.  Perhaps:
> 
>> v[[1]] = integer()
> Error in v[[1]] = integer() : [[ ]] replacement has zero length
> 
> The code in do_subassign2_dflt currently does not check that the
> replacement has length > 0 for the nsubs == 1 case.  I think we want:
> 
> 
> @@ -1529,6 +1532,8 @@ do_subassign2_dflt(SEXP call, SEXP op, SEXP args,
> SEXP rho)
>         if (nsubs == 0 || CAR(subs) == R_MissingArg)
>             error(_("[[ ]] with missing subscript"));
>         if (nsubs == 1) {
> +            if (length(y) == 0)
> +                error(_("[[ ]] replacement has zero length"));
>             offset = OneIndex(x, thesub, length(x), 0, &newname,
> recursed ? len-1 : -1, R_NilValue);
>             if (isVectorList(x) && isNull(y)) {
>                 x = DeleteOneVectorListItem(x, offset);
> 
> 
> + seth
> 
> ______________________________________________
> R-devel at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel



-- 
    O__  ---- Peter Dalgaard             ?ster Farimagsgade 5, Entr.B
   c/ /'_ --- Dept. of Biostatistics     PO Box 2099, 1014 Cph. K
  (*) \(*) -- University of Copenhagen   Denmark      Ph:  (+45) 35327918
~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk)              FAX: (+45) 35327907



[Rd] Rubbish values written with zero-length vectors (PR#14217)
Peter Dalgaard