develooper Front page | perl.perl5.porters | Postings from January 2017

Re: [PATCH] Documentation patch to recommend newSVpvn over newSVpv.

Thread Previous | Thread Next
From:
Colin Newell
Date:
January 7, 2017 21:52
Subject:
Re: [PATCH] Documentation patch to recommend newSVpvn over newSVpv.
Message ID:
CAKPO2a4jpC+nPi94jeXE9hD4P2kP2xEruyOimt59vH72_nbkdg@mail.gmail.com
Thanks Leon, that was exactly what I was looking for.  I failed to
find it because I only looked in sv.c rather than looking at the docs
or looking further.

Is this doc patch closer to useful?


Colin.

On 7 January 2017 at 21:32, Leon Timmermans <fawaka@gmail.com> wrote:
> On Tue, Jan 3, 2017 at 9:09 PM, Colin Newell <colin.newell@gmail.com> wrote:
>>
>> ---
>>  sv.c | 11 ++++++++++-
>>  1 file changed, 10 insertions(+), 1 deletion(-)
>>
>> diff --git a/sv.c b/sv.c
>> index 83d82fc..bb158bf 100644
>> --- a/sv.c
>> +++ b/sv.c
>> @@ -9323,7 +9323,16 @@ SV is set to 1.  If C<len> is zero, Perl will
>> compute the length using
>>  C<strlen()>, (which means if you use this option, that C<s> can't have
>> embedded
>>  C<NUL> characters and has to have a terminating C<NUL> byte).
>>
>> -For efficiency, consider using C<newSVpvn> instead.
>> +This function can cause reliability issues if you are likely to pass in
>> +empty strings that are not null terminated because it will run
>> +strlen on the string and potentially run past valid memory.
>> +
>> +Using C<newSVpvn> is safer and more efficient regardless of the type
>> +of string passed in.
>> +
>> +Safer for non C<NUL> terminated strings, and more efficient for both
>> +strings as it will not have to make an additional comparison to determine
>> +whether or not to run strlen.
>>
>>  =cut
>>  */
>
>
> Recommending newSVpvn is generally a good idea, but newSVpvs (which used
> newSVpvn under the hood) should probably be mentioned at the same time.
>
> Leon

Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About