Eric Wong <normalperson / yhbt.net> wrote:
> Returning invalid strings is the main problem, I think;
> and we should stop doing that.  Dir.entries and similar methods
> have the same problem.

Maybe this, too:

Subject: [PATCH] string.c (rb_external_str_with_enc): fall back to ASCII-8BIT

Fall back to returning ASCII-8BIT instead of returning invalid
strings for things like Dir.entries.
---
 string.c                   | 4 ++++
 test/ruby/test_dir_m17n.rb | 3 ++-
 2 files changed, 6 insertions(+), 1 deletion(-)

diff --git a/string.c b/string.c
index e4a02eb..e390dfc 100644
--- a/string.c
+++ b/string.c
@@ -958,6 +958,10 @@ rb_external_str_with_enc(VALUE str, rb_encoding *eenc)
 	return str;
     }
     rb_enc_associate(str, eenc);
+    if (rb_enc_str_coderange(str) == ENC_CODERANGE_BROKEN) {
+	rb_enc_associate(str, rb_ascii8bit_encoding());
+	return str;
+    }
     return rb_str_conv_enc(str, eenc, rb_default_internal_encoding());
 }
 
diff --git a/test/ruby/test_dir_m17n.rb b/test/ruby/test_dir_m17n.rb
index febfbc0..db5ac58 100644
--- a/test/ruby/test_dir_m17n.rb
+++ b/test/ruby/test_dir_m17n.rb
@@ -72,7 +72,8 @@ def test_filename_extutf8_invalid
         opts = {:encoding => Encoding.default_external} if /mswin|mingw/ =~ RUBY_PLATFORM
         ents = Dir.entries(".", opts)
         filename = "%FF" if /darwin/ =~ RUBY_PLATFORM && ents.include?("%FF")
-        assert_include(ents, filename)
+        assert_include(ents, filename.b)
+        ents.each { |f| assert_predicate f, :valid_encoding? }
       EOS
     }
   end unless /mswin|mingw/ =~ RUBY_PLATFORM
-- 
http://80x24.org/spew/20160207232116.15467-1-e%4080x24.org/raw

Unsubscribe: <mailto:ruby-core-request / ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-core>