pg.ddx.io  pgsql-bugs@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Tom Lane <tgl@sss.pgh.pa.us>
To: Arthur O'Dwyer <arthur.j.odwyer@gmail.com>
Cc: pgsql-bugs@postgresql.org
Subject: Re: BUG #10976: Two memory leaks in regcomp cleanup
Date: Fri, 18 Jul 2014 13:08:30 -0400
Message-ID: <25138.1405703310@sss.pgh.pa.us> (raw)
In-Reply-To: <CADvuK0+Oja=i7cDrZaW7LBvijp==TWOXDD-=hwOrhkmOmMcY4w@mail.gmail.com>
References: <20140716193828.2503.81394@wrigleys.postgresql.org>
	<5864.1405616688@sss.pgh.pa.us>
	<CADvuK0+Oja=i7cDrZaW7LBvijp==TWOXDD-=hwOrhkmOmMcY4w@mail.gmail.com>
List-Unsubscribe: <mailto:majordomo@postgresql.org?body=unsub%20pgsql-bugs>

"Arthur O'Dwyer" <arthur.j.odwyer@gmail.com> writes:
> On Thu, Jul 17, 2014 at 10:04 AM, Tom Lane <tgl@sss.pgh.pa.us> wrote:
>> The problem with this proposal is that if there are subres in v->tree
>> that *are* in the treechain, we'll possibly try to free them twice
>> (if they're not marked INUSE), and definitely will be accessing
>> already-freed memory when cleanst looks at them.

> Hmm. I think you're right --- I *think* the subres in v->tree are
> INUSE by definition, so double-free isn't an issue, but cleanst will
> definitely be looking at them after they've been freed, which is still
> a bug. What if you just swap the order that freev() does cleanst() and
> freesubre() so that the cleanst() happens first?

No, the INUSE marking doesn't happen till pg_regcomp runs markst(), so
that would break cleanup of failures occurring before that.  There's
somewhat of a narrow window for this case, since v->tree doesn't get
set until parse() returns, but failures there certainly are possible.

After some reflection I decided that what we need is to teach freesubre
to stick things back into the treefree list if and only if treechain is
non-NULL.  This guarantees we can't corrupt the treechain list with a
premature free of a subre, and it preserves the existing not-broken
logic for cleaning up after an error occuring before we reach markst().
So it ends up being one line of code change, though I added a bunch of
commentary as well:

http://git.postgresql.org/gitweb/?p=postgresql.git;a=commitdiff;h=1567e659a877d35ab4b85dafff41b2845d...

			regards, tom lane


-- 
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs



view thread (5+ messages)  latest in thread

Message-ID: <25138.1405703310@sss.pgh.pa.us>
Permalink:  ../25138.1405703310@sss.pgh.pa.us/
Also on:    postgresql.org/message-id/25138.1405703310@sss.pgh.pa.us

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-bugs@postgresql.org
  Cc: tgl@sss.pgh.pa.us, arthur.j.odwyer@gmail.com
  Subject: Re: BUG #10976: Two memory leaks in regcomp cleanup
  In-Reply-To: <25138.1405703310@sss.pgh.pa.us>
  References: <20140716193828.2503.81394@wrigleys.postgresql.org> <5864.1405616688@sss.pgh.pa.us> <CADvuK0+Oja=i7cDrZaW7LBvijp==TWOXDD-=hwOrhkmOmMcY4w@mail.gmail.com> <25138.1405703310@sss.pgh.pa.us>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox