develooper Front page | perl.perl5.porters | Postings from November 2005

[PATCH] concat interacts badly with magic

Thread Next
From:
Robin Houston
Date:
November 21, 2005 09:04
Subject:
[PATCH] concat interacts badly with magic
Message ID:
20051121170339.GA8214@rpc142.cs.man.ac.uk
This patch fixes the bug described at
http://www.perlmonks.org/?node_id=510411

whereby code such as:

  sub TIESCALAR { bless {} }
  sub FETCH { shift()->{i} ++ }
  tie $h, "main";
  print $h.$h;

would print "00" instead of the expected "01".

Robin

--- pp_hot.c.orig	2005-11-21 16:22:04.000000000 +0000
+++ pp_hot.c	2005-11-21 17:00:57.000000000 +0000
@@ -148,11 +148,14 @@
     dPOPTOPssrl;
     bool lbyte;
     STRLEN rlen;
-    const char *rpv = SvPV_const(right, rlen);	/* mg_get(right) happens here */
-    const bool rbyte = !DO_UTF8(right);
+    const char *rpv;
+    bool rbyte;
     bool rcopied = FALSE;
 
     if (TARG == right && right != left) {
+	/* mg_get(right) may happen here ... */
+	rpv = SvPV_const(right, rlen);
+	rbyte = !DO_UTF8(right);
 	right = sv_2mortal(newSVpvn(rpv, rlen));
 	rpv = SvPV_const(right, rlen);	/* no point setting UTF-8 here */
 	rcopied = TRUE;
@@ -179,6 +182,11 @@
 	    SvUTF8_off(TARG);
     }
 
+    /* or mg_get(right) may happen here */
+    if (!rcopied) {
+	rpv = SvPV_const(right, rlen);
+	rbyte = !DO_UTF8(right);
+    }
     if (lbyte != rbyte) {
 	if (lbyte)
 	    sv_utf8_upgrade_nomg(TARG);
--- t/op/tie.t.orig	2005-11-21 16:50:25.000000000 +0000
+++ t/op/tie.t	2005-11-21 16:52:07.000000000 +0000
@@ -578,3 +578,10 @@
 print $h,"\n";
 EXPECT
 3.3
+########
+sub TIESCALAR { bless {} }
+sub FETCH { shift()->{i} ++ }
+tie $h, "main";
+print $h.$h;
+EXPECT
+01

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