From 40c771fd45eb7b3fd5c024bae65990faa24ddf46 Mon Sep 17 00:00:00 2001 From: akr Date: Thu, 4 Sep 2008 15:06:34 +0000 Subject: * pack.c (encodes): make buff fixed length to avoid SEGV by ruby -e '["a"*10000000].pack("m1000000000")' git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@19139 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- pack.c | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) (limited to 'pack.c') diff --git a/pack.c b/pack.c index 8f26365d7c..1bc101dd99 100644 --- a/pack.c +++ b/pack.c @@ -1009,7 +1009,7 @@ static const char b64_table[] = 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 @@ encodes(VALUE str, const char *s, long len, int type) 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))]; -- cgit v1.2.3