Skip to content

Commit 5acdbee

Browse files
committed
merge revision(s) r46778: [Backport ruby#10019]
* pack.c (encodes): fix buffer overrun by tail_lf. Thanks to Mamoru Tasaka and Tomas Hoger. [ruby-core:63604] [Bug ruby#10019] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/branches/ruby_2_1@46806 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
1 parent ce99468 commit 5acdbee

File tree

4 files changed

+19
-4
lines changed

4 files changed

+19
-4
lines changed

ChangeLog

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
Sun Jul 13 22:52:43 2014 Nobuyoshi Nakada <[email protected]>
2+
3+
* pack.c (encodes): fix buffer overrun by tail_lf. Thanks to
4+
Mamoru Tasaka and Tomas Hoger. [ruby-core:63604] [Bug #10019]
5+
16
Sun Jul 13 22:44:05 2014 Nobuyoshi Nakada <[email protected]>
27

38
* ext/thread/thread.c (undumpable): ConditionVariable and Queue

pack.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -946,7 +946,8 @@ static const char b64_table[] =
946946
static void
947947
encodes(VALUE str, const char *s, long len, int type, int tail_lf)
948948
{
949-
char buff[4096];
949+
enum {buff_size = 4096, encoded_unit = 4};
950+
char buff[buff_size + 1]; /* +1 for tail_lf */
950951
long i = 0;
951952
const char *trans = type == 'u' ? uu_table : b64_table;
952953
char padding;
@@ -959,15 +960,15 @@ encodes(VALUE str, const char *s, long len, int type, int tail_lf)
959960
padding = '=';
960961
}
961962
while (len >= 3) {
962-
while (len >= 3 && sizeof(buff)-i >= 4) {
963+
while (len >= 3 && buff_size-i >= encoded_unit) {
963964
buff[i++] = trans[077 & (*s >> 2)];
964965
buff[i++] = trans[077 & (((*s << 4) & 060) | ((s[1] >> 4) & 017))];
965966
buff[i++] = trans[077 & (((s[1] << 2) & 074) | ((s[2] >> 6) & 03))];
966967
buff[i++] = trans[077 & s[2]];
967968
s += 3;
968969
len -= 3;
969970
}
970-
if (sizeof(buff)-i < 4) {
971+
if (buff_size-i < encoded_unit) {
971972
rb_str_buf_cat(str, buff, i);
972973
i = 0;
973974
}
@@ -987,6 +988,7 @@ encodes(VALUE str, const char *s, long len, int type, int tail_lf)
987988
}
988989
if (tail_lf) buff[i++] = '\n';
989990
rb_str_buf_cat(str, buff, i);
991+
if ((size_t)i > sizeof(buff)) rb_bug("encodes() buffer overrun");
990992
}
991993

992994
static const char hex_table[] = "0123456789ABCDEF";

test/ruby/test_pack.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -550,6 +550,14 @@ def test_pack_unpack_m
550550
assert_equal(["\0"], "AA\n".unpack("m"))
551551
assert_equal(["\0"], "AA=\n".unpack("m"))
552552
assert_equal(["\0\0"], "AAA\n".unpack("m"))
553+
554+
bug10019 = '[ruby-core:63604] [Bug #10019]'
555+
size = ((4096-4)/4*3+1)
556+
assert_separately(%W[- #{size} #{bug10019}], <<-'end;')
557+
size = ARGV.shift.to_i
558+
bug = ARGV.shift
559+
assert_equal(size, ["a"*size].pack("m#{size+2}").unpack("m")[0].size, bug)
560+
end;
553561
end
554562

555563
def test_pack_unpack_m0

version.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#define RUBY_VERSION "2.1.2"
22
#define RUBY_RELEASE_DATE "2014-07-13"
3-
#define RUBY_PATCHLEVEL 170
3+
#define RUBY_PATCHLEVEL 171
44

55
#define RUBY_RELEASE_YEAR 2014
66
#define RUBY_RELEASE_MONTH 7

0 commit comments

Comments
 (0)