From 7ae67e8f6ad6e7fd0677b28a7a10961f79d55495 Mon Sep 17 00:00:00 2001 From: Yusuke Endoh Date: Thu, 2 Oct 2025 19:19:51 +0900 Subject: [PATCH] load.c: Fix dest and src of MEMMOVE When multiple files with the same name are required, the features_index hash stores the indexes in `$LOADED_FEATURES` array into a darray. The dest and src arguments for `MEMMOVE` were wrongly reversed when inserting a new index in the darray. [Bug #21568] --- load.c | 2 +- test/ruby/test_require.rb | 14 ++++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/load.c b/load.c index a0c45561d7eca4..c63aa2abbbf087 100644 --- a/load.c +++ b/load.c @@ -268,7 +268,7 @@ features_index_add_single_callback(st_data_t *key, st_data_t *value, st_data_t r if (pos >= 0) { long *ptr = rb_darray_data_ptr(feature_indexes); long len = rb_darray_size(feature_indexes); - MEMMOVE(ptr + pos, ptr + pos + 1, long, len - pos - 1); + MEMMOVE(ptr + pos + 1, ptr + pos, long, len - pos - 1); ptr[pos] = FIX2LONG(offset); } } diff --git a/test/ruby/test_require.rb b/test/ruby/test_require.rb index 4f6c62dc35c174..49afcceb331e57 100644 --- a/test/ruby/test_require.rb +++ b/test/ruby/test_require.rb @@ -1035,4 +1035,18 @@ class Object end RUBY end + + def test_bug_21568 + load_path = $LOAD_PATH.dup + loaded_featrures = $LOADED_FEATURES.dup + + $LOAD_PATH.clear + $LOADED_FEATURES.replace(["foo.so", "a/foo.rb", "b/foo.rb"]) + + assert_nothing_raised(LoadError) { require "foo" } + + ensure + $LOAD_PATH.replace(load_path) if load_path + $LOADED_FEATURES.replace loaded_featrures + end end