summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authornobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2017-12-22 01:03:17 +0000
committernobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2017-12-22 01:03:17 +0000
commite2479cc43febe9cf26093fa2103a199d2d245711 (patch)
tree6616db836fa074425156cffc1a2612d64e314f2f
parentb4dc51ce1059ab33dffa3ca37739efafe0a56fff (diff)
downloadruby-e2479cc43febe9cf26093fa2103a199d2d245711.tar.gz
encoding.c: rb_enc_find_index2
* string.c (str_undump): use rb_enc_find_index2 to find encoding by unterminated string. check the format before encoding name. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@61396 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
-rw-r--r--encindex.h2
-rw-r--r--encoding.c11
-rw-r--r--string.c17
3 files changed, 17 insertions, 13 deletions
diff --git a/encindex.h b/encindex.h
index dcb1646f3a..658b60a9fd 100644
--- a/encindex.h
+++ b/encindex.h
@@ -57,6 +57,8 @@ enum ruby_preserved_encindex {
#define rb_utf8_encindex() RUBY_ENCINDEX_UTF_8
#define rb_usascii_encindex() RUBY_ENCINDEX_US_ASCII
+int rb_enc_find_index2(const char *name, long len);
+
#if defined(__cplusplus)
#if 0
{ /* satisfy cc-mode */
diff --git a/encoding.c b/encoding.c
index 68871db268..2043a69598 100644
--- a/encoding.c
+++ b/encoding.c
@@ -724,6 +724,17 @@ rb_enc_find_index(const char *name)
return i;
}
+int
+rb_enc_find_index2(const char *name, long len)
+{
+ char buf[ENCODING_NAMELEN_MAX+1];
+
+ if (len > ENCODING_NAMELEN_MAX) return -1;
+ memcpy(buf, name, len);
+ buf[len] = '\0';
+ return rb_enc_find_index(buf);
+}
+
rb_encoding *
rb_enc_find(const char *name)
{
diff --git a/string.c b/string.c
index e6db6a502c..30d57b5df4 100644
--- a/string.c
+++ b/string.c
@@ -6263,7 +6263,6 @@ str_undump(VALUE str)
}
else {
const char *encname;
- char *buf;
int encidx;
ptrdiff_t size;
@@ -6280,22 +6279,14 @@ str_undump(VALUE str)
s = memchr(s, '"', s_end-s);
size = s - encname;
if (!s) goto invalid_format;
- if (size > 100) {
- rb_raise(rb_eRuntimeError, "dumped string has unknown encoding name");
- }
- buf = ALLOC_N(char, size+1);
- memcpy(buf, encname, size);
- buf[size] = '\0';
- encidx = rb_enc_find_index(buf);
- xfree(buf);
+ if (s_end - s != 2) goto invalid_format;
+ if (s[0] != '"' || s[1] != ')') goto invalid_format;
+
+ encidx = rb_enc_find_index2(encname, (long)size);
if (encidx < 0) {
rb_raise(rb_eRuntimeError, "dumped string has unknown encoding name");
}
rb_enc_associate_index(undumped, encidx);
-
- if (s_end - s != 2 ||
- s[0] != '"' ||
- s[1] != ')') goto invalid_format;
}
break;
}