xref: /openbsd-src/gnu/usr.bin/perl/t/re/qr-72922.t (revision e068048151d29f2562a32185e21a8ba885482260)
1#!perl -w
2use strict;
3
4BEGIN {
5    chdir 't' if -d 't';
6    require './test.pl';
7}
8
9no warnings 'experimental::builtin';
10use builtin 'weaken';
11
12plan(tests => 14);
13
14# [perl 72922]: A 'copy' of a Regex object which has magic should not crash
15# When a Regex object was copied and the copy weaken then the original regex object
16# could no longer be 'copied' with qr//
17
18sub s1 {
19    my $re = qr/abcdef/;
20    my $re_copy1 = $re;
21    my $re_weak_copy = $re;;
22    weaken($re_weak_copy);
23    my $re_copy2 = qr/$re/;
24
25    my $str_re = "$re";
26    is("$$re_weak_copy", $str_re, "weak copy equals original");
27    is("$re_copy1", $str_re, "copy1 equals original");
28    is("$re_copy2", $str_re, "copy2 equals original");
29
30    my $refcnt_start = Internals::SvREFCNT($$re_weak_copy);
31
32    undef $re;
33    refcount_is $re_weak_copy, $refcnt_start - 1, "refcnt decreased";
34    is("$re_weak_copy", $str_re, "weak copy still equals original");
35
36    undef $re_copy2;
37    refcount_is $re_weak_copy, $refcnt_start - 1, "refcnt not decreased";
38    is("$re_weak_copy", $str_re, "weak copy still equals original");
39}
40s1();
41s1();
42