summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorFrederick John Milens III <[email protected]>2013-08-22 16:44:00 -0500
committerFrederick John Milens III <[email protected]>2013-08-22 16:44:00 -0500
commit28211d677173c8279cfa8b89dd7df31ec30ee1c3 (patch)
tree28be9819cdd66205cf687ef8be95c5b724084d6c
parentde5f5732fe222858e629f62aedaf6d32d14f167d (diff)
downloadmruby-28211d677173c8279cfa8b89dd7df31ec30ee1c3.tar.gz
mruby-28211d677173c8279cfa8b89dd7df31ec30ee1c3.zip
Fix for string-length-related issue in String#start_with? logic.
-rw-r--r--mrbgems/mruby-string-ext/src/string.c11
1 files changed, 7 insertions, 4 deletions
diff --git a/mrbgems/mruby-string-ext/src/string.c b/mrbgems/mruby-string-ext/src/string.c
index edebcecbc..9763794f4 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)
@@ -119,10 +120,12 @@ mrb_str_start_with(mrb_state *mrb, mrb_value self)
size_t len_l, len_r, len_cmp;
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) {
+ 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();
+ }
+ }
}
return mrb_false_value();
}