[Ruby 1.9-Bug#4061][Open] char class may cause memory leak

Bug #4061: char class may cause memory leak
http://redmine.ruby-lang.org/issues/show/4061

起票者: Yusuke E.
ステータス: Open, 優先度: Normal
担当者: Yusuke E.
ruby -v: ruby 1.9.3dev (2010-11-15 trunk 29800) [i686-linux]

遠藤です。

以下のようにするとメモリリークによってメモリ消費量がじわじわと増えていきます。
Coverity Scan が見つけてくれました。

a = (0…8000).map {|n| (n * 4 ).chr(“UTF-8”) }
b = (0…8000).map {|n| (n * 4 + 2).chr(“UTF-8”) }
loop do
begin
/[[#{ a }]||[#{ b }]]/
rescue RegexpError
end
end

diff --git a/regparse.c b/regparse.c
index 2caab90…bf40603 100644
— a/regparse.c
+++ b/regparse.c
@@ -2006,7 +2006,7 @@ and_cclass(CClassNode* dest, CClassNode* cc,
ScanEnv* env)
{
OnigEncoding enc = env->enc;
int r, not1, not2;

  • BBuf *buf1, *buf2, *pbuf;
  • BBuf *buf1, *buf2, *pbuf = 0;
    BitSetRef bsr1, bsr2;
    BitSet bs1, bs2;

@@ -2041,17 +2041,16 @@ and_cclass(CClassNode* dest, CClassNode* cc,
ScanEnv* env)
else {
r = and_code_range_buf(buf1, not1, buf2, not2, &pbuf, env);
if (r == 0 && not1 != 0) {

  • BBuf *tbuf;
  • BBuf *tbuf = 0;
    r = not_code_range_buf(enc, pbuf, &tbuf, env);
  • if (r != 0) {
  • bbuf_free(pbuf);
  • return r;
  • }
    bbuf_free(pbuf);
    pbuf = tbuf;
    }
    }
  • if (r != 0) return r;
  • if (r != 0) {

  • bbuf_free(pbuf);

  • return r;

  • }

    dest->mbuf = pbuf;
    bbuf_free(buf1);
    @@ -2065,7 +2064,7 @@ or_cclass(CClassNode* dest, CClassNode* cc,
    ScanEnv* env)
    {
    OnigEncoding enc = env->enc;
    int r, not1, not2;

  • BBuf *buf1, *buf2, *pbuf;
  • BBuf *buf1, *buf2, *pbuf = 0;
    BitSetRef bsr1, bsr2;
    BitSet bs1, bs2;

@@ -2100,17 +2099,16 @@ or_cclass(CClassNode* dest, CClassNode* cc,
ScanEnv* env)
else {
r = or_code_range_buf(enc, buf1, not1, buf2, not2, &pbuf, env);
if (r == 0 && not1 != 0) {

  • BBuf *tbuf;
  • BBuf *tbuf = 0;
    r = not_code_range_buf(enc, pbuf, &tbuf, env);
  • if (r != 0) {
  • bbuf_free(pbuf);
  • return r;
  • }
    bbuf_free(pbuf);
    pbuf = tbuf;
    }
    }
  • if (r != 0) return r;
  • if (r != 0) {

  • bbuf_free(pbuf);

  • return r;

  • }

    dest->mbuf = pbuf;
    bbuf_free(buf1);


Yusuke E. [email protected]

$B$^$D$b$H(B $B$f$-$R$m$G$9(B

In message “Re: [ruby-dev:42579] [Ruby 1.9-Bug#4061][Open] char class
may cause memory leak”
on Tue, 16 Nov 2010 00:20:37 +0900, Yusuke E.
[email protected] writes:

|$B0J2<$N$h$&$K$9$k$H%a%b%j%j!<%/$K$h$C$F%a%b%j>CHqNL$,$8$o$8$o$HA}$($F$$$-$^$9!#(B
|Coverity Scan $B$,8+$D$1$F$/$l$^$7$?!#(B

$B%D!<%k$C$FAG@2$i$7$$!#%3%_%C%H$7$F$/$@$5$$$^$;$s$+(B?

チケット #4061 が更新されました。 (by Yusuke E.)

ステータス OpenからClosedに変更
進捗 % 0から100に変更

This issue was solved with changeset r29803.
Yusuke, thank you for reporting this issue.
Your contribution to Ruby is greatly appreciated.
May Ruby be with you.