From seth at userprimary.net  Sun Jun 28 19:02:08 2009
From: seth at userprimary.net (Seth Falcon)
Date: Sun, 28 Jun 2009 10:02:08 -0700
Subject: [Rd] bug in Rf_PrintValue ?
In-Reply-To: <4A455541.2000102@fhcrc.org>
References: <c2350ba40906261201o2b2224a8w3e641686702fe40a@mail.gmail.com>
	<4A455541.2000102@fhcrc.org>
Message-ID: <20090628170208.GH383@ziti.local>

Hi,


* Kynn Jones wrote:
> > I'm very green with R, so maybe this is not a bug, but it looks like one to
> > me.  The following program segfaults at the second call to
> > Rf_PrintValue().

Yes, I think you found a bug.

* On 2009-06-26 at 16:09 -0700 Martin Morgan wrote:
> mkChar creates a CHARSXP. These are not normally user-visible, but
> instead are placed into a STRSXP (vector type of 'character' in R). So
> you want to
> 
>     PROTECT( x_r = allocVector(STRSXP, 1) );
>     SET_STRING_ELT(x_r, 0, mkChar( x ));
> 
> (There is also mkString( x ) for the special case of constructing
> character(1)).
> 
> I think the segfault is because the CHARSXP returned by mkChar is
> initialized with information different from that expected of
> user-visible SEXPs (I think it is the information on chaining the node
> to the hash table; see Defn.h:120 and memory.c:2844); I think the
> success of Rf_PrintValue on 'foo' is a ghost left over from when
> CHARSXPs were user-visible.

CHARSXPs are not intended to be user-visible.  However, Rf_PrintValue
should not segfault either.  Indeed the root cause was attempting to
print the attributes for the CHARSXP which have been repurposed for
handling the CHARSXP cache.

I have patched R-devel so that PrintValue works as expected on
CHARSXPs.  The original code should now work without crashing.  But
this should really only be used to assist in debugging.  CHARSXPs
should never be exposed at the user level and should instead be
elements of a character vector (STRSXP).

+ seth

--
Seth Falcon
http://userprimary.net/user



[Rd] bug in Rf_PrintValue ?
Seth Falcon