Subject: | Typemap for std::string should not use SvCUR() |
Hello,
the current typemap for std::string does this:
$var = std::string( SvPV_nolen( $arg ), SvCUR( $arg ) );
However, SvCUR() gets called before SvPV_nolen() but it does not guarantee the PV value is updated before computing length. See the following test case:
======
use Devel::Peek qw(Dump);
use Inline C => ();
my $value = '0.1';
$value = 2;
Dump($value);
foo($value);
__END__
__C__
void foo (SV* var) {
printf("Got %s with length = %zu\n", SvPV_nolen(var), SvCUR(var));
}
// expected output: Got 2 with length = 1
// actual output: Got 2 with length = 3 <- WRONG!
======
So, something like this should be used in order to ensure std::string() is constructed using the right length.
T_STD_STRING_PTR
size_t len;
const char * c = SvPV($arg, len);
$var = new std::string(c, len);