summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorUfuk Kayserilioglu <ufuk.kayserilioglu@shopify.com>2021-10-25 15:48:18 +0300
committerUfuk Kayserilioglu <ufuk.kayserilioglu@shopify.com>2021-10-25 16:01:34 +0300
commitcd8bbe56a3d0a3eca1419ae850088ce204f12ee5 (patch)
treea86da5744c231f4801248b9d20fb261b308f96f4
parente3ed3ebe2f86a7b2cbd122810a7b4be9fcde8053 (diff)
downloadjson-cd8bbe56a3d0a3eca1419ae850088ce204f12ee5.tar.gz
Call `super` in `included` hook
The C extension defines an `included` hook for the `JSON::Ext::Generator::GeneratorMethods::String` module but neglects to call `super` in the hook. This can break the functionality of various other code that rely on the fact that `included` on `Module` will always be called.
-rw-r--r--ext/json/ext/generator/generator.c1
-rw-r--r--tests/json_generator_test.rb23
2 files changed, 24 insertions, 0 deletions
diff --git a/ext/json/ext/generator/generator.c b/ext/json/ext/generator/generator.c
index e3a8347..711b7b8 100644
--- a/ext/json/ext/generator/generator.c
+++ b/ext/json/ext/generator/generator.c
@@ -478,6 +478,7 @@ static VALUE mFloat_to_json(int argc, VALUE *argv, VALUE self)
*/
static VALUE mString_included_s(VALUE self, VALUE modul) {
VALUE result = rb_funcall(modul, i_extend, 1, mString_Extend);
+ rb_call_super(1, &modul);
return result;
}
diff --git a/tests/json_generator_test.rb b/tests/json_generator_test.rb
index f31b6b2..b0f9a01 100644
--- a/tests/json_generator_test.rb
+++ b/tests/json_generator_test.rb
@@ -391,6 +391,29 @@ EOT
end
end
+ if defined?(JSON::Ext::Generator)
+ def test_string_ext_included_calls_super
+ included = false
+
+ Module.alias_method(:included_orig, :included)
+ Module.define_method(:included) do |base|
+ included_orig(base)
+ included = true
+ end
+
+ Class.new(String) do
+ include JSON::Ext::Generator::GeneratorMethods::String
+ end
+
+ assert included
+ ensure
+ if Module.private_method_defined?(:included_orig)
+ Module.alias_method(:included, :included_orig)
+ Module.remove_method(:included_orig)
+ end
+ end
+ end
+
if defined?(Encoding)
def test_nonutf8_encoding
assert_equal("\"5\u{b0}\"", "5\xb0".force_encoding("iso-8859-1").to_json)