ruby-changes:31723
From: naruse <ko1@a...>
Date: Fri, 22 Nov 2013 17:50:23 +0900 (JST)
Subject: [ruby-changes:31723] naruse:r43802 (trunk): * transcode.c (str_transcode0): don't scrub invalid chars if
naruse 2013-11-22 17:50:14 +0900 (Fri, 22 Nov 2013) New Revision: 43802 http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=43802 Log: * transcode.c (str_transcode0): don't scrub invalid chars if str.encode doesn't have explicit invalid: :replace. workaround fix for see #8995 Modified files: trunk/ChangeLog trunk/test/ruby/test_transcode.rb trunk/transcode.c Index: ChangeLog =================================================================== --- ChangeLog (revision 43801) +++ ChangeLog (revision 43802) @@ -1,3 +1,9 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1 +Fri Nov 22 17:20:50 2013 NARUSE, Yui <naruse@r...> + + * transcode.c (str_transcode0): don't scrub invalid chars if + str.encode doesn't have explicit invalid: :replace. + workaround fix for see #8995 + Fri Nov 22 17:11:26 2013 Narihiro Nakamura <authornari@g...> * include/ruby/intern.h, internal.h: Expose rb_gc_count(). Index: test/ruby/test_transcode.rb =================================================================== --- test/ruby/test_transcode.rb (revision 43801) +++ test/ruby/test_transcode.rb (revision 43802) @@ -2071,4 +2071,13 @@ class TestTranscode < Test::Unit::TestCa https://github.com/ruby/ruby/blob/trunk/test/ruby/test_transcode.rb#L2071 assert_equal(4, 'aaa'.encode(enc).length, "should count in #{enc} with BOM") end end + + def test_encode_with_invalid_chars + bug8995 = '[ruby-dev:47747]' + EnvUtil.with_default_internal(Encoding::UTF_8) do + str = "\xff".force_encoding('utf-8') + assert_equal str, str.encode, bug8995 + assert_equal "\ufffd", str.encode(invalid: :replace), bug8995 + end + end end Index: transcode.c =================================================================== --- transcode.c (revision 43801) +++ transcode.c (revision 43802) @@ -2672,6 +2672,7 @@ str_transcode0(int argc, VALUE *argv, VA https://github.com/ruby/ruby/blob/trunk/transcode.c#L2672 rb_encoding *senc, *denc; const char *sname, *dname; int dencidx; + int explicitly_invalid_replace = TRUE; rb_check_arity(argc, 0, 2); @@ -2681,6 +2682,9 @@ str_transcode0(int argc, VALUE *argv, VA https://github.com/ruby/ruby/blob/trunk/transcode.c#L2682 if (!ecflags) return -1; arg1 = rb_obj_encoding(str); } + if (!(ecflags & ECONV_INVALID_MASK)) { + explicitly_invalid_replace = FALSE; + } ecflags |= ECONV_INVALID_REPLACE | ECONV_UNDEF_REPLACE; } else { @@ -2694,7 +2698,7 @@ str_transcode0(int argc, VALUE *argv, VA https://github.com/ruby/ruby/blob/trunk/transcode.c#L2698 ECONV_XML_ATTR_CONTENT_DECORATOR| ECONV_XML_ATTR_QUOTE_DECORATOR)) == 0) { if (senc && senc == denc) { - if (ecflags & ECONV_INVALID_MASK) { + if ((ecflags & ECONV_INVALID_MASK) && explicitly_invalid_replace) { VALUE rep = Qnil; if (!NIL_P(ecopts)) { rep = rb_hash_aref(ecopts, sym_replace); -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/