From 2cf12ca386108a48f89271eff638fe6d39076a65 Mon Sep 17 00:00:00 2001 From: Hiroshi Shirosaki Date: Fri, 29 Mar 2013 16:03:22 +0900 Subject: [PATCH] load.c: fix require with non ascii path --- internal.h | 1 + load.c | 6 +++--- string.c | 13 +++++++++++-- test/ruby/test_require.rb | 20 ++++++++++++++++++++ 4 files changed, 35 insertions(+), 5 deletions(-) diff --git a/internal.h b/internal.h index fccbeb6..02d6a39 100644 --- a/internal.h +++ b/internal.h @@ -295,6 +295,7 @@ int rb_str_buf_cat_escaped_char(VALUE result, unsigned int c, int unicode_p); int rb_str_symname_p(VALUE); VALUE rb_str_quote_unprintable(VALUE); VALUE rb_id_quote_unprintable(ID); +VALUE rb_str_subseq_without_enc(VALUE, long, long); #define QUOTE(str) rb_str_quote_unprintable(str) #define QUOTE_ID(id) rb_id_quote_unprintable(id) diff --git a/load.c b/load.c index 7c69461..d04c475 100644 --- a/load.c +++ b/load.c @@ -247,16 +247,16 @@ features_index_add(VALUE feature, VALUE offset) if (p < feature_str) break; /* Now *p == '/'. We reach this point for every '/' in `feature`. */ - short_feature = rb_str_substr(feature, p + 1 - feature_str, feature_end - p - 1); + short_feature = rb_str_subseq_without_enc(feature, p + 1 - feature_str, feature_end - p - 1); features_index_add_single(short_feature, offset); if (ext) { - short_feature = rb_str_substr(feature, p + 1 - feature_str, ext - p - 1); + short_feature = rb_str_subseq_without_enc(feature, p + 1 - feature_str, ext - p - 1); features_index_add_single(short_feature, offset); } } features_index_add_single(feature, offset); if (ext) { - short_feature = rb_str_substr(feature, 0, ext - feature_str); + short_feature = rb_str_subseq_without_enc(feature, 0, ext - feature_str); features_index_add_single(short_feature, offset); } } diff --git a/string.c b/string.c index 8bbd8a4..924152e 100644 --- a/string.c +++ b/string.c @@ -1667,7 +1667,7 @@ rb_str_sublen(VALUE str, long pos) } VALUE -rb_str_subseq(VALUE str, long beg, long len) +rb_str_subseq_without_enc(VALUE str, long beg, long len) { VALUE str2; @@ -1681,12 +1681,21 @@ rb_str_subseq(VALUE str, long beg, long len) RB_GC_GUARD(str); } - rb_enc_cr_str_copy_for_substr(str2, str); OBJ_INFECT(str2, str); return str2; } +VALUE +rb_str_subseq(VALUE str, long beg, long len) +{ + VALUE str2 = rb_str_subseq_without_enc(str, beg, len); + + rb_enc_cr_str_copy_for_substr(str2, str); + + return str2; +} + static char * rb_str_subpos(VALUE str, long beg, long *lenp) { diff --git a/test/ruby/test_require.rb b/test/ruby/test_require.rb index cb17b91..56041c1 100644 --- a/test/ruby/test_require.rb +++ b/test/ruby/test_require.rb @@ -639,4 +639,24 @@ class TestRequire < Test::Unit::TestCase ensure script.close(true) if script end + + def test_require_with_non_ascii_path + bug8165 = '[ruby-core:53733] [Bug #8165]' + Dir.mktmpdir {|tmp| + Dir.chdir(tmp) { + dir = "\u3042" * 5 + Dir.mkdir(dir) + path = File.join(tmp, dir, 'foo.rb').force_encoding('UTF-8') + open(path, "w") {|f| + f.puts "p :ok" + } + assert_in_out_err([], <<-INPUT, %w(:ok), [], bug8165) + # coding: UTF-8 + $:.replace([IO::NULL]) + require '#{path}' + p :ng if require '#{path}' + INPUT + } + } + end end -- 1.7.10.4