From luke-tierney at uiowa.edu  Tue Mar 31 00:41:39 2015
From: luke-tierney at uiowa.edu (luke-tierney at uiowa.edu)
Date: Mon, 30 Mar 2015 17:41:39 -0500
Subject: [Rd] Segfault with match()
In-Reply-To: <CABdHhvEXZ-4ZO8DUsb=bNaJ4vSgWP8qYWwo3Ow46x3wpsN-dWg@mail.gmail.com>
References: <CABdHhvGoW+p0Rw3Oeg7bWdi-XsXAv-XAbS-Q9rv90StXAtnXKw@mail.gmail.com>
	<CAF8bMcaO+_La_wU1s7uFpMo2atfxDrcLogWvsHz7HaO8JfESGw@mail.gmail.com>
	<CABdHhvEXZ-4ZO8DUsb=bNaJ4vSgWP8qYWwo3Ow46x3wpsN-dWg@mail.gmail.com>
Message-ID: <alpine.DEB.2.10.1503301738110.2707@luke-Latitude>

The segfault was occurring in Rf_asCharacterFactor, which was using
the levels attribute without sanity checks. Sanity checks are now
added (r68119 in trunk and r68120. in R-3-2-branch), which now gives

> data(housing, package ="MASS")
> x <- housing$Type + housing$Sat
Warning message:
Incompatible methods ("Ops.factor", "Ops.ordered") for "+" 
> match(x, unique(x))
Error in match(x, unique(x)) : malformed factor

I'll leave it to others to figure out how not to get the malformed
factor in the first place (but as a user could intentionally create
one we need the sanity checks anyway).

Best,

luke

On Mon, 30 Mar 2015, Hadley Wickham wrote:

> I left out the warning - it's still there. The output object is
> malformed but either +.factor should prevent this or match() should
> check.
>
> Hadley
>
> On Mon, Mar 30, 2015 at 3:50 PM, William Dunlap <wdunlap at tibco.com> wrote:
>> Did you leave out the warning from "+", which should be an error,
>> as it produces an illegal ordered factor in this case and factor+factor
>> is nonsensical?  Or is the warning missing in the current development
>> version of R?
>>
>>> x <- factor("A", ordered=FALSE) + factor(c("B","C"), ordered=TRUE)
>> Warning message:
>> Incompatible methods ("Ops.factor", "Ops.ordered") for "+"
>>> str(x) # 2 levels, so integer codes of 3 is illegal
>> ordered[1:2] w/ 2 levels B<C:  2 3
>>
>>
>> Bill Dunlap
>> TIBCO Software
>> wdunlap tibco.com
>>
>> On Mon, Mar 30, 2015 at 1:10 PM, Hadley Wickham <h.wickham at gmail.com> wrote:
>>>
>>> This is admittedly a contrived example, but...
>>>
>>> data(housing, package ="MASS")
>>> x <- housing$Type + housing$Sat
>>> match(x, unique(x))
>>>
>>> Hadley
>>>
>>> --
>>> http://had.co.nz/
>>>
>>> ______________________________________________
>>> R-devel at r-project.org mailing list
>>> https://stat.ethz.ch/mailman/listinfo/r-devel
>>
>>
>
>
>
>

-- 
Luke Tierney
Ralph E. Wareham Professor of Mathematical Sciences
University of Iowa                  Phone:             319-335-3386
Department of Statistics and        Fax:               319-335-3017
    Actuarial Science
241 Schaeffer Hall                  email:   luke-tierney at uiowa.edu
Iowa City, IA 52242                 WWW:  http://www.stat.uiowa.edu



[Rd] Segfault with match()
luke-tierney at uiowa.edu