ruby-changes:45138
From: usa <ko1@a...>
Date: Tue, 27 Dec 2016 19:11:56 +0900 (JST)
Subject: [ruby-changes:45138] usa:r57211 (ruby_2_2): merge revision(s) 56894: [Backport #12974]
usa 2016-12-27 19:11:49 +0900 (Tue, 27 Dec 2016) New Revision: 57211 https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=57211 Log: merge revision(s) 56894: [Backport #12974] marshal.c: fix infinite recursion * marshal.c (check_userdump_arg): marshal_dump should not return an instance of the same class, otherwise it causes infinite recursion. [ruby-core:78289] [Bug #12974] Modified directories: branches/ruby_2_2/ Modified files: branches/ruby_2_2/ChangeLog branches/ruby_2_2/marshal.c branches/ruby_2_2/test/ruby/test_marshal.rb branches/ruby_2_2/version.h Index: ruby_2_2/version.h =================================================================== --- ruby_2_2/version.h (revision 57210) +++ ruby_2_2/version.h (revision 57211) @@ -1,6 +1,6 @@ https://github.com/ruby/ruby/blob/trunk/ruby_2_2/version.h#L1 #define RUBY_VERSION "2.2.7" #define RUBY_RELEASE_DATE "2016-12-27" -#define RUBY_PATCHLEVEL 401 +#define RUBY_PATCHLEVEL 402 #define RUBY_RELEASE_YEAR 2016 #define RUBY_RELEASE_MONTH 12 Index: ruby_2_2/marshal.c =================================================================== --- ruby_2_2/marshal.c (revision 57210) +++ ruby_2_2/marshal.c (revision 57211) @@ -165,15 +165,33 @@ struct dump_call_arg { https://github.com/ruby/ruby/blob/trunk/ruby_2_2/marshal.c#L165 int limit; }; -static void -check_dump_arg(struct dump_arg *arg, const char *name) +static VALUE +check_dump_arg(VALUE ret, struct dump_arg *arg, const char *name) { if (!arg->symbols) { rb_raise(rb_eRuntimeError, "Marshal.dump reentered at %s", name); } + return ret; +} + +static VALUE +check_userdump_arg(VALUE obj, ID sym, int argc, const VALUE *argv, + struct dump_arg *arg, const char *name) +{ + VALUE ret = rb_funcallv(obj, sym, argc, argv); + VALUE klass = CLASS_OF(obj); + if (CLASS_OF(ret) == klass) { + rb_raise(rb_eRuntimeError, "%"PRIsVALUE"#%s returned same class instance", + klass, name); + } + return check_dump_arg(ret, arg, name); } -#define check_dump_arg(arg, sym) check_dump_arg(arg, name_##sym) + +#define dump_funcall(arg, obj, sym, argc, argv) \ + check_userdump_arg(obj, sym, argc, argv, arg, name_##sym) +#define dump_check_funcall(arg, obj, sym, argc, argv) \ + check_dump_arg(rb_check_funcall(obj, sym, argc, argv), arg, name_##sym) static void clear_dump_arg(struct dump_arg *arg); @@ -706,8 +724,7 @@ w_object(VALUE obj, struct dump_arg *arg https://github.com/ruby/ruby/blob/trunk/ruby_2_2/marshal.c#L724 if (rb_obj_respond_to(obj, s_mdump, TRUE)) { st_add_direct(arg->data, obj, arg->data->num_entries); - v = rb_funcall2(obj, s_mdump, 0, 0); - check_dump_arg(arg, s_mdump); + v = dump_funcall(arg, obj, s_mdump, 0, 0); w_class(TYPE_USRMARSHAL, obj, arg, FALSE); w_object(v, arg, limit); return; @@ -718,8 +735,7 @@ w_object(VALUE obj, struct dump_arg *arg https://github.com/ruby/ruby/blob/trunk/ruby_2_2/marshal.c#L735 VALUE encname2; v = INT2NUM(limit); - v = rb_funcall2(obj, s_dump, 1, &v); - check_dump_arg(arg, s_dump); + v = dump_funcall(arg, obj, s_dump, 1, &v); if (!RB_TYPE_P(v, T_STRING)) { rb_raise(rb_eTypeError, "_dump() must return string"); } @@ -901,8 +917,7 @@ w_object(VALUE obj, struct dump_arg *arg https://github.com/ruby/ruby/blob/trunk/ruby_2_2/marshal.c#L917 "no _dump_data is defined for class %s", rb_obj_classname(obj)); } - v = rb_funcall2(obj, s_dump_data, 0, 0); - check_dump_arg(arg, s_dump_data); + v = dump_funcall(arg, obj, s_dump_data, 0, 0); w_class(TYPE_DATA, obj, arg, TRUE); w_object(v, arg, limit); } @@ -1012,9 +1027,7 @@ marshal_dump(int argc, VALUE *argv) https://github.com/ruby/ruby/blob/trunk/ruby_2_2/marshal.c#L1027 io_needed(); } arg->dest = port; - if (rb_check_funcall(port, s_binmode, 0, 0) != Qundef) { - check_dump_arg(arg, s_binmode); - } + dump_check_funcall(arg, port, s_binmode, 0, 0); } else { port = arg->str; @@ -1047,15 +1060,17 @@ struct load_arg { https://github.com/ruby/ruby/blob/trunk/ruby_2_2/marshal.c#L1060 int infection; }; -static void -check_load_arg(struct load_arg *arg, const char *name) +static VALUE +check_load_arg(VALUE ret, struct load_arg *arg, const char *name) { if (!arg->symbols) { rb_raise(rb_eRuntimeError, "Marshal.load reentered at %s", name); } + return ret; } -#define check_load_arg(arg, sym) check_load_arg(arg, name_##sym) +#define load_funcall(arg, obj, sym, argc, argv) \ + check_load_arg(rb_funcallv(obj, sym, argc, argv), arg, name_##sym) static void clear_load_arg(struct load_arg *arg); @@ -1118,9 +1133,7 @@ r_byte1_buffered(struct load_arg *arg) https://github.com/ruby/ruby/blob/trunk/ruby_2_2/marshal.c#L1133 long readable = arg->readable < BUFSIZ ? arg->readable : BUFSIZ; VALUE str, n = LONG2NUM(readable); - str = rb_funcall2(arg->src, s_read, 1, &n); - - check_load_arg(arg, s_read); + str = load_funcall(arg, arg->src, s_read, 1, &n); if (NIL_P(str)) too_short(); StringValue(str); arg->infection |= (int)FL_TEST(str, MARSHAL_INFECTION); @@ -1150,8 +1163,7 @@ r_byte(struct load_arg *arg) https://github.com/ruby/ruby/blob/trunk/ruby_2_2/marshal.c#L1163 c = r_byte1_buffered(arg); } else { - VALUE v = rb_funcall2(arg->src, s_getbyte, 0, 0); - check_load_arg(arg, s_getbyte); + VALUE v = load_funcall(arg, arg->src, s_getbyte, 0, 0); if (NIL_P(v)) rb_eof_error(); c = (unsigned char)NUM2CHR(v); } @@ -1212,8 +1224,7 @@ r_bytes1(long len, struct load_arg *arg) https://github.com/ruby/ruby/blob/trunk/ruby_2_2/marshal.c#L1224 { VALUE str, n = LONG2NUM(len); - str = rb_funcall2(arg->src, s_read, 1, &n); - check_load_arg(arg, s_read); + str = load_funcall(arg, arg->src, s_read, 1, &n); if (NIL_P(str)) too_short(); StringValue(str); if (RSTRING_LEN(str) != len) too_short(); @@ -1241,9 +1252,7 @@ r_bytes1_buffered(long len, struct load_ https://github.com/ruby/ruby/blob/trunk/ruby_2_2/marshal.c#L1252 readable = readable < BUFSIZ ? readable : BUFSIZ; read_len = need_len > readable ? need_len : readable; n = LONG2NUM(read_len); - tmp = rb_funcall2(arg->src, s_read, 1, &n); - - check_load_arg(arg, s_read); + tmp = load_funcall(arg, arg->src, s_read, 1, &n); if (NIL_P(tmp)) too_short(); StringValue(tmp); @@ -1428,8 +1437,7 @@ static VALUE https://github.com/ruby/ruby/blob/trunk/ruby_2_2/marshal.c#L1437 r_post_proc(VALUE v, struct load_arg *arg) { if (arg->proc) { - v = rb_funcall(arg->proc, s_call, 1, v); - check_load_arg(arg, s_call); + v = load_funcall(arg, arg->proc, s_call, 1, &v); } return v; } @@ -1833,8 +1841,7 @@ r_object0(struct load_arg *arg, int *ivp https://github.com/ruby/ruby/blob/trunk/ruby_2_2/marshal.c#L1841 r_ivar(data, NULL, arg); *ivp = FALSE; } - v = rb_funcall2(klass, s_load, 1, &data); - check_load_arg(arg, s_load); + v = load_funcall(arg, klass, s_load, 1, &data); v = r_entry(v, arg); if (st_lookup(compat_allocator_tbl, (st_data_t)rb_get_alloc_func(klass), &d)) { marshal_compat_t *compat = (marshal_compat_t*)d; @@ -1861,8 +1868,7 @@ r_object0(struct load_arg *arg, int *ivp https://github.com/ruby/ruby/blob/trunk/ruby_2_2/marshal.c#L1868 } v = r_entry(v, arg); data = r_object(arg); - rb_funcall2(v, s_mload, 1, &data); - check_load_arg(arg, s_mload); + load_funcall(arg, v, s_mload, 1, &data); v = r_fixup_compat(v, arg); v = r_copy_ivar(v, data); v = r_post_proc(v, arg); @@ -1903,8 +1909,7 @@ r_object0(struct load_arg *arg, int *ivp https://github.com/ruby/ruby/blob/trunk/ruby_2_2/marshal.c#L1909 rb_class2name(klass)); } r = r_object0(arg, 0, extmod); - rb_funcall2(v, s_load_data, 1, &r); - check_load_arg(arg, s_load_data); + load_funcall(arg, v, s_load_data, 1, &r); v = r_leave(v, arg); } break; Property changes on: ruby_2_2/marshal.c ___________________________________________________________________ Added: svn:mergeinfo Merged /trunk:r56894 Merged /trunk/marshal.c:r48993,48997,48999,49007,49009,49014-49017,49034,49036,49041-49042,49055-49056,49058,49064,49071-49073,49078,49082,49088-49090,49095-49096,49099,49104-49105,49140,49148,49154,49157,49162,49168-49171,49175-49176,49178,49180,49182-49184,49192-49193,49201-49203,49221-49225,49234-49235,49244,49254,49259,49266,49306,49315,49321-49322,49334,49340,49376,49386-49387,49389-49390,49397,49402-49403,49405-49408,49411,49414,49419,49428,49451-49452,49463,49474,49477-49483,49485,49487,49489-49491,49493-49495,49499-49507,49517,49525-49528,49531-49533,49536-49537,49541-49543,49545,49575-49576,49579,49586,49618,49620,49634,49640,49658,49660-49664,49675,49684-49685,49687,49689,49693,49695,49702,49706,49710,49740,49750,49752-49753,49758,49776,49788,49790,49792,49797,49802,49813-49816,49842,49859,49861,49865,49867,49875,49898,49913,49922,49930,49957,49964,49999-50000,50036,50038,50057,50065,50071,50084,50088-50089,50104-50105,50111-50112,50141,50172-50173,50182-50183,50 185,50187,50202,50206,50218,50265,50292,50305,50316,50334,50337-50340,50342-50343,50356,50359,50366,50372-50373,50402,50404-50407,50409-50410,50412-50416,50421,50423,50430,50440,50444,50449,50509,50515,50541-50542,50637,50646,50671,50685,50717,50737,50793,50827,50829,50834,50850,50860,50887,50896,50902,50914-50915,50921,50972,50982-50983,50985,51006,51016,51037,51046,51061,51063,51066,51091,51116,51120,51190,51202-51204,51213,51242,51263-51264,51292,51344,51350,51353,51409-51410,51423,51425,51427,51439-51440,51445,51453,51464-51465,51470,51488-51489,51531-51532,51534,51569,51575,51583-51584,51594,51616-51617,51624,51626,51638,51649,51651,51655,51685,51705-51706,51708,51713,51722,51724,51766-51767,51805,51807-51808,51812,51816,51820-51821,51916,51930,51939,52016-52017,52019-52021,52026,52124,52128,52133,52140,52199,52211,52222,52227-52229,52244,52273,52277,52357,52391,52461,52476-52477,52536,52556-52557,52605,52668,52682-52684,52694,52745,52777,52795,52856,52868-52869,52883-5 2884,52892,52906,52928,53022-53023,53039,53046,53050,53064,53083,53092-53093,53153,53164,53167-53169,53198,53202,53214-53215,53227,53231,53244,53259,53298-53299,53318,53329,53346-53349,53381-53382,53391,53411,53424-53425,53435,53449,53455,53495,53511-53512,53517,53519,53521-53523,53526-53527,53533,53561,53609,53619-53624,53626-53627,53654,53677,53718,53784,53790,53810,53819,53822,53834,53844,53928,53943,54000,54002-54005,54010,54017-54018,54020-54022,54028,54034,54089,54097,54105,54108,54129-54132,54136,54138-54139,54144,54168,54172,54210,54231,54256-54258,54288,54304,54307,54423,54444,54465,54484,54489,54492,54494-54496,54499,54503,54510,54529,54542,54611,54681,54706,54716,54719-54721,54750,54755,54887-54889,54893,54896-54897,54934,54970,55008,55047,55054,55063,55074,55115,55123,55129-55130,55133,55136,55154,55163,55165,55175,55181-55182,55228,55232-55233,55380,55410,55427,55434,55509,55547,55554,55562-55563,55568,55573,55579,55581-55582,55602,55609,55613,55663,55705,55726- 55727,55729,55731,55797,55822,55984,56030,56035-56036,56041,56111,56117,56166,56224,56252,56254,56374,56421-56422,56469,56489,56559,56561,56582,56584-56585,56596,56625,56682,56684,56766-56767,56832,56894 Index: ruby_2_2/ChangeLog =================================================================== --- ruby_2_2/ChangeLog (revision 57210) +++ ruby_2_2/ChangeLog (revision 57211) @@ -1,3 +1,13 @@ https://github.com/ruby/ruby/blob/trunk/ruby_2_2/ChangeLog#L1 +Tue Dec 27 19:10:09 2016 Nobuyoshi Nakada <nobu@r...> + + * marshal.c (dump_funcall, dump_check_funcall, load_funcall): + function calls with reentrant check. always show names + corresponding to the called methods. + + * marshal.c (check_userdump_arg): marshal_dump should not return an + instance of the same class, otherwise it causes infinite recursion. + [Bug #12974] + Tue Dec 27 18:34:04 2016 Nobuyoshi Nakada <nobu@r...> * compile.c (setup_args): duplicate splatting array if more Index: ruby_2_2/test/ruby/test_marshal.rb =================================================================== --- ruby_2_2/test/ruby/test_marshal.rb (revision 57210) +++ ruby_2_2/test/ruby/test_marshal.rb (revision 57211) @@ -671,4 +671,16 @@ class TestMarshal < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/ruby_2_2/test/ruby/test_marshal.rb#L671 end RUBY end + + class Bug12974 + def marshal_dump + dup + end + end + + def test_marshal_dump_recursion + assert_raise_with_message(RuntimeError, /same class instance/) do + Marshal.dump(Bug12974.new) + end + end end Property changes on: ruby_2_2 ___________________________________________________________________ Modified: svn:mergeinfo Merged /trunk:r51939,56894 -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/