develooper Front page | perl.perl5.porters | Postings from July 2016

Re: substr as parameter to a XS sub

Thread Previous | Thread Next
From:
Tony Cook
Date:
July 4, 2016 00:18
Subject:
Re: substr as parameter to a XS sub
Message ID:
20160704001725.GI29470@mars.tony.develop-help.com
On Sun, Jul 03, 2016 at 09:45:07PM +0100, Alberto Simões wrote:
> Hi
> 
> I have a module that has ties to a C library.
> 
> the XS file looks like:
> 
> -----
> void
> bt_add_macro_text (macro, text, filename=NULL, line=0)
>     char * macro
>     char * text
>     char * filename
>     int    line
> -----
> 
> the C code in the library is like
> 
> -----
> void
> bt_add_macro_text (char * macro, char * text, char * filename, int line)
> {
> -----
> 
> So everything looks good.
> 
> If I call this function as
> 
>    my $string = "january";
>    bt_add_macro_text( substr($string, 0, 3), $string);
> 
> I get a segmentation fault, because first parameter is 0x000000
> 
> but if I do
> 
>    my $string = "january";
>    my $abb = substr($string, 0, 3);
>    bt_add_macro_text( $abb, $string);
> 
> everything works well.
> 
> Is this a bug, or something I am doing incorrectly?
> I imagine this happens becuse substr can be used as an lvalue, but I am not
> 100% sure.

The SvPV_nolen() in the standard typemap for T_PV should handle that
correctly.

A non-default typemap that changes the mapping for char * or changes
T_PV could break it.

If you have a more complete example please open a ticket.

Tony

Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About