develooper Front page | perl.perl5.porters | Postings from October 2003

Re: find_encoding("UTF-16BE")->encode("abc") does not NUL-terminate

Thread Previous
From:
Gisle Aas
Date:
October 7, 2003 08:06
Subject:
Re: find_encoding("UTF-16BE")->encode("abc") does not NUL-terminate
Message ID:
lrisn15bsn.fsf@caliper.activestate.com
Gisle Aas <gisle@ActiveState.com> writes:

> The following invariant should always hold if SvPOK(sv):
> 
>    - SvCUR(sv) < SvLEN(sv)
>    - *SvEND(sv) == '\0'
> 
> The perl core ensures that and so should extensions.  The perlguts
> manpage says:
> 
>        All SVs that contain strings should be terminated with a
>        NUL character.  If it is not NUL-terminated there is a
>        risk of core dumps and corruptions from code which passes
>        the string to C functions or system calls which expect a
>        NUL-terminated string.

I just wanted to follow up on this with one more point.  It's not only
for external APIs this is important.  You will also find code that
does things like this:

   char *s = SvPVX(sv);
   //...
   while (isDIGIT(*s))
       s++;

This is unsafe unless the invariant described above holds.  The perl
core is full of similar cases and I know I've depended on this a lot
in the HTML::Parser module and other places.

--Gisle

Thread Previous


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