Commit 234eb25561 for perl
commit 234eb25561c01ee9a1c88c252dbaa52d07c91785
Author: Yves Orton <demerphq@gmail.com>
Date: Thu Nov 5 16:17:41 2026 +0100
Data::Dumper: avoid interpreting regexp text as Perl dereferences
Data::Dumper can emit a qr// expression that Perl misreads when the dump
is evaluated. For example, dumping qr'${[1]}' can produce code that
tries to dereference a string and dies with 'Not a HASH reference'.
Escaping the dollar sign alone leaves the following brackets where Perl
can treat them as a dereference.
We now quote the sigil together with any immediately following opening
braces or brackets, then interpolate that text into the emitted pattern.
This gives the regex compiler the original text without letting the Perl
parser interpret it as a variable or dereference. Both the XS and pure
Perl dumpers use this approach.
The interpolation looks odd, but it saves us from having to write
another regex parser just to decide what to escape. We leave dollar
signs alone in common anchor positions to keep the output readable.
Those exceptions are for readability; quoting them would work too.
The documentation no longer promises that evaluating a dump produces an
identical copy of the original structure. It describes reconstruction as
a limited best effort for controlled debugging and development work,
states that production serialization through eval is unsupported, and
recommends dedicated data formats such as JSON or Sereal for network
exchange.
diff --git a/dist/Data-Dumper/Changes b/dist/Data-Dumper/Changes
index 817682db17..874ad2d211 100644
--- a/dist/Data-Dumper/Changes
+++ b/dist/Data-Dumper/Changes
@@ -7,6 +7,11 @@ in use.
Release history: https://metacpan.org/dist/Data-Dumper/releases
+2.193 2026-11-18 (cpan + core)
+ - Properly escape literal @ and $ in patterns, along with related fixes.
+ - Clarify the security policy. Dumper output may be eval'ed, but that
+ does not make it a safe technique for production data serialization.
+
2.192 2025-03-26 (core-only)
- Use PERL_MAGIC_vstring instead of a literal magic-type character in
the XS implementation.
diff --git a/dist/Data-Dumper/Dumper.pm b/dist/Data-Dumper/Dumper.pm
index 0ebe3485d4..e3d54c82ad 100644
--- a/dist/Data-Dumper/Dumper.pm
+++ b/dist/Data-Dumper/Dumper.pm
@@ -30,7 +30,7 @@ our ( $Indent, $Trailingcomma, $Purity, $Pad, $Varname, $Useqq, $Terse, $Freezer
our ( @ISA, @EXPORT, @EXPORT_OK, $VERSION );
BEGIN {
- $VERSION = '2.192'; # Don't forget to set version and release
+ $VERSION = '2.193'; # Don't forget to set version and release
# date in POD below!
@ISA = qw(Exporter);
@@ -365,16 +365,33 @@ sub _dump {
else {
$pat = "$val";
}
- $pat =~ s <
- (\\.) # anything backslash escaped
- | (\$)(?![)|]|\z) # any unescaped $, except $| $) and end
- | / # any unescaped /
- >
- {
- $1 ? $1
- : $2 ? '${\q($)}'
- : '\\/'
- }gex;
+ # Escape the pattern to make sure it is processed properly if the output
+ # is evalled. This includes handling things like escaping / characters
+ # as we output it as a qr/.../, along with sigils or other entities which
+ # might be misinterpreted.
+ # Note 1: Try to keep the $ anchor exceptions in sync with S_scan_const()
+ # in toke.c and the regex parser logic in regcomp.c
+ # Note 2: The exceptions are not required and this code should still DTRT
+ # but produce uglier output if the '$' exception rule were removed.
+ # Note 3: This escaping looks weird, but allows us to avoid concerns
+ # about whether we have misparsed the pattern or not. When the
+ # pattern is interpolated it comes back just as it was before, without
+ # us worrying if we misidentified something. The exception list is
+ # purely to make the output more readable in common cases where we
+ # are confident we won't misparse a $.
+ $pat =~ s ~
+ (\\.) # 1 == anything backslash escaped
+ | ( \@[\{\[]* # 2 == unescaped @ and optional { or [
+ | \$ # or unescaped $
+ (?![()|\\ \t\r\n]|\z) # .. except before one of these
+ [\{\[]*
+ ) # and optional { or [
+ | / # Otherwise: any unescaped /
+ ~
+ $1 ? $1 :
+ $2 ? '${\\q(' . $2 . ')}' :
+ '\\/'
+ ~gex;
$out .= "qr/$pat/$flags";
}
elsif ($realtype eq 'SCALAR' || $realtype eq 'REF'
@@ -882,9 +899,28 @@ perl syntax. The references can also be objects. The content of each
variable is output in a single Perl statement. Handles self-referential
structures correctly.
-The return value can be C<eval>ed to get back an identical copy of the
-original reference structure. (Please do consider the security implications
-of eval'ing code from untrusted sources!)
+Data::Dumper outputs Perl code which can be C<eval>ed to reconstruct data
+structures. This is useful in controlled debugging and development work,
+for example to generate a data set for tests when you control both its
+generation and its use. Using Data::Dumper and C<eval> for serialization in
+production is not supported.
+
+Reconstruction is a limited best effort. We aim to produce output that can
+be C<eval>ed without error, but cannot guarantee an exact round trip or safe
+evaluation of dumps of arbitrary, uncontrolled data, B<including data from
+untrusted sources>. This applies both to received dumps and to dumps you
+generate from untrusted data. Do not C<eval> dumps from untrusted sources.
+C<Purity> improves reconstruction of references, but does not guarantee
+fidelity or safety. See also L</BUGS>.
+
+For secure, portable data exchange over a network, use JSON (see
+L<JSON::PP|https://metacpan.org/pod/JSON::PP> or
+L<JSON|https://metacpan.org/pod/JSON>), Sereal (see
+L<Sereal::Encoder|https://metacpan.org/pod/Sereal::Encoder> and
+L<Sereal::Decoder|https://metacpan.org/pod/Sereal::Decoder>), or another data
+format designed for serialization. Use its decoder rather than C<eval>, and
+follow its guidance for untrusted input, resource limits, and object handling.
+Choose a format that supports the data structures you need to exchange.
Any references that are the same as one of those passed in will be named
C<$VAR>I<n> (where I<n> is a numeric suffix), and other duplicate references
@@ -1456,7 +1492,7 @@ modify it under the same terms as Perl itself.
=head1 VERSION
-Version 2.191
+Version 2.193
=head1 SEE ALSO
diff --git a/dist/Data-Dumper/Dumper.xs b/dist/Data-Dumper/Dumper.xs
index 1901701fc7..51ce54120a 100644
--- a/dist/Data-Dumper/Dumper.xs
+++ b/dist/Data-Dumper/Dumper.xs
@@ -579,6 +579,20 @@ dump_regexp(pTHX_ SV *retval, SV *val)
rend = p + rlen;
do_utf8 = DO_UTF8(sv_pattern);
+ /* Escape the pattern to make sure it is processed properly if the output
+ * is evalled. This includes handling things like escaping / characters
+ * as we output it as a qr/.../, along with sigils or other entities which
+ * might be misinterpreted.
+ * Note 1: Try to keep the $ anchor exceptions in sync with S_scan_const()
+ * in toke.c and the regex parser logic in regcomp.c
+ * Note 2: The exceptions are not required and this code should still DTRT
+ * but produce uglier output if the '$' exception rule were removed.
+ * Note 3: This escaping looks weird, but allows us to avoid concerns
+ * about whether we have misparsed the pattern or not. When the
+ * pattern is interpolated it comes back just as it was before, without
+ * us worrying if we misidentified something. The exception list is
+ * purely to make the output more readable in common cases where we
+ * are confident we won't misparse a $. */
while (p < rend) {
UV k = *p;
int saw_backslash = k == '\\';
@@ -594,10 +608,14 @@ dump_regexp(pTHX_ SV *retval, SV *val)
if (/* / that was not backslashed */
(k == '/' && !saw_backslash)
- /* $ that was not backslashed, unless it is at the end of the regex
- or it is followed by | or it is followed by ) */
+ /* Unescaped sigils can interpolate when the dump is evaluated. */
+ || (k == '@' && !saw_backslash)
+ /* Match the $ anchor exceptions in S_scan_const() in toke.c. */
|| (k == '$' && !saw_backslash
- && (p + 1 != rend && p[1] != '|' && p[1] != ')'))
+ && (p + 1 != rend && p[1] != ' '
+ && p[1] != '(' && p[1] != ')' && p[1] != '|'
+ && p[1] != '\\'
+ && p[1] != '\t' && p[1] != '\r' && p[1] != '\n'))
/* or need to use \x{} notation. */
|| (do_utf8 && ! UTF8_IS_INVARIANT(k)))
{
@@ -610,10 +628,14 @@ dump_regexp(pTHX_ SV *retval, SV *val)
sv_catpvs(retval, "\\/");
++p;
}
- else if (k == '$') {
- /* this approach suggested by Eirik Berg Hanssen: */
- sv_catpvs(retval, "${\\q($)}");
+ else if (k == '@' || k == '$') {
+ const U8 *start = p;
++p;
+ while (p < rend && (*p == '{' || *p == '['))
+ ++p;
+ sv_catpvs(retval, "${\\q(");
+ sv_catpvn(retval, (const char *) start, p - start);
+ sv_catpvs(retval, ")}");
}
else {
/* If there was a \, we have copied it already, so all that is
diff --git a/dist/Data-Dumper/t/dumper.t b/dist/Data-Dumper/t/dumper.t
index 62ddb4265a..ba88e84ab0 100644
--- a/dist/Data-Dumper/t/dumper.t
+++ b/dist/Data-Dumper/t/dumper.t
@@ -1756,6 +1756,7 @@ EOW
{
# [CPAN #84569]
my $dollar = '${\q($)}';
+ my $anchor = '$';
my $want = <<"EOW";
#\$VAR1 = [
# "\\x{2e18}",
@@ -1763,10 +1764,10 @@ EOW
# qr/^\$/,
# qr/${dollar}foo/,
# qr/\\\$foo/,
-# qr/$dollar \x{B6} /u,
-# qr/$dollar \x{203d} /u,
+# qr/$anchor \x{B6} /u,
+# qr/$anchor \x{203d} /u,
# qr/\\\$ \x{203d} /u,
-# qr/\\\\$dollar \x{203d} /u,
+# qr/\\\\$anchor \x{203d} /u,
# qr/ \$| \x{203d} /u,
# qr/ (\$) \x{203d} /u,
# '\xB6'
diff --git a/dist/Data-Dumper/t/qr.t b/dist/Data-Dumper/t/qr.t
index 47d150d576..7ccb4c33c0 100644
--- a/dist/Data-Dumper/t/qr.t
+++ b/dist/Data-Dumper/t/qr.t
@@ -3,9 +3,23 @@
use strict;
use warnings;
-use Test::More tests => 2;
+use Test::More;
use Data::Dumper;
+my $XS = defined &Data::Dumper::Dumpxs;
+my $dollar = '${\q($)}';
+my $at = '${\q(@)}';
+my $dollar_brace = '${\q(${)}';
+my $at_brace = '${\q(@{)}';
+my $dollar_brace_bracket = '${\q(${[)}';
+my $at_brace_bracket = '${\q(@{[)}';
+my $dollar_brace_two_brackets = '${\q(${[[)}';
+my $at_brace_two_brackets = '${\q(@{[[)}';
+my $dollar_two_braces = '${\q(${{)}';
+my $at_two_braces = '${\q(@{{)}';
+my $dollar_bracket = '${\q($[)}';
+my $at_bracket = '${\q(@[)}';
+
{
my $q = q| \/ |;
use Data::Dumper;
@@ -22,3 +36,88 @@ use Data::Dumper;
}
ok(!$@, "Dumping $qr with PP") or diag $@, Dumper $qr;
}
+
+{
+ our $DUMP_INTERPOLATED;
+ for my $case (
+ [ q(@{[ $main::DUMP_INTERPOLATED++ ]}), $at_brace_bracket ],
+ [ q(${[ $main::DUMP_INTERPOLATED++ ]}), $dollar_brace_bracket ],
+ [ q(${[1]}), $dollar_brace_bracket ],
+ [ q(${ [ $main::DUMP_INTERPOLATED++ ]}), $dollar_brace ],
+ [ q(@{2}), $at_brace . '2}' ],
+ [ q(${2}), $dollar_brace . '2}' ],
+ [ q(@{ 1,2 }), $at_brace . ' 1,2 }' ],
+ [ q(${ 1,2 }), $dollar_brace . ' 1,2 }' ],
+ [ q(@{1,}), $at_brace . '1,}' ],
+ [ q(${1,}), $dollar_brace . '1,}' ],
+ [ q(@[1]), $at_bracket . '1]' ],
+ [ q($[1]), $dollar_bracket . '1]' ],
+ [ q(@{[[1]]}), $at_brace_two_brackets . '1]]}' ],
+ [ q(${[[1]]}), $dollar_brace_two_brackets . '1]]}' ],
+ [ q(@{{2}), $at_two_braces . '2}' ],
+ [ q(${{2}), $dollar_two_braces . '2}' ],
+ [ q(@foo), $at . 'foo' ],
+ [ q($foo), $dollar . 'foo' ],
+ [ q($|), '$|' ],
+ [ q($\w), '$\w' ],
+ [ q($\\w), '$\\w' ],
+ [ q($!), $dollar . '!' ],
+ [ q(\@{x}), '\@{x}' ],
+ [ q(\${x}), '\${x}' ],
+ [ ('\\' x 2) . '@foo', ('\\' x 2) . $at . 'foo' ],
+ [ ('\\' x 2) . '$foo', ('\\' x 2) . $dollar . 'foo' ],
+ [ ('\\' x 3) . '@foo', ('\\' x 3) . '@foo' ],
+ [ ('\\' x 3) . '$foo', ('\\' x 3) . '$foo' ],
+ [ ('\\' x 4) . '@foo', ('\\' x 4) . $at . 'foo' ],
+ [ ('\\' x 4) . '$foo', ('\\' x 4) . $dollar . 'foo' ],
+ [ ('\\' x 2) . '@{x}', ('\\' x 2) . $at_brace . 'x}' ],
+ [ ('\\' x 2) . '$' . '{x}', ('\\' x 2) . $dollar_brace . 'x}' ],
+ [ ('\\' x 3) . '@{x}', ('\\' x 3) . '@{x}' ],
+ [ ('\\' x 3) . '$' . '{x}', ('\\' x 3) . '$' . '{x}' ],
+ [ ('\\' x 4) . '@{x}', ('\\' x 4) . $at_brace . 'x}' ],
+ [ ('\\' x 4) . '$' . '{x}', ('\\' x 4) . $dollar_brace . 'x}' ],
+ [ ('\\' x 2) . '@[1]', ('\\' x 2) . $at_bracket . '1]' ],
+ [ ('\\' x 2) . '$[1]', ('\\' x 2) . $dollar_bracket . '1]' ],
+ [ ('\\' x 3) . '@[1]', ('\\' x 3) . '@[1]' ],
+ [ ('\\' x 3) . '$[1]', ('\\' x 3) . '$[1]' ],
+ [ ('\\' x 4) . '@[1]', ('\\' x 4) . $at_bracket . '1]' ],
+ [ ('\\' x 4) . '$[1]', ('\\' x 4) . $dollar_bracket . '1]' ],
+ [ q[($)], '($)' ],
+ [ q[$(a)], '$(a)' ],
+ [ q[$ ], '$ ' ],
+ [ q[$], '$/' ],
+ ) {
+ my ($source, $escaped) = @$case;
+ my $regexp = qr/$source/;
+ for my $useperl (1, 0) {
+ next if !$XS && !$useperl;
+ local $Data::Dumper::Useperl = $useperl;
+ local $DUMP_INTERPOLATED = 0;
+ my $implementation = $useperl ? 'PP' : 'XS';
+ my $dump = Dumper($regexp);
+ like($dump, qr{qr/\Q$escaped\E},
+ "$implementation dumps $source");
+ my $restored;
+ {
+ no strict 'vars';
+ $restored = eval $dump;
+ }
+ is($@, '', "$implementation output evaluates for $source");
+ is($DUMP_INTERPOLATED, 0,
+ "$implementation does not interpolate $source");
+ if (!$@) {
+ for my $subject ('', '@', '@@', '@@@', '$', '$$',
+ '@{2}', '@{{', '${2}', '${[1]}',
+ '@{[1]}', '@[1]', '1', 'foo', '@foo', '$foo') {
+ is(!!($subject =~ $restored), !!($subject =~ $regexp),
+ "$implementation preserves $source against $subject");
+ }
+ }
+ if ($source eq '$|') {
+ ok('' =~ $restored, "$implementation preserves the anchor");
+ }
+ }
+ }
+}
+
+done_testing();
diff --git a/pod/perldelta.pod b/pod/perldelta.pod
index f7c632cbd2..5341616d18 100644
--- a/pod/perldelta.pod
+++ b/pod/perldelta.pod
@@ -131,6 +131,19 @@ XXX Remove this section if F<Porting/corelist-perldelta.pl> did not add any cont
=item *
+L<Data::Dumper> has been upgraded from version 2.192 to 2.193. Regexp dumps
+now escape sigils and following opening brackets together, so the dumped
+C<qr//> expression can be evaluated without unintended dereferences. For
+example, dumping C<qr'${[1]}'> no longer produces C<Not a HASH reference>
+when the dump is evaluated.
+
+The documentation clarifies that evaluating Data::Dumper output is useful
+in controlled development work but unsupported for production serialization.
+It describes safety and fidelity limits and recommends dedicated data
+formats such as JSON or Sereal for over-the-wire exchange.
+
+=item *
+
L<XXX> has been upgraded from version A.xx to B.yy.
XXX If there was something important to note about this change, include that here.