Skip to content

Commit

Permalink
IO#set_encoding_by_bom should err when encoding is already set
Browse files Browse the repository at this point in the history
Except for ASCII-8BIT.  [Bug #16422]
  • Loading branch information
nobu committed Dec 15, 2019
1 parent eeb99fb commit e8c6283
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
4 changes: 4 additions & 0 deletions io.c
Expand Up @@ -8334,6 +8334,10 @@ rb_io_set_encoding_by_bom(VALUE io)
if (fptr->encs.enc2) {
rb_raise(rb_eArgError, "encoding conversion is set");
}
else if (fptr->encs.enc && fptr->encs.enc != rb_ascii8bit_encoding()) {
rb_raise(rb_eArgError, "encoding is set to %s already",
rb_enc_name(fptr->encs.enc));
}
if (!io_set_encoding_by_bom(io)) return Qnil;
return rb_enc_from_encoding(fptr->encs.enc);
}
Expand Down
10 changes: 10 additions & 0 deletions test/ruby/test_io_m17n.rb
Expand Up @@ -2102,6 +2102,9 @@ def test_w_xml_attr
File.open(path, "rb") {|f|
assert_equal(Encoding.find(name), f.set_encoding_by_bom)
}
File.open(path, "rb", encoding: "iso-8859-1") {|f|
assert_raise(ArgumentError) {f.set_encoding_by_bom}
}
}
end
end
Expand All @@ -2114,6 +2117,10 @@ def test_strip_bom_no_conv
bug3407 = '[ruby-core:30641]'
result = File.read(path, encoding: 'BOM|UTF-8')
assert_equal("a", result.b, bug3407)

File.open(path, "rb", encoding: "iso-8859-1") {|f|
assert_raise(ArgumentError) {f.set_encoding_by_bom}
}
}
end

Expand Down Expand Up @@ -2148,6 +2155,9 @@ def test_strip_bom_no_bom
File.open(path, "rb") {|f|
assert_nil(f.set_encoding_by_bom)
}
File.open(path, "rb", encoding: "iso-8859-1") {|f|
assert_raise(ArgumentError) {f.set_encoding_by_bom}
}
}
end

Expand Down

0 comments on commit e8c6283

Please sign in to comment.