[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index][Thread Index][Top&Search][Original]

Re: [ID 19991229.002] Bug when using 4-param substr as an argument to a function



On Wed, 29 Dec 1999 17:21:45 PST, Jim Bodwin wrote:
>sub foo {
>    my ($str) = @_;
>    return uc $str;
>}
>
>
>my ($str) = "abcdefghijklmnopqrstuvwxyz";
>
>my ($ucafew) = foo(substr($str, 0, 4, "xxxx"));
>
>print "$ucafew $str\n";
>
>my ($notuc) = substr($str, 0, 4, "yyyy");
>
>print "$notuc $str\n";
>
>============================================
>$ perl /tmp/perlbug.pl
>ABCD abcdefghijklmnopqrstuvwxyz      <===== should be xxxxefghijkl...
>abcd yyyyefghijklmnopqrstuvwxyz

Here's a fix.


Sarathy
gsar@ActiveState.com
-----------------------------------8<-----------------------------------
Change 4747 by gsar@auger on 2000/01/02 20:17:36

	fix 4-arg substr() when used as argument to subroutine

Affected files ...

... //depot/perl/pp.c#166 edit
... //depot/perl/t/op/substr.t#12 edit

Differences ...

==== //depot/perl/pp.c#166 (text) ====
Index: perl/pp.c
--- perl/pp.c.~1~	Wed Jan  5 10:44:22 2000
+++ perl/pp.c	Wed Jan  5 10:44:22 2000
@@ -2021,7 +2021,9 @@
 	    sv_pos_u2b(sv, &pos, &rem);
 	tmps += pos;
 	sv_setpvn(TARG, tmps, rem);
-	if (lvalue) {			/* it's an lvalue! */
+	if (repl)
+	    sv_insert(sv, pos, rem, repl, repl_len);
+	else if (lvalue) {		/* it's an lvalue! */
 	    if (!SvGMAGICAL(sv)) {
 		if (SvROK(sv)) {
 		    STRLEN n_a;
@@ -2050,8 +2052,6 @@
 	    LvTARGOFF(TARG) = pos;
 	    LvTARGLEN(TARG) = rem;
 	}
-	else if (repl)
-	    sv_insert(sv, pos, rem, repl, repl_len);
     }
     SPAGAIN;
     PUSHs(TARG);		/* avoid SvSETMAGIC here */

==== //depot/perl/t/op/substr.t#12 (xtext) ====
Index: perl/t/op/substr.t
--- perl/t/op/substr.t.~1~	Wed Jan  5 10:44:22 2000
+++ perl/t/op/substr.t	Wed Jan  5 10:44:22 2000
@@ -1,6 +1,6 @@
 #!./perl
 
-print "1..106\n";
+print "1..108\n";
 
 #P = start of string  Q = start of substr  R = end of substr  S = end of string
 
@@ -209,3 +209,9 @@
 eval 'substr($a,0,0,"") = "abc"';
 print "not " unless $@ && $@ =~ /Can't modify substr/ && $a eq "foo";
 print "ok 106\n";
+
+$a = "abcdefgh";
+print "not " unless sub { shift }->(substr($a, 0, 4, "xxxx")) eq 'abcd';
+print "ok 107\n";
+print "not " unless $a eq 'xxxxefgh';
+print "ok 108\n";
End of Patch.


[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index][Thread Index][Top&Search][Original]