From wdunlap at tibco.com  Wed Dec  3 19:38:05 2008
From: wdunlap at tibco.com (William Dunlap)
Date: Wed, 3 Dec 2008 10:38:05 -0800
Subject: [Rd] Matrix dimnames crash (PR#13361)
In-Reply-To: <alpine.LFD.2.00.0812031702110.13705@gannet.stats.ox.ac.uk>
References: <20081203125009.42B49282EFF6@mail.pubhealth.ku.dk>
	<alpine.LFD.2.00.0812031702110.13705@gannet.stats.ox.ac.uk>
Message-ID: <77EB52C6DD32BA4D87471DCD70C8D700491A51@NA-PA-VBE03.na.tibco.com>

Brian,
   Your change to src/main/attrib.c:dimnamesgets(),
--- attrib.c    (revision 47045)
+++ attrib.c    (working copy)
@@ -857,6 +857,9 @@
        UNPROTECT(1);
        PROTECT(val = newval);
     }
+    if (k != length(val))
+       error(_("length of 'dimnames' [%d] must match that of 'dims'
[%d]"),
+             length(val), k);
     for (i = 0; i < k; i++) {
        SEXP _this = VECTOR_ELT(val, i);
        if (_this != R_NilValue) {
results in array() and matrix() treating dimnames=list() differently
and I think they ought to act the same when feasible.
Also, the error message is not really the right one, as the length
of the dimnames handed to the array() or matrix() function does not
have to be the length of dim - a short one gets padded with NULL's.
(I don't know if this padding is the best thing to do -- R does it
and S+ does not -- and I think it generally reflects user error.)
E.g., in R:

> f<-function(dimnames){
    mat<-try(matrix(1:6,nrow=2,dimnames=dimnames), silent=TRUE)
    arr<-try(array(1:6,dim=c(2,3),dimnames=dimnames), silent=TRUE)
    list(matrix=mat, array=arr, identical=identical(mat, arr))
}

> f(dimnames=NULL)
$matrix
     [,1] [,2] [,3]
[1,]    1    3    5
[2,]    2    4    6

$array
     [,1] [,2] [,3]
[1,]    1    3    5
[2,]    2    4    6

$identical
[1] TRUE

> f(dimnames=list())
$matrix
[1] "Error in matrix(1:6, nrow = 2, dimnames = dimnames) : \n  length of
'dimnames' [0] must match that of 'dims' [2]\n"
attr(,"class")
[1] "try-error"

$array
     [,1] [,2] [,3]
[1,]    1    3    5
[2,]    2    4    6

$identical
[1] FALSE

> f(dimnames=list(Row=LETTERS[24:25])) # short dimnames
$matrix

Row [,1] [,2] [,3]
  X    1    3    5
  Y    2    4    6

$array

Row [,1] [,2] [,3]
  X    1    3    5
  Y    2    4    6

$identical
[1] TRUE


The following change to src/main/array.c makes array() and matrix()
treat dimnames=list() in the same way (equivalent to dimnames=NULL.

--- array.c     (revision 47047)
+++ array.c     (working copy)
@@ -152,7 +152,7 @@
            ;
        }
     }
-    if(!isNull(dimnames)) ans = dimnamesgets(ans, dimnames);
+    if(!isNull(dimnames) && length(dimnames)>0) ans = dimnamesgets(ans,
dimnames);
     UNPROTECT(1);
     return ans;
 }

The change to attrib.c:dimnamesgets() is fine: this change to
array.c:do_matrix just avoids the call.

Bill Dunlap
TIBCO Software Inc - Spotfire Division
wdunlap tibco.com  

> -----Original Message-----
> From: r-devel-bounces at r-project.org 
> [mailto:r-devel-bounces at r-project.org] On Behalf Of Prof Brian Ripley
> Sent: Wednesday, December 03, 2008 9:14 AM
> To: arnima at u.washington.edu
> Cc: R-bugs at r-project.org; r-devel at stat.math.ethz.ch
> Subject: Re: [Rd] Matrix dimnames crash (PR#13361)
> 
> On Wed, 3 Dec 2008, arnima at u.washington.edu wrote:
> 
> > In Windows XP, the matrix() function crashes the program 
> when 'dimnames'
> > is an empty list:
> >
> >   matrix(1:4, nrow=2, dimnames=list())
> >   # R has encountered a problem and needs to close ...
> >
> > This bug is specific to WinXP, as Linux64 handles this 
> situation more
> > gracefully:
> >
> >   matrix(1:4, nrow=2, dimnames=list())
> >   Error in matrix(1:4, nrow = 2, dimnames = list()) :
> >     invalid type (environment) for 'dimnames' (must be a vector)
> 
> Actually no (the reported type is wrong, and my x86_64 Linux 
> system also 
> crashes).
> 
> This case slips though the error-checking, and I've added a 
> final error 
> check that will catch it in R-patched.
> 
> Thank you for the report.
> 
> >
> > Thanks,
> > Arni
> >
> > R 2.8.0-patched on WinXP
> >
> > ______________________________________________
> > R-devel at r-project.org mailing list
> > https://stat.ethz.ch/mailman/listinfo/r-devel
> >
> 
> -- 
> Brian D. Ripley,                  ripley at stats.ox.ac.uk
> Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
> University of Oxford,             Tel:  +44 1865 272861 (self)
> 1 South Parks Road,                     +44 1865 272866 (PA)
> Oxford OX1 3TG, UK                Fax:  +44 1865 272595
> 
> ______________________________________________
> R-devel at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel
> 



[Rd] Matrix dimnames crash (PR#13361)
William Dunlap