diff options
| author | Yukihiro "Matz" Matsumoto <[email protected]> | 2013-08-27 19:39:05 -0700 |
|---|---|---|
| committer | Yukihiro "Matz" Matsumoto <[email protected]> | 2013-08-27 19:39:05 -0700 |
| commit | 9cc5797894049be41ffffb786552aaf13d1a2051 (patch) | |
| tree | bc0f3ab1cdb846fa004f6a19fc706492895a5a7d /mrbgems/mruby-string-ext | |
| parent | 91b878e5dfa671b63c26019962736dda1a58f9fc (diff) | |
| parent | f77dc2e6cb2c2ea21c6cca96f88d06acfbf30d17 (diff) | |
| download | mruby-9cc5797894049be41ffffb786552aaf13d1a2051.tar.gz mruby-9cc5797894049be41ffffb786552aaf13d1a2051.zip | |
Merge pull request #1486 from iij/pr-typeerror-string-start-end-with
Crash if you call String#start_with/end_with with non-String arguments.
Diffstat (limited to 'mrbgems/mruby-string-ext')
| -rw-r--r-- | mrbgems/mruby-string-ext/src/string.c | 18 | ||||
| -rw-r--r-- | mrbgems/mruby-string-ext/test/string.rb | 2 |
2 files changed, 14 insertions, 6 deletions
diff --git a/mrbgems/mruby-string-ext/src/string.c b/mrbgems/mruby-string-ext/src/string.c index 7bb6254ce..6718e734a 100644 --- a/mrbgems/mruby-string-ext/src/string.c +++ b/mrbgems/mruby-string-ext/src/string.c @@ -112,16 +112,19 @@ mrb_str_concat2(mrb_state *mrb, mrb_value self) static mrb_value mrb_str_start_with(mrb_state *mrb, mrb_value self) { - mrb_value *argv; + mrb_value *argv, sub; int argc, i; mrb_get_args(mrb, "*", &argv, &argc); for (i = 0; i < argc; i++) { size_t len_l, len_r; + int ai = mrb_gc_arena_save(mrb); + sub = mrb_string_type(mrb, argv[i]); + mrb_gc_arena_restore(mrb, ai); len_l = RSTRING_LEN(self); - len_r = RSTRING_LEN(argv[i]); + len_r = RSTRING_LEN(sub); if (len_l >= len_r) { - if (memcmp(RSTRING_PTR(self), RSTRING_PTR(argv[i]), len_r) == 0) { + if (memcmp(RSTRING_PTR(self), RSTRING_PTR(sub), len_r) == 0) { return mrb_true_value(); } } @@ -138,17 +141,20 @@ mrb_str_start_with(mrb_state *mrb, mrb_value self) static mrb_value mrb_str_end_with(mrb_state *mrb, mrb_value self) { - mrb_value *argv; + mrb_value *argv, sub; int argc, i; mrb_get_args(mrb, "*", &argv, &argc); for (i = 0; i < argc; i++) { size_t len_l, len_r; + int ai = mrb_gc_arena_save(mrb); + sub = mrb_string_type(mrb, argv[i]); + mrb_gc_arena_restore(mrb, ai); len_l = RSTRING_LEN(self); - len_r = RSTRING_LEN(argv[i]); + len_r = RSTRING_LEN(sub); if (len_l >= len_r) { if (memcmp(RSTRING_PTR(self) + (len_l - len_r), - RSTRING_PTR(argv[i]), + RSTRING_PTR(sub), len_r) == 0) { return mrb_true_value(); } diff --git a/mrbgems/mruby-string-ext/test/string.rb b/mrbgems/mruby-string-ext/test/string.rb index 6bd868993..3ab959437 100644 --- a/mrbgems/mruby-string-ext/test/string.rb +++ b/mrbgems/mruby-string-ext/test/string.rb @@ -105,10 +105,12 @@ assert('String#start_with?') do assert_true "hello".start_with?("heaven", "hell") assert_true !"hello".start_with?("heaven", "paradise") assert_true !"h".start_with?("heaven", "hell") + assert_raise TypeError do "hello".start_with?(true) end end assert('String#end_with?') do assert_true "string".end_with?("ing", "mng") assert_true !"string".end_with?("str", "tri") assert_true !"ng".end_with?("ing", "mng") + assert_raise TypeError do "hello".end_with?(true) end end |
