summaryrefslogtreecommitdiff
path: root/string.c
diff options
context:
space:
mode:
authornobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2013-09-24 08:39:01 +0000
committernobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2013-09-24 08:39:01 +0000
commit3788742bc972f25939f70a76186b55f4b469562d (patch)
tree86f9d325e3b0091f52d1d65a6b962b38f42a8440 /string.c
parentd8441fcc6edec94921a827980035091db875979d (diff)
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
Diffstat (limited to 'string.c')
-rw-r--r--string.c12
1 files changed, 8 insertions, 4 deletions
diff --git a/string.c b/string.c
index 1b64425977..92f15dd294 100644
--- a/string.c
+++ b/string.c
@@ -4735,23 +4735,27 @@ rb_str_inspect(VALUE str)
p = RSTRING_PTR(str); pend = RSTRING_END(str);
prev = p;
- if (encidx == ENCINDEX_UTF_16) {
+ if (encidx == ENCINDEX_UTF_16 && p + 2 <= pend) {
const unsigned char *q = (const unsigned char *)p;
if (q[0] == 0xFE && q[1] == 0xFF)
enc = rb_enc_from_index(ENCINDEX_UTF_16BE);
else if (q[0] == 0xFF && q[1] == 0xFE)
enc = rb_enc_from_index(ENCINDEX_UTF_16LE);
- else
+ else {
+ enc = rb_ascii8bit_encoding();
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;
if (q[0] == 0 && q[1] == 0 && q[2] == 0xFE && q[3] == 0xFF)
enc = rb_enc_from_index(ENCINDEX_UTF_32BE);
else if (q[3] == 0 && q[2] == 0 && q[1] == 0xFE && q[0] == 0xFF)
enc = rb_enc_from_index(ENCINDEX_UTF_32LE);
- else
+ else {
+ enc = rb_ascii8bit_encoding();
unicode_p = 0;
+ }
}
while (p < pend) {
unsigned int c, cc;