Skip to content

Commit

Permalink
Fix an out of bounds write in Data-Dumper with malformed utf8 input
Browse files Browse the repository at this point in the history
When warnings are enabled and Dumper() is called with an invalid utf8
string that still has the UTF8 flag on, esc_q_utf8() miscounts the size
of the escaped string.
  • Loading branch information
ntyni authored and Father Chrysostomos committed Nov 7, 2010
1 parent 6d351bf commit 7d3a730
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
6 changes: 5 additions & 1 deletion dist/Data-Dumper/Dumper.xs
Original file line number Diff line number Diff line change
Expand Up @@ -142,11 +142,15 @@ esc_q_utf8(pTHX_ SV* sv, register const char *src, register STRLEN slen)
STRLEN single_quotes = 0;
STRLEN qq_escapables = 0; /* " $ @ will need a \ in "" strings. */
STRLEN normal = 0;
int increment;

/* this will need EBCDICification */
for (s = src; s < send; s += UTF8SKIP(s)) {
for (s = src; s < send; s += increment) {
const UV k = utf8_to_uvchr((U8*)s, NULL);

/* check for invalid utf8 */
increment = (k == 0 && *s != '\0') ? 1 : UTF8SKIP(s);

#ifdef EBCDIC
if (!isprint(k) || k > 256) {
#else
Expand Down
14 changes: 13 additions & 1 deletion dist/Data-Dumper/t/bugs.t
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ BEGIN {
}

use strict;
use Test::More tests => 6;
use Test::More tests => 7;
use Data::Dumper;

{
Expand Down Expand Up @@ -85,4 +85,16 @@ Data::Dumper->Dump([*{*STDERR{IO}}]);
ok("ok", #ok
"empty-string glob [perl #72332]");

# writing out of bounds with malformed utf8
SKIP: {
eval { require Encode };
skip("Encode not available", 1) if $@;
local $^W=1;
local $SIG{__WARN__} = sub {};
my $a="\x{fc}'" x 50;
Encode::_utf8_on($a);
Dumper $a;
ok("ok", "no crash dumping malformed utf8 with the utf8 flag on");
}

# EOF

0 comments on commit 7d3a730

Please sign in to comment.