Fix bug #38815 (localising keys which are UTF-8 encoded didn't delete
Nicholas Clark [Thu, 30 Mar 2006 20:37:03 +0000 (20:37 +0000)]
them correctly on scope exit)

p4raw-id: //depot/perl@27637

pp_hot.c
scope.c
t/op/local.t

index 3b4d8ed..91940ad 100644 (file)
--- a/pp_hot.c
+++ b/pp_hot.c
@@ -1828,7 +1828,8 @@ PP(pp_helem)
                if (!preeminent) {
                    STRLEN keylen;
                    const char * const key = SvPV_const(keysv, keylen);
-                   SAVEDELETE(hv, savepvn(key,keylen), keylen);
+                   SAVEDELETE(hv, savepvn(key,keylen),
+                              SvUTF8(keysv) ? -(I32)keylen : keylen);
                } else
                    save_helem(hv, keysv, svp);
             }
diff --git a/scope.c b/scope.c
index a49a82a..7b76823 100644 (file)
--- a/scope.c
+++ b/scope.c
@@ -826,7 +826,7 @@ Perl_leave_scope(pTHX_ I32 base)
            ptr = SSPOPPTR;
            hv = (HV*)ptr;
            ptr = SSPOPPTR;
-           (void)hv_delete(hv, (char*)ptr, (U32)SSPOPINT, G_DISCARD);
+           (void)hv_delete(hv, (char*)ptr, (I32)SSPOPINT, G_DISCARD);
            SvREFCNT_dec(hv);
            Safefree(ptr);
            break;
index 561e395..bc24657 100755 (executable)
@@ -4,7 +4,7 @@ BEGIN {
     chdir 't' if -d 't';
     require './test.pl';
 }
-plan tests => 95;
+plan tests => 104;
 
 my $list_assignment_supported = 1;
 
@@ -368,3 +368,27 @@ sub f { ok(0 == $[); }
        ::ok(f1() eq "f1", "localised sub restored");
        ::ok(f2() eq "f2", "localised sub restored");
 }
+
+# Localising unicode keys (bug #38815)
+{
+    my %h;
+    $h{"\243"} = "pound";
+    $h{"\302\240"} = "octects";
+    is(scalar keys %h, 2);
+    {
+       my $unicode = chr 256;
+       my $ambigous = "\240" . $unicode;
+       chop $ambigous;
+       local $h{$unicode} = 256;
+       local $h{$ambigous} = 160;
+
+       is(scalar keys %h, 4);
+       is($h{"\243"}, "pound");
+       is($h{$unicode}, 256);
+       is($h{$ambigous}, 160);
+       is($h{"\302\240"}, "octects");
+    }
+    is(scalar keys %h, 2);
+    is($h{"\243"}, "pound");
+    is($h{"\302\240"}, "octects");
+}