Skip to content

Commit e133ac9

Browse files
committed
Emphasize SvPVbyte and SvPVutf8 over SvPV.
1 parent 3e25f6d commit e133ac9

File tree

4 files changed

+126
-27
lines changed

4 files changed

+126
-27
lines changed

dist/ExtUtils-ParseXS/lib/perlxs.pod

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -603,7 +603,7 @@ and C<$type> can be used as in typemaps.
603603

604604
bool_t
605605
rpcb_gettime(host,timep)
606-
char *host = (char *)SvPV_nolen($arg);
606+
char *host = (char *)SvPVbyte_nolen($arg);
607607
time_t &timep = 0;
608608
OUTPUT:
609609
timep
@@ -630,7 +630,7 @@ Here's a truly obscure example:
630630
bool_t
631631
rpcb_gettime(host,timep)
632632
time_t &timep; /* \$v{timep}=@{[$v{timep}=$arg]} */
633-
char *host + SvOK($v{timep}) ? SvPV_nolen($arg) : NULL;
633+
char *host + SvOK($v{timep}) ? SvPVbyte_nolen($arg) : NULL;
634634
OUTPUT:
635635
timep
636636

@@ -993,7 +993,7 @@ The XS code, with ellipsis, follows.
993993
char *host = "localhost";
994994
CODE:
995995
if( items > 1 )
996-
host = (char *)SvPV_nolen(ST(1));
996+
host = (char *)SvPVbyte_nolen(ST(1));
997997
RETVAL = rpcb_gettime( host, &timep );
998998
OUTPUT:
999999
timep
@@ -1294,7 +1294,7 @@ prototypes.
12941294
char *host = "localhost";
12951295
CODE:
12961296
if( items > 1 )
1297-
host = (char *)SvPV_nolen(ST(1));
1297+
host = (char *)SvPVbyte_nolen(ST(1));
12981298
RETVAL = rpcb_gettime( host, &timep );
12991299
OUTPUT:
13001300
timep

dist/ExtUtils-ParseXS/lib/perlxstut.pod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1143,7 +1143,7 @@ Mytest.xs:
11431143
for (n = 0; n <= numpaths; n++) {
11441144
HV * rh;
11451145
STRLEN l;
1146-
char * fn = SvPV(*av_fetch((AV *)SvRV(paths), n, 0), l);
1146+
char * fn = SvPVbyte(*av_fetch((AV *)SvRV(paths), n, 0), l);
11471147

11481148
i = statfs(fn, &buf);
11491149
if (i != 0) {

pod/perlguts.pod

Lines changed: 115 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -153,37 +153,73 @@ Perl's own functions typically add a trailing C<NUL> for this reason.
153153
Nevertheless, you should be very careful when you pass a string stored
154154
in an SV to a C function or system call.
155155

156-
To access the actual value that an SV points to, you can use the macros:
157-
158-
SvIV(SV*)
159-
SvUV(SV*)
160-
SvNV(SV*)
161-
SvPV(SV*, STRLEN len)
162-
SvPV_nolen(SV*)
163-
164-
which will automatically coerce the actual scalar type into an IV, UV, double,
165-
or string.
166-
167-
In the C<SvPV> macro, the length of the string returned is placed into the
168-
variable C<len> (this is a macro, so you do I<not> use C<&len>). If you do
169-
not care what the length of the data is, use the C<SvPV_nolen> macro.
170-
Historically the C<SvPV> macro with the global variable C<PL_na> has been
171-
used in this case. But that can be quite inefficient because C<PL_na> must
156+
To access the actual value that an SV points to, Perl's API exposes
157+
several macros that coerce the actual scalar type into an IV, UV, double,
158+
or string:
159+
160+
=over
161+
162+
=item * C<SvIV(SV*)> (C<IV>) and C<SvUV(SV*)> (C<UV>)
163+
164+
=item * C<SvNV(SV*)> (C<double>)
165+
166+
=item * Strings are a bit complicated:
167+
168+
=over
169+
170+
=item * Byte string: C<SvPVbyte(SV*, STRLEN len)> or C<SvPVbyte_nolen(SV*)>
171+
172+
If the Perl string is C<"\xff\xff">, then this returns a 2-byte C<char*>.
173+
174+
=item * UTF-8 string: C<SvPVutf8(SV*, STRLEN len)> or C<SvPVutf8_nolen(SV*)>
175+
176+
If the Perl string is C<"\xff\xff">, then this returns a 4-byte C<char*>.
177+
178+
=item * You can also use C<SvPV(SV*, STRLEN len)> or C<SvPV_nolen(SV*)>
179+
to fetch the SV's raw internal buffer. This is tricky, though; if your Perl
180+
string
181+
is C<"\xff\xff">, then depending on the SV's internal encoding you might get
182+
back a 2-byte B<OR> a 4-byte C<char*>.
183+
Moreover, if it's the 4-byte string, that could come from either Perl
184+
C<"\xff\xff"> stored UTF-8 encoded, or Perl C<"\xc3\xbf\xc3\xbf"> stored
185+
as raw octets. To differentiate between these you B<MUST> look up the
186+
SV's UTF8 bit (cf. C<SvUTF8>) to know whether the source Perl string
187+
is 2 characters (C<SvUTF8> would be on) or 4 characters (C<SvUTF8> would be
188+
off).
189+
190+
B<IMPORTANT:> Use of C<SvPV>, C<SvPV_nolen>, or
191+
similarly-named macros I<without> looking up the SV's UTF8 bit is
192+
almost certainly a bug.
193+
194+
=back
195+
196+
(See L</How do I pass a Perl string to a C library?> for more details.)
197+
198+
In C<SvPVbyte>, C<SvPVutf8>, and C<SvPV>, the length of the C<char*> returned
199+
is placed into the
200+
variable C<len> (these are macros, so you do I<not> use C<&len>). If you do
201+
not care what the length of the data is, use C<SvPVbyte_nolen>,
202+
C<SvPVutf8_nolen>, or C<SvPV_nolen> instead.
203+
The global variable C<PL_na> can also be given to
204+
C<SvPVbyte>/C<SvPVutf8>/C<SvPV>
205+
in this case. But that can be quite inefficient because C<PL_na> must
172206
be accessed in thread-local storage in threaded Perl. In any case, remember
173207
that Perl allows arbitrary strings of data that may both contain NULs and
174208
might not be terminated by a C<NUL>.
175209

176-
Also remember that C doesn't allow you to safely say C<foo(SvPV(s, len),
210+
Also remember that C doesn't allow you to safely say C<foo(SvPVbyte(s, len),
177211
len);>. It might work with your
178212
compiler, but it won't work for everyone.
179213
Break this sort of statement up into separate assignments:
180214

181215
SV *s;
182216
STRLEN len;
183217
char *ptr;
184-
ptr = SvPV(s, len);
218+
ptr = SvPVbyte(s, len);
185219
foo(ptr, len);
186220

221+
=back
222+
187223
If you want to know if the scalar value is TRUE, you can use:
188224

189225
SvTRUE(SV*)
@@ -200,7 +236,7 @@ add space for the trailing C<NUL> byte (perl's own string functions typically do
200236
C<SvGROW(sv, len + 1)>).
201237

202238
If you want to write to an existing SV's buffer and set its value to a
203-
string, use SvPV_force() or one of its variants to force the SV to be
239+
string, use SvPVbyte_force() or one of its variants to force the SV to be
204240
a PV. This will remove any of various types of non-stringness from
205241
the SV while preserving the content of the SV in the PV. This can be
206242
used, for example, to append data from an API function to a buffer
@@ -3243,6 +3279,66 @@ There is no published API for dealing with this, as it is subject to
32433279
change, but you can look at the code for C<pp_lc> in F<pp.c> for an
32443280
example as to how it's currently done.
32453281

3282+
=head2 How do I pass a Perl string to a C library?
3283+
3284+
A Perl string, conceptually, is an opaque sequence of code points.
3285+
Many C libraries expect their inputs to be "classical" C strings, which are
3286+
arrays of octets 1-255, terminated with a NUL byte. Your job when writing
3287+
an interface between Perl and a C library is to define the mapping between
3288+
Perl and that library.
3289+
3290+
Generally speaking, C<SvPVbyte> and related macros suit this task well.
3291+
These assume that your Perl string is a "byte string", i.e., is either
3292+
raw, undecoded input into Perl or is pre-encoded to, e.g., UTF-8.
3293+
3294+
Alternatively, if your C library expects UTF-8 text, you can use
3295+
C<SvPVutf8> and related macros. This has the same effect as encoding
3296+
to UTF-8 then calling the corresponding C<SvPVbyte>-related macro.
3297+
3298+
Some C libraries may expect other encodings (e.g., UTF-16LE). To give
3299+
Perl strings to such libraries
3300+
you must either do that encoding in Perl then use C<SvPVbyte>, or
3301+
use an intermediary C library to convert from however Perl stores the
3302+
string to the desired encoding.
3303+
3304+
Take care also that NULs in your Perl string don't confuse the C
3305+
library. If possible, give the string's length to the C library; if that's
3306+
not possible, consider rejecting strings that contain NUL bytes.
3307+
3308+
=head3 What about C<SvPV>, C<SvPV_nolen>, etc.?
3309+
3310+
Consider a 3-character Perl string C<$foo = "\x64\x78\x8c">.
3311+
Perl can store these 3 characters either of two ways:
3312+
3313+
=over
3314+
3315+
=item * bytes: 0x64 0x78 0x8c
3316+
3317+
=item * UTF-8: 0x64 0x78 0xc2 0x8c
3318+
3319+
=back
3320+
3321+
Now let's say you convert C<$foo> to a C string thus:
3322+
3323+
STRLEN strlen;
3324+
char *str = SvPV(foo_sv, strlen);
3325+
3326+
At this point C<str> could point to a 3-byte C string or a 4-byte one.
3327+
3328+
Generally speaking, we want C<str> to be the same regardless of how
3329+
Perl stores C<$foo>, so the ambiguity here is undesirable. C<SvPVbyte>
3330+
and C<SvPVutf8> solve that by giving predictable output: use
3331+
C<SvPVbyte> if your C library expects byte strings, or C<SvPVutf8>
3332+
if it expects UTF-8.
3333+
3334+
If your C library happens to support both encodings, then of course
3335+
C<SvPV>--always in tandem with lookups to C<SvUTF8>!--is preferable since
3336+
it will avoid superfluous encoding/decoding operations.
3337+
3338+
B<TESTING> B<TIP:> Use L<utf8>'s C<upgrade> and C<downgrade> functions
3339+
in your tests to ensure consistent handling regardless of Perl's
3340+
internal encoding.
3341+
32463342
=head2 How do I convert a string to UTF-8?
32473343

32483344
If you're mixing UTF-8 and non-UTF-8 strings, it is necessary to upgrade

sv.h

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -801,7 +801,10 @@ compiler will complain if you were to try to modify the contents of the string,
801801
(unless you cast away const yourself).
802802
803803
=for apidoc Am|STRLEN|SvCUR|SV* sv
804-
Returns the length of the string which is in the SV. See C<L</SvLEN>>.
804+
Returns the length, in bytes, of the C string which is in the SV.
805+
Note that this may not match Perl's C<length>; for that, use
806+
C<SvUTF8(sv) ? sv_len_utf8(sv) : sv_len(sv)>.
807+
See C<L</SvLEN>> also.
805808
806809
=for apidoc Am|STRLEN|SvLEN|SV* sv
807810
Returns the size of the string buffer in the SV, not including any part
@@ -855,8 +858,8 @@ Set the value of the MAGIC pointer in C<sv> to val. See C<L</SvIV_set>>.
855858
Set the value of the STASH pointer in C<sv> to val. See C<L</SvIV_set>>.
856859
857860
=for apidoc Am|void|SvCUR_set|SV* sv|STRLEN len
858-
Set the current length of the string which is in the SV. See C<L</SvCUR>>
859-
and C<SvIV_set>>.
861+
Sets the current length, in bytes, of the C string which is in the SV.
862+
See C<L</SvCUR>> and C<SvIV_set>>.
860863
861864
=for apidoc Am|void|SvLEN_set|SV* sv|STRLEN len
862865
Set the size of the string buffer for the SV. See C<L</SvLEN>>.

0 commit comments

Comments
 (0)