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

ruby-changes:7618

From: akr <ko1@a...>
Date: Fri, 5 Sep 2008 00:08:42 +0900 (JST)
Subject: [ruby-changes:7618] Ruby:r19139 (trunk): * pack.c (encodes): make buff fixed length to avoid SEGV by

akr	2008-09-05 00:06:34 +0900 (Fri, 05 Sep 2008)

  New Revision: 19139

  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=19139

  Log:
    * pack.c (encodes): make buff fixed length to avoid SEGV by
      ruby -e '["a"*10000000].pack("m1000000000")'

  Modified files:
    trunk/ChangeLog
    trunk/pack.c

Index: ChangeLog
===================================================================
--- ChangeLog	(revision 19138)
+++ ChangeLog	(revision 19139)
@@ -1,3 +1,8 @@
+Fri Sep  5 00:05:27 2008  Tanaka Akira  <akr@f...>
+
+	* pack.c (encodes): make buff fixed length to avoid SEGV by
+	  ruby -e '["a"*10000000].pack("m1000000000")'
+
 Thu Sep  4 23:47:05 2008  Yusuke Endoh  <mame@t...>
 
 	* ext/bigdecimal/bigdecimal.c (BigDecimal_mode): set exception mode
Index: pack.c
===================================================================
--- pack.c	(revision 19138)
+++ pack.c	(revision 19139)
@@ -1009,7 +1009,7 @@
 static void
 encodes(VALUE str, const char *s, long len, int type)
 {
-    char *buff = ALLOCA_N(char, len * 4 / 3 + 6);
+    char buff[4096];
     long i = 0;
     const char *trans = type == 'u' ? uu_table : b64_table;
     int padding;
@@ -1022,13 +1022,20 @@
 	padding = '=';
     }
     while (len >= 3) {
-	buff[i++] = trans[077 & (*s >> 2)];
-	buff[i++] = trans[077 & (((*s << 4) & 060) | ((s[1] >> 4) & 017))];
-	buff[i++] = trans[077 & (((s[1] << 2) & 074) | ((s[2] >> 6) & 03))];
-	buff[i++] = trans[077 & s[2]];
-	s += 3;
-	len -= 3;
+        while (len >= 3 && sizeof(buff)-i >= 4) {
+            buff[i++] = trans[077 & (*s >> 2)];
+            buff[i++] = trans[077 & (((*s << 4) & 060) | ((s[1] >> 4) & 017))];
+            buff[i++] = trans[077 & (((s[1] << 2) & 074) | ((s[2] >> 6) & 03))];
+            buff[i++] = trans[077 & s[2]];
+            s += 3;
+            len -= 3;
+        }
+        if (sizeof(buff)-i < 4) {
+            rb_str_buf_cat(str, buff, i);
+            i = 0;
+        }
     }
+
     if (len == 2) {
 	buff[i++] = trans[077 & (*s >> 2)];
 	buff[i++] = trans[077 & (((*s << 4) & 060) | ((s[1] >> 4) & 017))];

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

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