summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTakashi Kokubun <takashikkbn@gmail.com>2024-05-29 11:07:07 -0700
committerTakashi Kokubun <takashikkbn@gmail.com>2024-05-29 11:07:07 -0700
commit548c7cb9f517dcb8029bd9698187c81819e08edd (patch)
tree45ed193d5d7f36bf0fb66b9dbc919dd800122f79
parent8f1084db9b07cb74f99de70d6f8bb6076d27d8aa (diff)
merge revision(s) 7e4b1f8e1935a10df3c41ee60ca0987d73281126: [Backport #20322]
[Bug #20322] Fix rb_enc_interned_str_cstr null encoding The documentation for `rb_enc_interned_str_cstr` notes that `enc` can be a null pointer, but this currently causes a segmentation fault when trying to autoload the encoding. This commit fixes the issue by checking for NULL before calling `rb_enc_autoload`.
-rw-r--r--ext/-test-/string/fstring.c4
-rw-r--r--string.c2
-rw-r--r--test/-ext-/string/test_fstring.rb8
-rw-r--r--version.h2
4 files changed, 12 insertions, 4 deletions
diff --git a/ext/-test-/string/fstring.c b/ext/-test-/string/fstring.c
index 7ee14a8570..8dfa36e345 100644
--- a/ext/-test-/string/fstring.c
+++ b/ext/-test-/string/fstring.c
@@ -21,13 +21,13 @@ bug_s_fstring_fake_str(VALUE self)
VALUE
bug_s_rb_enc_interned_str(VALUE self, VALUE encoding)
{
- return rb_enc_interned_str("foo", 3, RDATA(encoding)->data);
+ return rb_enc_interned_str("foo", 3, NIL_P(encoding) ? NULL : RDATA(encoding)->data);
}
VALUE
bug_s_rb_enc_str_new(VALUE self, VALUE encoding)
{
- return rb_enc_str_new("foo", 3, RDATA(encoding)->data);
+ return rb_enc_str_new("foo", 3, NIL_P(encoding) ? NULL : RDATA(encoding)->data);
}
void
diff --git a/string.c b/string.c
index cae234687d..3d4650ff05 100644
--- a/string.c
+++ b/string.c
@@ -12092,7 +12092,7 @@ rb_interned_str_cstr(const char *ptr)
VALUE
rb_enc_interned_str(const char *ptr, long len, rb_encoding *enc)
{
- if (UNLIKELY(rb_enc_autoload_p(enc))) {
+ if (enc != NULL && UNLIKELY(rb_enc_autoload_p(enc))) {
rb_enc_autoload(enc);
}
diff --git a/test/-ext-/string/test_fstring.rb b/test/-ext-/string/test_fstring.rb
index b7416ccbf3..962e0a0a6f 100644
--- a/test/-ext-/string/test_fstring.rb
+++ b/test/-ext-/string/test_fstring.rb
@@ -20,6 +20,10 @@ class Test_String_Fstring < Test::Unit::TestCase
RUBY
end
+ def test_rb_enc_interned_str_null_encoding
+ assert_equal Encoding::ASCII_8BIT, Bug::String.rb_enc_interned_str(nil).encoding
+ end
+
def test_rb_enc_str_new_autoloaded_encoding
assert_separately([], <<~RUBY)
require '-test-/string'
@@ -28,6 +32,10 @@ class Test_String_Fstring < Test::Unit::TestCase
RUBY
end
+ def test_rb_enc_str_new_null_encoding
+ assert_equal Encoding::ASCII_8BIT, Bug::String.rb_enc_str_new(nil).encoding
+ end
+
def test_instance_variable
str = __method__.to_s * 3
str.instance_variable_set(:@test, 42)
diff --git a/version.h b/version.h
index a7495506e9..e0ec87da86 100644
--- a/version.h
+++ b/version.h
@@ -11,7 +11,7 @@
# define RUBY_VERSION_MINOR RUBY_API_VERSION_MINOR
#define RUBY_VERSION_TEENY 1
#define RUBY_RELEASE_DATE RUBY_RELEASE_YEAR_STR"-"RUBY_RELEASE_MONTH_STR"-"RUBY_RELEASE_DAY_STR
-#define RUBY_PATCHLEVEL 63
+#define RUBY_PATCHLEVEL 64
#include "ruby/version.h"
#include "ruby/internal/abi.h"