From 2dea5206f4a9cd86869566eef3266ec7793a0ab3 Mon Sep 17 00:00:00 2001 From: Thomas Marshall Date: Tue, 5 Mar 2024 17:27:45 +0000 Subject: [PATCH] Support null encoding for rb_enc_interned_str_cstr This commit adds support for a null pointer to be passed as the encoding argument to rb_enc_interned_str_cstr. When we implemented this function originally, we noticed that the CRuby behavior did not match the header documentation, so we opted not to support it. However, this has now been fixed in CRuby [1], so we can fix it here as well. [1]: https://github.com/ruby/ruby/pull/10169 --- CHANGELOG.md | 2 ++ lib/cext/ABI_check.txt | 2 +- spec/ruby/optional/capi/ext/string_spec.c | 2 +- spec/ruby/optional/capi/string_spec.rb | 8 ++++++++ src/main/c/cext/string.c | 2 +- 5 files changed, 13 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 14798aa3a12d..9aed84476e80 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,8 @@ Bug fixes: Compatibility: +* Allow null encoding pointer in `rb_enc_interned_str_cstr` (@thomasmarshall). + Performance: Changes: diff --git a/lib/cext/ABI_check.txt b/lib/cext/ABI_check.txt index b1bd38b62a08..8351c19397f4 100644 --- a/lib/cext/ABI_check.txt +++ b/lib/cext/ABI_check.txt @@ -1 +1 @@ -13 +14 diff --git a/spec/ruby/optional/capi/ext/string_spec.c b/spec/ruby/optional/capi/ext/string_spec.c index 070a88759ba2..cec3f65f458b 100644 --- a/spec/ruby/optional/capi/ext/string_spec.c +++ b/spec/ruby/optional/capi/ext/string_spec.c @@ -573,7 +573,7 @@ static VALUE string_spec_rb_str_unlocktmp(VALUE self, VALUE str) { } static VALUE string_spec_rb_enc_interned_str_cstr(VALUE self, VALUE str, VALUE enc) { - rb_encoding *e = rb_to_encoding(enc); + rb_encoding *e = NIL_P(enc) ? 0 : rb_to_encoding(enc); return rb_enc_interned_str_cstr(RSTRING_PTR(str), e); } diff --git a/spec/ruby/optional/capi/string_spec.rb b/spec/ruby/optional/capi/string_spec.rb index d9c20cf1760f..378bf7323f1d 100644 --- a/spec/ruby/optional/capi/string_spec.rb +++ b/spec/ruby/optional/capi/string_spec.rb @@ -1236,6 +1236,14 @@ def inspect it "returns the same string as String#-@" do @s.rb_enc_interned_str_cstr("hello", Encoding::UTF_8).should.equal?(-"hello") end + + ruby_bug "#20322", ""..."3.4" do + it "uses the default encoding if encoding is null" do + str = "hello" + val = @s.rb_enc_interned_str_cstr(str, nil) + val.encoding.should == Encoding::ASCII_8BIT + end + end end describe "rb_str_to_interned_str" do diff --git a/src/main/c/cext/string.c b/src/main/c/cext/string.c index ed1f212bc4d7..8d1a966f86db 100644 --- a/src/main/c/cext/string.c +++ b/src/main/c/cext/string.c @@ -439,7 +439,7 @@ long rb_str_coderange_scan_restartable(const char *s, const char *e, rb_encoding } VALUE rb_enc_interned_str_cstr(const char *ptr, rb_encoding *enc) { - VALUE str = rb_enc_str_new_cstr(ptr, enc); + VALUE str = rb_enc_str_new_cstr(ptr, enc ? enc : rb_ascii8bit_encoding()); return rb_str_to_interned_str(str); }