summaryrefslogtreecommitdiffhomepage
path: root/mrbgems/mruby-string-ext/src/string.c
diff options
context:
space:
mode:
authorYukihiro "Matz" Matsumoto <[email protected]>2013-08-26 04:16:50 -0700
committerYukihiro "Matz" Matsumoto <[email protected]>2013-08-26 04:16:50 -0700
commitb11ae3fddbda288effeaa74c6fe5e6880f028af0 (patch)
tree4b0edd074dedcd83aa73d90b2666555161f0df84 /mrbgems/mruby-string-ext/src/string.c
parent975d670ab740c552f7f952c155aae90967d9d829 (diff)
parent2548569f60a9e64843f9da04cb497d268af33082 (diff)
downloadmruby-b11ae3fddbda288effeaa74c6fe5e6880f028af0.tar.gz
mruby-b11ae3fddbda288effeaa74c6fe5e6880f028af0.zip
Merge pull request #1484 from fjmilens3/string_start_with
Bug in String#start_with? resulting from incorrect length determination
Diffstat (limited to 'mrbgems/mruby-string-ext/src/string.c')
-rw-r--r--mrbgems/mruby-string-ext/src/string.c12
1 files changed, 7 insertions, 5 deletions
diff --git a/mrbgems/mruby-string-ext/src/string.c b/mrbgems/mruby-string-ext/src/string.c
index edebcecbc..33ed3095b 100644
--- a/mrbgems/mruby-string-ext/src/string.c
+++ b/mrbgems/mruby-string-ext/src/string.c
@@ -107,6 +107,7 @@ mrb_str_concat2(mrb_state *mrb, mrb_value self)
* # returns true if one of the prefixes matches.
* "hello".start_with?("heaven", "hell") #=> true
* "hello".start_with?("heaven", "paradise") #=> false
+ * "h".start_with?("heaven", "hell") #=> false
*/
static mrb_value
mrb_str_start_with(mrb_state *mrb, mrb_value self)
@@ -116,13 +117,14 @@ mrb_str_start_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), RSTRING_PTR(argv[i]), len_cmp) == 0) {
- return mrb_true_value();
- }
+ if (len_l >= len_r) {
+ if (memcmp(RSTRING_PTR(self), RSTRING_PTR(argv[i]), len_r) == 0) {
+ return mrb_true_value();
+ }
+ }
}
return mrb_false_value();
}