Skip to content

Commit 3c3f883

Browse files
FGasperkhwilliamson
authored andcommitted
Docs: Emphasize SvPVbyte and SvPVutf8 over SvPV. This updates
perlguts, perlxs, perlxstut, and perlapi. Issue #18600
1 parent dace60f commit 3c3f883

File tree

5 files changed

+154
-29
lines changed

5 files changed

+154
-29
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: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1143,7 +1143,8 @@ 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+
SV * path = *av_fetch((AV *)SvRV(paths), n, 0);
1147+
char * fn = SvPVbyte(path, l);
11471148

11481149
i = statfs(fn, &buf);
11491150
if (i != 0) {

pod/perldelta.pod

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -143,8 +143,13 @@ XXX
143143

144144
=head1 Documentation
145145

146-
XXX Changes to files in F<pod/> go here. Consider grouping entries by
147-
file and be sure to link to the appropriate page, e.g. L<perlfunc>.
146+
L<perlguts> now explains in greater detail the need to consult SvUTF8
147+
when calling SvPV (or variants). A new "How do I pass a Perl string to a C
148+
library?" section in the same document discusses when to use which style of
149+
macro to read an SV's string value.
150+
151+
L<perlapi>, L<perlguts>, L<perlxs>, and L<perlxstut> now prefer SvPVbyte
152+
over SvPV.
148153

149154
=head2 New Documentation
150155

pod/perlguts.pod

Lines changed: 128 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -153,37 +153,86 @@ 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+
This is suitable for Perl strings that represent bytes.
175+
176+
=item * UTF-8 string: C<SvPVutf8(SV*, STRLEN len)> or C<SvPVutf8_nolen(SV*)>
177+
178+
If the Perl string is C<"\xff\xff">, then this returns a 4-byte C<char*>.
179+
180+
This is suitable for Perl strings that represent characters.
181+
182+
B<CAVEAT>: That C<char*> will be encoded via Perl's internal UTF-8 variant,
183+
which means that if the SV contains non-Unicode code points (e.g.,
184+
0x110000), then the result may contain extensions over valid UTF-8.
185+
See L<perlapi/is_strict_utf8_string> for some methods Perl gives
186+
you to check the UTF-8 validity of these macros' returns.
187+
188+
=item * You can also use C<SvPV(SV*, STRLEN len)> or C<SvPV_nolen(SV*)>
189+
to fetch the SV's raw internal buffer. This is tricky, though; if your Perl
190+
string
191+
is C<"\xff\xff">, then depending on the SV's internal encoding you might get
192+
back a 2-byte B<OR> a 4-byte C<char*>.
193+
Moreover, if it's the 4-byte string, that could come from either Perl
194+
C<"\xff\xff"> stored UTF-8 encoded, or Perl C<"\xc3\xbf\xc3\xbf"> stored
195+
as raw octets. To differentiate between these you B<MUST> look up the
196+
SV's UTF8 bit (cf. C<SvUTF8>) to know whether the source Perl string
197+
is 2 characters (C<SvUTF8> would be on) or 4 characters (C<SvUTF8> would be
198+
off).
199+
200+
B<IMPORTANT:> Use of C<SvPV>, C<SvPV_nolen>, or
201+
similarly-named macros I<without> looking up the SV's UTF8 bit is
202+
almost certainly a bug if non-ASCII input is allowed.
203+
204+
When the UTF8 bit is on, the same B<CAVEAT> about UTF-8 validity applies
205+
here as for C<SvPVutf8>.
206+
207+
=back
208+
209+
(See L</How do I pass a Perl string to a C library?> for more details.)
210+
211+
In C<SvPVbyte>, C<SvPVutf8>, and C<SvPV>, the length of the C<char*> returned
212+
is placed into the
213+
variable C<len> (these are macros, so you do I<not> use C<&len>). If you do
214+
not care what the length of the data is, use C<SvPVbyte_nolen>,
215+
C<SvPVutf8_nolen>, or C<SvPV_nolen> instead.
216+
The global variable C<PL_na> can also be given to
217+
C<SvPVbyte>/C<SvPVutf8>/C<SvPV>
218+
in this case. But that can be quite inefficient because C<PL_na> must
172219
be accessed in thread-local storage in threaded Perl. In any case, remember
173220
that Perl allows arbitrary strings of data that may both contain NULs and
174221
might not be terminated by a C<NUL>.
175222

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

181228
SV *s;
182229
STRLEN len;
183230
char *ptr;
184-
ptr = SvPV(s, len);
231+
ptr = SvPVbyte(s, len);
185232
foo(ptr, len);
186233

234+
=back
235+
187236
If you want to know if the scalar value is TRUE, you can use:
188237

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

202251
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
252+
string, use SvPVbyte_force() or one of its variants to force the SV to be
204253
a PV. This will remove any of various types of non-stringness from
205254
the SV while preserving the content of the SV in the PV. This can be
206255
used, for example, to append data from an API function to a buffer
@@ -3243,6 +3292,66 @@ There is no published API for dealing with this, as it is subject to
32433292
change, but you can look at the code for C<pp_lc> in F<pp.c> for an
32443293
example as to how it's currently done.
32453294

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

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

sv.h

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -801,7 +801,9 @@ 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 PV inside the SV.
805+
Note that this may not match Perl's C<length>; for that, use
806+
C<sv_len_utf8(sv)>. See C<L</SvLEN>> also.
805807
806808
=for apidoc Am|STRLEN|SvLEN|SV* sv
807809
Returns the size of the string buffer in the SV, not including any part
@@ -855,8 +857,8 @@ Set the value of the MAGIC pointer in C<sv> to val. See C<L</SvIV_set>>.
855857
Set the value of the STASH pointer in C<sv> to val. See C<L</SvIV_set>>.
856858
857859
=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>>.
860+
Sets the current length, in bytes, of the C string which is in the SV.
861+
See C<L</SvCUR>> and C<SvIV_set>>.
860862
861863
=for apidoc Am|void|SvLEN_set|SV* sv|STRLEN len
862864
Set the size of the string buffer for the SV. See C<L</SvLEN>>.
@@ -1657,6 +1659,14 @@ see C<L</SvPV_force>>.
16571659
16581660
The differences between the forms are:
16591661
1662+
The forms with neither C<byte> nor C<utf8> in their names (e.g., C<SvPV> or
1663+
C<SvPV_nolen>) can expose the SV's internal string buffer. If
1664+
that buffer consists entirely of bytes 0-255 and includes any bytes above
1665+
127, then you B<MUST> consult C<SvUTF8> to determine the actual code points
1666+
the string is meant to contain. Generally speaking, it is probably safer to
1667+
prefer C<SvPVbyte>, C<SvPVutf8>, and the like. See
1668+
L<perlguts/How do I pass a Perl string to a C library?> for more details.
1669+
16601670
The forms with C<flags> in their names allow you to use the C<flags> parameter
16611671
to specify to process 'get' magic (by setting the C<SV_GMAGIC> flag) or to skip
16621672
'get' magic (by clearing it). The other forms process 'get' magic, except for

0 commit comments

Comments
 (0)