[前][次][番号順一覧][スレッド一覧]

ruby-changes:49279

From: nobu <ko1@a...>
Date: Fri, 22 Dec 2017 10:03:23 +0900 (JST)
Subject: [ruby-changes:49279] nobu:r61396 (trunk): encoding.c: rb_enc_find_index2

nobu	2017-12-22 10:03:17 +0900 (Fri, 22 Dec 2017)

  New Revision: 61396

  https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=61396

  Log:
    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.

  Modified files:
    trunk/encindex.h
    trunk/encoding.c
    trunk/string.c
Index: encoding.c
===================================================================
--- encoding.c	(revision 61395)
+++ encoding.c	(revision 61396)
@@ -724,6 +724,17 @@ rb_enc_find_index(const char *name) https://github.com/ruby/ruby/blob/trunk/encoding.c#L724
     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)
 {
Index: encindex.h
===================================================================
--- encindex.h	(revision 61395)
+++ encindex.h	(revision 61396)
@@ -57,6 +57,8 @@ enum ruby_preserved_encindex { https://github.com/ruby/ruby/blob/trunk/encindex.h#L57
 #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 */
Index: string.c
===================================================================
--- string.c	(revision 61395)
+++ string.c	(revision 61396)
@@ -6263,7 +6263,6 @@ str_undump(VALUE str) https://github.com/ruby/ruby/blob/trunk/string.c#L6263
 	    }
 	    else {
 		const char *encname;
-		char *buf;
 		int encidx;
 		ptrdiff_t size;
 
@@ -6280,22 +6279,14 @@ str_undump(VALUE str) https://github.com/ruby/ruby/blob/trunk/string.c#L6279
 		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;
 	}

--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]