Merge pull request #1485 from fjmilens3/string_end_with

Bug in String#end_with? resulting from incorrect length determination
This commit is contained in:
Yukihiro "Matz" Matsumoto
2013-08-26 04:17:20 -07:00
2 changed files with 9 additions and 7 deletions
+8 -7
View File
@@ -143,15 +143,16 @@ mrb_str_end_with(mrb_state *mrb, mrb_value self)
mrb_get_args(mrb, "*", &argv, &argc);
for (i = 0; i < argc; i++) {
size_t len_l, len_r, len_cmp;
size_t len_l, len_r;
len_l = RSTRING_LEN(self);
len_r = RSTRING_LEN(argv[i]);
len_cmp = (len_l > len_r) ? len_r : len_l;
if (memcmp(RSTRING_PTR(self) + (len_l - len_cmp),
RSTRING_PTR(argv[i]) + (len_r - len_cmp),
len_cmp) == 0) {
return mrb_true_value();
}
if (len_l >= len_r) {
if (memcmp(RSTRING_PTR(self) + (len_l - len_r),
RSTRING_PTR(argv[i]),
len_r) == 0) {
return mrb_true_value();
}
}
}
return mrb_false_value();
}
+1
View File
@@ -110,4 +110,5 @@ 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")
end