Commit e70b2721 authored by Yukihiro "Matz" Matsumoto's avatar Yukihiro "Matz" Matsumoto

Merge pull request #1210 from iij/pr-inspect-many-non-printables

String#inspec & String#dump may raise an "arena overflow" error
parents 62ca4c17 b1114489
...@@ -17,7 +17,6 @@ ...@@ -17,7 +17,6 @@
#include "mruby.h" #include "mruby.h"
#include "mruby/array.h" #include "mruby/array.h"
#include "mruby/class.h" #include "mruby/class.h"
#include "mruby/numeric.h"
#include "mruby/range.h" #include "mruby/range.h"
#include "mruby/string.h" #include "mruby/string.h"
#include "re.h" #include "re.h"
...@@ -2349,16 +2348,9 @@ mrb_str_dump(mrb_state *mrb, mrb_value str) ...@@ -2349,16 +2348,9 @@ mrb_str_dump(mrb_state *mrb, mrb_value str)
*q++ = c; *q++ = c;
} }
else { else {
mrb_value octstr; *q++ = '\\';
mrb_value chr; sprintf(q, "%03o", c&0xff);
const char *ptr; q += 3;
int len;
chr = mrb_fixnum_value(c & 0xff);
octstr = mrb_fixnum_to_str(mrb, chr, 8);
ptr = mrb_str_body(octstr, &len);
memcpy(q, "\\000", 4);
memcpy(q + 4 - len, ptr, len);
q += 4;
} }
} }
} }
...@@ -2442,16 +2434,8 @@ mrb_str_inspect(mrb_state *mrb, mrb_value str) ...@@ -2442,16 +2434,8 @@ mrb_str_inspect(mrb_state *mrb, mrb_value str)
continue; continue;
} }
else { else {
mrb_value octstr; int n = sprintf(buf, "\\%03o", c & 0377);
mrb_value chr; mrb_str_buf_cat(mrb, result, buf, n);
const char *ptr;
int len;
chr = mrb_fixnum_value(c & 0xff);
octstr = mrb_fixnum_to_str(mrb, chr, 8);
ptr = mrb_str_body(octstr, &len);
memcpy(buf, "\\000", 4);
memcpy(buf + 4 - len, ptr, len);
mrb_str_buf_cat(mrb, result, buf, 4);
continue; continue;
} }
} }
......
...@@ -415,3 +415,10 @@ assert('String#each_byte') do ...@@ -415,3 +415,10 @@ assert('String#each_byte') do
bytes1 == bytes2 bytes1 == bytes2
end end
assert('String#dump') do
("\1" * 100).dump # should not raise an exception - regress #1210
end
assert('String#inspect') do
("\1" * 100).inspect # should not raise an exception - regress #1210
end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment