[前][次][番号順一覧][スレッド一覧]

ruby-changes:68312

From: Nobuyoshi <ko1@a...>
Date: Fri, 8 Oct 2021 15:04:09 +0900 (JST)
Subject: [ruby-changes:68312] 78ff9b719c (master): Add tests for the edge caces of `String#end_with?`

https://git.ruby-lang.org/ruby.git/commit/?id=78ff9b719c

From 78ff9b719c236b56956d446053256f8e30edf0c3 Mon Sep 17 00:00:00 2001
From: Nobuyoshi Nakada <nobu@r...>
Date: Fri, 8 Oct 2021 14:08:03 +0900
Subject: Add tests for the edge caces of `String#end_with?`

Also, check if a suffix is empty, to guarantee the assumption of
`onigenc_get_left_adjust_char_head` that `*s` is always accessible,
even in the case of `SHARABLE_MIDDLE_SUBSTRING`.
---
 string.c                 | 8 +++++---
 test/ruby/test_string.rb | 2 ++
 2 files changed, 7 insertions(+), 3 deletions(-)

diff --git a/string.c b/string.c
index 7890e6cc88..13079251d4 100644
--- a/string.c
+++ b/string.c
@@ -10237,12 +10237,14 @@ rb_str_end_with(int argc, VALUE *argv, VALUE str) https://github.com/ruby/ruby/blob/trunk/string.c#L10237
 
     for (i=0; i<argc; i++) {
 	VALUE tmp = argv[i];
+	long slen, tlen;
 	StringValue(tmp);
 	enc = rb_enc_check(str, tmp);
-	if (RSTRING_LEN(str) < RSTRING_LEN(tmp)) continue;
+	if ((tlen = RSTRING_LEN(tmp)) == 0) return Qtrue;
+	if ((slen = RSTRING_LEN(str)) < tlen) continue;
 	p = RSTRING_PTR(str);
-        e = p + RSTRING_LEN(str);
-	s = e - RSTRING_LEN(tmp);
+        e = p + slen;
+	s = e - tlen;
 	if (rb_enc_left_char_head(p, s, e, enc) != s)
 	    continue;
 	if (memcmp(s, RSTRING_PTR(tmp), RSTRING_LEN(tmp)) == 0)
diff --git a/test/ruby/test_string.rb b/test/ruby/test_string.rb
index 12e4b0fe2a..3f7c06e075 100644
--- a/test/ruby/test_string.rb
+++ b/test/ruby/test_string.rb
@@ -1180,6 +1180,8 @@ CODE https://github.com/ruby/ruby/blob/trunk/test/ruby/test_string.rb#L1180
     assert_send([S("hello"), :end_with?, S("llo")])
     assert_not_send([S("hello"), :end_with?, S("ll")])
     assert_send([S("hello"), :end_with?, S("el"), S("lo")])
+    assert_send([S("hello"), :end_with?, S("")])
+    assert_not_send([S("hello"), :end_with?])
 
     bug5536 = '[ruby-core:40623]'
     assert_raise(TypeError, bug5536) {S("str").end_with? :not_convertible_to_string}
-- 
cgit v1.2.1


--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]