develooper Front page | perl.perl5.porters | Postings from February 2004

Re: [perl #24346] pulling in stuff from outside the substr lvalue window

Thread Previous | Thread Next
From:
Dave Mitchell
Date:
February 29, 2004 08:42
Subject:
Re: [perl #24346] pulling in stuff from outside the substr lvalue window
Message ID:
20040229164300.GE28954@fdisolutions.com
On Wed, Oct 29, 2003 at 09:56:53AM +0000, Graham Barr wrote:
> It gets worse
> 
> #! /usr/bin/perl -w
> $a="abcdefg";
> for (substr($a,0, 4)) {
>     print "a=$a\n";
>     print "$_\n";
>     $_="12";
>     print "$_\n";
>     print "a=$a\n";
>     $_=".";
>     print "$_\n";
>     print "a=$a\n";
> }
> 
> prints:
> 
> a=abcdefg
> abcd
> 12ef
> a=12efg
> .g
> a=.g
> 
> So multiple assigns to the LV with strings that are shorter than the 
> original LV length will result in the string being nibbled away. Below 
> is a patch that makes it output what I think is expected
> 
> a=abcdefg
> abcd
> 12
> a=12efg
> .
> a=.efg
> 
> All tests still pass, so if there are no objections to this patch I 
> shall add some tests to t/op/substr.t to test for this specifically

Thanks, applied to bleed as change #22414.
Are you still intending to write those tests? ;-)

> 
> --- mg.c.orig	Wed Oct 29 08:28:52 2003
> +++ mg.c	Wed Oct 29 09:20:31 2003
> @@ -1744,16 +1744,20 @@
>  	sv_utf8_upgrade(lsv);
>   	sv_pos_u2b(lsv, &lvoff, &lvlen);
>  	sv_insert(lsv, lvoff, lvlen, tmps, len);
> +	LvTARGLEN(sv) = sv_len_utf8(sv);
>  	SvUTF8_on(lsv);
>      }
>      else if (lsv && SvUTF8(lsv)) {
>  	sv_pos_u2b(lsv, &lvoff, &lvlen);
> +	LvTARGLEN(sv) = len;
>  	tmps = (char*)bytes_to_utf8((U8*)tmps, &len);
>  	sv_insert(lsv, lvoff, lvlen, tmps, len);
>  	Safefree(tmps);
>      }
> -    else
> -        sv_insert(lsv, lvoff, lvlen, tmps, len);
> +    else {
> +	sv_insert(lsv, lvoff, lvlen, tmps, len);
> +	LvTARGLEN(sv) = len;
> +    }
> 
>      return 0;
>  }
> 
> Graham.

-- 
Technology is dominated by two types of people: those who understand what
they do not manage, and those who manage what they do not understand. 

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