Skip to content

Commit

Permalink
Fixed Encoding::CompatibilityError under certain condition.
Browse files Browse the repository at this point in the history
`MessagePack.pack(obj, out)` or `obj.to_msgpack(out)` method raised
'incompatible character encodings: ASCII-8BIT and UTF-8' error if `out`
is a string and `obj` includes a string with non-ASCII-8BIT encoding and
longer than write_reference_threshold (5KB by defualt).
  • Loading branch information
frsyuki committed Feb 4, 2015
1 parent 5d3a881 commit db58d34
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions ext/msgpack/buffer.c
Expand Up @@ -332,7 +332,19 @@ void _msgpack_buffer_append_long_string(msgpack_buffer_t* b, VALUE string)

if(b->io != Qnil) {
msgpack_buffer_flush(b);
#ifdef COMPAT_HAVE_ENCODING
if (ENCODING_GET(string) == s_enc_ascii8bit) {
rb_funcall(b->io, b->io_write_all_method, 1, string);
} else if(!STR_DUP_LIKELY_DOES_COPY(string)) {
VALUE s = rb_str_dup(string);
ENCODING_SET(s, s_enc_ascii8bit);
rb_funcall(b->io, b->io_write_all_method, 1, s);
} else {
msgpack_buffer_append(b, RSTRING_PTR(string), length);
}
#else
rb_funcall(b->io, b->io_write_all_method, 1, string);
#endif

} else if(!STR_DUP_LIKELY_DOES_COPY(string)) {
_msgpack_buffer_append_reference(b, string);
Expand Down

0 comments on commit db58d34

Please sign in to comment.