Thanks, your code works, and helped me figure it out.
I had two problems.
SvPOK_on must be called at some point, otherwise, even though it contains data, perl doesn't 'know' that it contains data. Also, I wasn't setting the SvCUR_set, which makes perl think I had an empty string.
I went through several variations on this theme, but all of them were missing that key piece... which makes perfect sense after reading perlguts a bit more carefully.
| [reply] |
perlguts doesn't cover the writing of strings as fully as it does reading. I went through the same confusion -- earlier versions of one of my XS distros, Sort::External, performed unnecessary copy operations before I figured it out.
Spelunking sv.h and sv.c in the Perl source can be very helpful.
| [reply] |
Actually, SvPV_nolen() is safer than SvPVX(); SvPVX() gives you a pointer to the "PV" (the scalar's string value) even if the scalar doesn't have a string value while SvPV_nolen() will force the scalar to get a string value if it doesn't have one already.
The problem is that the size of the buffer is specified but the length of the string stored in that buffer is never set. I'd change your XS-code-for-cargo-culting, (:, to:
int bufsize= 83;
SV* svBuf= newSV( bufsize );
char* pBuf= SvPV_nolen( svBuf );
...( ..., pBuf, bufsize, ... );
SvCUR_set( svBuf, length_of_data_written );
where "length_of_data_written" might be variable, such as the return value from the function that stuffs characters into pBuf.
| [reply] [d/l] |
Hi Tye,
While it's true that SvPV_nolen is safer for reading, SvPVX and SvPV_nolen are equally dangerous with regards to the write operation that TheDauthi wants to deploy. In both cases, it is absolutely necessary to...
- allocate space via newSV(STRLEN), SvGROW(SV*, STRLEN), etc.
- make the SV* "POK" via SvPOK_on(SV*), so that it knows it contains a string.
The SvPV_nolen macro first checks the SV's private SVf_POK flag to see whether it contains a string. If it does, then it returns the pointer to the string, via SvPVX(SV*). If it doesn't, it calls sv_2pv_flags, which spits out a Use of uninitialized value warning, upgrades the sv, and returns (char*)"". That's safe to read from, but if you try to write to it... kaboom.
Here's a demo app...
#!/usr/bin/perl
use strict;
use warnings;
use Inline C => <<'END_C';
void
POKe() {
SV *good_sv, *bad_sv;
char *good_ptr, *bad_ptr;
good_sv = newSV(83);
bad_sv = newSV(83);
SvPOK_on(good_sv); /* !!!! */
good_ptr = SvPV_nolen(good_sv);
Copy("Joy!", good_ptr, 4, char);
SvCUR_set(good_sv, 4);
fprintf(stderr, "%s\n", SvPVX(good_sv));
bad_ptr = SvPV_nolen(bad_sv);
fprintf(stderr, "wait for it...\n");
Copy("DEATH!", bad_ptr, 6, char);
fprintf(stderr, "in heaven, everything is fine...");
}
END_C
POKe();
... and here's the output on my system...
slothbear:~/perltest marvin$ perl sv_poke.plx
Joy!
Use of uninitialized value in subroutine entry at sv_poke.plx line 33.
wait for it...
Bus error
slothbear:~/perltest marvin$
| [reply] [d/l] [select] |
Thanks for the corrections. My memory was rusty and I didn't double check it. Mea culpa.
Having double checked now, I'd still do things differently than you did but I realized more of the reasons for my reluctance to using SvPVX() and SvPOK_on() and those had to do with dealing with scalars passed in rather than one freshly created right there. So I now agree that your suggestion is safe (for this particular case) (and mine was fatally flawed, of course).
I'd personally still avoid using SvPVX() and SvPOK_on() as sticking with techniques that work in both cases makes sense to me. But, I see I used SvPV_force() and getting the "use of undefined value" warning was actually a desired feature. If I was creating a new scalar, then I'd sv_setpvn(sv,"",0) before doing SvPV_force() and thus not get the warning when it wasn't appropriate (in part because these two steps would likely be in separate macros "of my own design"). But that has a trivial amount of extra overhead that some might dislike. I'd also use SvPOK_only() but do that and the SvCUR_set() only after the call to fill the allocated buffer had succeeded.
So I guess I'd do something like this in 3 steps: 1) Allocate a scalar containing an empty string (preferably with the desired size of buffer), 2) Prepare the scalar to have a large enough buffer and extract a pointer to it (using a macro that doesn't assume a pristine SV), 3) (after the buffer has been "filled") Mark the scalar as containing the string of the proper length. I find that layer of abstraction makes the XS subroutine easier to understand and each of my three macros easier to understand in isolation. In fact, I spent some concerted study on each of my macros, carefully verifying implementation details of the "sv" macros that they use to make sure my macros were correct and safe. And then I hapilly forgot most of the niggling details of all of those "sv" macros which I'd never faithfully remember anyway.
Thanks again and sorry for the misinformation.
| [reply] [d/l] |
The SvPV_nolen macro first checks the SV's private SVf_POK flag to see whether it contains a string. If it does, then it returns the pointer to the string, via SvPVX(SV*). If it doesn't, it calls sv_2pv_flags, which spits out a Use of uninitialized value warning, upgrades the sv, and returns (char*)"". That's safe to read from, but if you try to write to it... kaboom.
That was actually why I was calling SvPV_nolen (to force the SV to contain a string), but my understanding of it was a bit incorrect. I was expecting it to set that flag, but I was also expecting the return of the pointer to the existing buffer I had allocated with newSV, not an empty string.
When I realized that perl also had the length of the string (which, in retrospect, is obvious), the other part fell into place. I'm actually thinking that it IS safer to use SvPVX to get the buffer, so that I don't promote it 'by accident'.
| [reply] |