mirror of
https://github.com/mruby/mruby
synced 2026-06-08 16:11:16 +00:00
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.
This commit is contained in:
@@ -41,6 +41,7 @@ mrb_value mrb_ptr_to_str(mrb_state *, void *);
|
||||
mrb_value mrb_obj_as_string(mrb_state *mrb, mrb_value obj);
|
||||
mrb_value mrb_str_resize(mrb_state *mrb, mrb_value str, mrb_int len);
|
||||
mrb_value mrb_str_substr(mrb_state *mrb, mrb_value str, mrb_int beg, mrb_int len);
|
||||
mrb_value mrb_string_type(mrb_state *mrb, mrb_value str);
|
||||
mrb_value mrb_check_string_type(mrb_state *mrb, mrb_value str);
|
||||
mrb_value mrb_str_buf_new(mrb_state *mrb, mrb_int capa);
|
||||
mrb_value mrb_str_buf_cat(mrb_state *mrb, mrb_value str, const char *ptr, size_t len);
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -1524,6 +1524,12 @@ mrb_ptr_to_str(mrb_state *mrb, void *p)
|
||||
return mrb_obj_value(p_str);
|
||||
}
|
||||
|
||||
mrb_value
|
||||
mrb_string_type(mrb_state *mrb, mrb_value str)
|
||||
{
|
||||
return mrb_convert_type(mrb, str, MRB_TT_STRING, "String", "to_str");
|
||||
}
|
||||
|
||||
mrb_value
|
||||
mrb_check_string_type(mrb_state *mrb, mrb_value str)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user