in reply to Re^2: Bit vector fiddling with Inline C
in thread Bit vector fiddling with Inline C
Is casting the return of SvPV to an unsigned char* a sensible thing to be doing?
The union element returned by SvPV is defined as char *
#define _SV_HEAD_UNION \ union { \ char* svu_pv; /* pointer to malloced string */ \
Whether directly changing the bytes of a Perl variable in C runs the risk of 'breaking' Perl internals in some scenarios?
What you are doing in the sample code--neither lengthening nor shortening the perl allocated memory, only modifying the bits within it--should be about as safe as it gets.
There may be some risk of confusing Perl if you performed bitwise operations upon a string that was currently marked as other than bytes--eg. some form of utf.
Perl might subsequently try to perform some operation upon the PV assuming it contains a valid utf string, which might confuse it, but I wouldn;t expect any dire consequences. I suspect that you could create the same situation by passing a utf encoded scalar to vec.
The simple answer is don't use utf. (That is, at least don't pass utf strings to the function.)
Ideally, it would be possible to define a typemap that rejected attempts to pass utf encoded strings, but since perl (along with the rest of the world) has chosen to conflate multiple data formats as a single type, there's not much that can be done in that regard.
C nor any language has a mechanism for typing arrays of variable length entities, so the world is stuck with this mess until the powers that be see the problems it creates and do something sensible about it.
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^4: Bit vector fiddling with Inline C
by oxone (Friar) on May 09, 2011 at 16:52 UTC | |
by BrowserUk (Patriarch) on May 09, 2011 at 19:22 UTC | |
by oxone (Friar) on May 09, 2011 at 21:54 UTC | |
by syphilis (Archbishop) on May 10, 2011 at 02:12 UTC | |
by BrowserUk (Patriarch) on May 10, 2011 at 07:06 UTC | |
| |
by oxone (Friar) on May 10, 2011 at 05:56 UTC |