Internals::SvREFCNT() now treats reference counts as unsigned
authorTony Cook <tony@develop-help.com>
Mon, 14 Nov 2011 08:48:54 +0000 (19:48 +1100)
committerTony Cook <tony@develop-help.com>
Mon, 14 Nov 2011 08:48:54 +0000 (19:48 +1100)
Previously setting a large (negative in 32-bit signed) reference count
would be returned as a positive number on 64-bit builds and negative
on 32-bit builds.

lib/Internals.t
universal.c

index d6c2367..d3fce9c 100644 (file)
@@ -7,7 +7,7 @@ BEGIN {
     }
 }
 
-use Test::More tests => 76;
+use Test::More tests => 78;
 
 my $ro_err = qr/^Modification of a read-only value attempted/;
 
@@ -166,4 +166,11 @@ is(  Internals::SvREFCNT($foo{foo}), 1 );
 is(  Internals::SvREFCNT($foo, 2), 2, "update ref count");
 is(  Internals::SvREFCNT($foo), 2, "check we got the stored value");
 
+# the reference count is a U16, but was returned as an IV resulting in
+# different values between 32 and 64-bit builds
+my $big_count = 0xFFFFFFF0; # -16 32-bit signed
+is( Internals::SvREFCNT($foo, $big_count), $big_count,
+    "set reference count unsigned");
+is( Internals::SvREFCNT($foo), $big_count, "reference count unsigned");
+
 Internals::SvREFCNT($foo, 1 );
index 0599e67..6ea0e29 100644 (file)
@@ -924,11 +924,11 @@ XS(XS_Internals_SvREFCNT) /* This is dangerous stuff. */
     sv = SvRV(svz);
 
     if (items == 1)
-        XSRETURN_IV(SvREFCNT(sv) - 1); /* Minus the ref created for us. */
+        XSRETURN_UV(SvREFCNT(sv) - 1); /* Minus the ref created for us. */
     else if (items == 2) {
          /* I hope you really know what you are doing. */
-        SvREFCNT(sv) = SvIV(ST(1)) + 1; /* we free one ref on exit */
-        XSRETURN_IV(SvREFCNT(sv) - 1);
+        SvREFCNT(sv) = SvUV(ST(1)) + 1; /* we free one ref on exit */
+        XSRETURN_UV(SvREFCNT(sv) - 1);
     }
     XSRETURN_UNDEF; /* Can't happen. */
 }