From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on dcvr.yhbt.net X-Spam-Level: X-Spam-ASN: AS37560 197.231.220.0/22 X-Spam-Status: No, score=-3.3 required=3.0 tests=AWL,BAYES_00, RCVD_IN_MSPIKE_BL,RCVD_IN_MSPIKE_ZBI,RCVD_IN_XBL,SPF_FAIL,SPF_HELO_FAIL, TO_EQ_FM_DOM_SPF_FAIL shortcircuit=no autolearn=no autolearn_force=no version=3.4.0 Received: from 80x24.org (exit1.ipredator.se [197.231.221.211]) by dcvr.yhbt.net (Postfix) with ESMTP id 7AF7620D12 for ; Mon, 30 Jan 2017 21:53:13 +0000 (UTC) From: Eric Wong To: spew@80x24.org Subject: [PATCH] string.c (rb_str_tmp_frozen_release): release embedded strings Date: Mon, 30 Jan 2017 21:52:59 +0000 Message-Id: <20170130215259.29151-1-e@80x24.org> List-Id: Handle the embedded case first, since we may have an embedded duplicate and non-embedded original string. * string.c (rb_str_tmp_frozen_release): handled embedded strings * test/ruby/test_io.rb (test_write_no_garbage): new test [ruby-core:78898] [Bug #13085] --- string.c | 10 +++++----- test/ruby/test_io.rb | 16 ++++++++++++++++ 2 files changed, 21 insertions(+), 5 deletions(-) diff --git a/string.c b/string.c index a65a423b8f..3a7113dcc0 100644 --- a/string.c +++ b/string.c @@ -1150,7 +1150,11 @@ rb_str_tmp_frozen_release(VALUE orig, VALUE tmp) if (RBASIC_CLASS(tmp) != 0) return; - if (FL_TEST_RAW(orig, STR_SHARED) && + if (STR_EMBED_P(tmp)) { + assert(OBJ_FROZEN_RAW(tmp)); + rb_gc_force_recycle(tmp); + } + else if (FL_TEST_RAW(orig, STR_SHARED) && !FL_TEST_RAW(orig, STR_TMPLOCK|RUBY_FL_FREEZE)) { VALUE shared = RSTRING(orig)->as.heap.aux.shared; @@ -1164,10 +1168,6 @@ rb_str_tmp_frozen_release(VALUE orig, VALUE tmp) rb_gc_force_recycle(tmp); } } - else if (STR_EMBED_P(tmp)) { - assert(OBJ_FROZEN_RAW(tmp)); - rb_gc_force_recycle(tmp); - } } static VALUE diff --git a/test/ruby/test_io.rb b/test/ruby/test_io.rb index b146e8e321..c501edd0ab 100644 --- a/test/ruby/test_io.rb +++ b/test/ruby/test_io.rb @@ -3504,5 +3504,21 @@ def test_closed_stream_in_rescue end end end + + def test_write_no_garbage + res = {} + ObjectSpace.count_objects(res) # creates strings on first call + [ 'foo'.b, '*' * 24 ].each do |buf| + with_pipe do |r, w| + before = ObjectSpace.count_objects(res)[:T_STRING] + n = w.write(buf) + after = ObjectSpace.count_objects(res)[:T_STRING] + assert_equal before, after, + 'no strings left over after write [ruby-core:78898] [Bug #13085]' + assert_not_predicate buf, :frozen?, 'no inadvertant freeze' + assert_equal buf.bytesize, n, 'wrote expected size' + end + end + end end end -- EW