From a0183b394a699224a6451deb9762b8806799aea7 Mon Sep 17 00:00:00 2001 From: Frederick John Milens III Date: Thu, 22 Aug 2013 18:55:07 -0500 Subject: Fix for string-length-related issue in String#end_with? logic. --- mrbgems/mruby-string-ext/src/string.c | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) (limited to 'mrbgems/mruby-string-ext/src/string.c') diff --git a/mrbgems/mruby-string-ext/src/string.c b/mrbgems/mruby-string-ext/src/string.c index edebcecbc..e2ba24d1b 100644 --- a/mrbgems/mruby-string-ext/src/string.c +++ b/mrbgems/mruby-string-ext/src/string.c @@ -144,12 +144,14 @@ mrb_str_end_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) + (len_l - len_cmp), - RSTRING_PTR(argv[i]) + (len_r - len_cmp), - 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) + (len_l - len_cmp), + RSTRING_PTR(argv[i]) + (len_r - len_cmp), + len_cmp) == 0) { + return mrb_true_value(); + } + } } return mrb_false_value(); } -- cgit v1.2.3 From d682be9d328227aff3597ddb40cb21ecb5816f1d Mon Sep 17 00:00:00 2001 From: Frederick John Milens III Date: Thu, 22 Aug 2013 19:23:53 -0500 Subject: Refactor of String#end_with? comparison logic. --- mrbgems/mruby-string-ext/src/string.c | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) (limited to 'mrbgems/mruby-string-ext/src/string.c') diff --git a/mrbgems/mruby-string-ext/src/string.c b/mrbgems/mruby-string-ext/src/string.c index e2ba24d1b..4f8f87949 100644 --- a/mrbgems/mruby-string-ext/src/string.c +++ b/mrbgems/mruby-string-ext/src/string.c @@ -141,14 +141,13 @@ 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]); if (len_l >= len_r) { - 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) { + if (memcmp(RSTRING_PTR(self) + (len_l - len_r), + RSTRING_PTR(argv[i]), + len_r) == 0) { return mrb_true_value(); } } -- cgit v1.2.3