string.c: fix for UTF-16/32

* string.c (rb_str_inspect): get rid of out-of-bound access.
* string.c (rb_str_inspect): when a UTF-16/32 string doesn't have a
  BOM, inspect as a dummy encoding string.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@43035 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
nobu 2013-09-24 08:39:01 +00:00
parent d8441fcc6e
commit 3788742bc9
2 changed files with 15 additions and 4 deletions

View File

@ -1,3 +1,10 @@
Tue Sep 24 17:38:56 2013 Nobuyoshi Nakada <nobu@ruby-lang.org>
* string.c (rb_str_inspect): get rid of out-of-bound access.
* string.c (rb_str_inspect): when a UTF-16/32 string doesn't have a
BOM, inspect as a dummy encoding string.
Tue Sep 24 17:15:10 2013 Nobuyoshi Nakada <nobu@ruby-lang.org> Tue Sep 24 17:15:10 2013 Nobuyoshi Nakada <nobu@ruby-lang.org>
* enc/encdb.c (ENC_DUMMY_UNICODE): make BOM-encodings dummy. * enc/encdb.c (ENC_DUMMY_UNICODE): make BOM-encodings dummy.

View File

@ -4735,23 +4735,27 @@ rb_str_inspect(VALUE str)
p = RSTRING_PTR(str); pend = RSTRING_END(str); p = RSTRING_PTR(str); pend = RSTRING_END(str);
prev = p; prev = p;
if (encidx == ENCINDEX_UTF_16) { if (encidx == ENCINDEX_UTF_16 && p + 2 <= pend) {
const unsigned char *q = (const unsigned char *)p; const unsigned char *q = (const unsigned char *)p;
if (q[0] == 0xFE && q[1] == 0xFF) if (q[0] == 0xFE && q[1] == 0xFF)
enc = rb_enc_from_index(ENCINDEX_UTF_16BE); enc = rb_enc_from_index(ENCINDEX_UTF_16BE);
else if (q[0] == 0xFF && q[1] == 0xFE) else if (q[0] == 0xFF && q[1] == 0xFE)
enc = rb_enc_from_index(ENCINDEX_UTF_16LE); enc = rb_enc_from_index(ENCINDEX_UTF_16LE);
else else {
enc = rb_ascii8bit_encoding();
unicode_p = 0; unicode_p = 0;
}
} }
else if (encidx == ENCINDEX_UTF_32) { else if (encidx == ENCINDEX_UTF_32 && p + 4 <= pend) {
const unsigned char *q = (const unsigned char *)p; const unsigned char *q = (const unsigned char *)p;
if (q[0] == 0 && q[1] == 0 && q[2] == 0xFE && q[3] == 0xFF) if (q[0] == 0 && q[1] == 0 && q[2] == 0xFE && q[3] == 0xFF)
enc = rb_enc_from_index(ENCINDEX_UTF_32BE); enc = rb_enc_from_index(ENCINDEX_UTF_32BE);
else if (q[3] == 0 && q[2] == 0 && q[1] == 0xFE && q[0] == 0xFF) else if (q[3] == 0 && q[2] == 0 && q[1] == 0xFE && q[0] == 0xFF)
enc = rb_enc_from_index(ENCINDEX_UTF_32LE); enc = rb_enc_from_index(ENCINDEX_UTF_32LE);
else else {
enc = rb_ascii8bit_encoding();
unicode_p = 0; unicode_p = 0;
}
} }
while (p < pend) { while (p < pend) {
unsigned int c, cc; unsigned int c, cc;