1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

IO#set_encoding_by_bom should err when encoding is already set

Except for ASCII-8BIT.  [Bug #16422]
This commit is contained in:
Nobuyoshi Nakada 2019-12-15 23:13:16 +09:00
parent eeb99fb525
commit e8c62836a6
No known key found for this signature in database
GPG key ID: 4BC7D6DF58D8DF60
2 changed files with 14 additions and 0 deletions

4
io.c
View file

@ -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);
}

View file

@ -2102,6 +2102,9 @@ EOT
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
@ -2114,6 +2117,10 @@ EOT
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
@ -2148,6 +2155,9 @@ EOT
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