Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1X8BoJ-0005Yl-HA for pgsql-bugs@arkaria.postgresql.org; Fri, 18 Jul 2014 17:19:07 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.80) (envelope-from ) id 1X8BoI-0000r4-GM for pgsql-bugs@arkaria.postgresql.org; Fri, 18 Jul 2014 17:19:06 +0000 Received: from makus.postgresql.org ([2001:4800:1501:1::229]) by malur.postgresql.org with esmtps (TLS1.2:DHE_RSA_AES_256_CBC_SHA256:256) (Exim 4.80) (envelope-from ) id 1X8Be8-0007ZJ-F4 for pgsql-bugs@postgresql.org; Fri, 18 Jul 2014 17:08:36 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by makus.postgresql.org with esmtps (TLS1.2:DHE_RSA_AES_256_CBC_SHA256:256) (Exim 4.80) (envelope-from ) id 1X8Be5-0001HO-1Q for pgsql-bugs@postgresql.org; Fri, 18 Jul 2014 17:08:34 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.14.4/8.14.4) with ESMTP id s6IH8U3G025139; Fri, 18 Jul 2014 13:08:30 -0400 From: Tom Lane To: "Arthur O'Dwyer" cc: pgsql-bugs@postgresql.org Subject: Re: BUG #10976: Two memory leaks in regcomp cleanup In-reply-to: References: <20140716193828.2503.81394@wrigleys.postgresql.org> <5864.1405616688@sss.pgh.pa.us> Comments: In-reply-to "Arthur O'Dwyer" message dated "Thu, 17 Jul 2014 12:11:03 -0700" Date: Fri, 18 Jul 2014 13:08:30 -0400 Message-ID: <25138.1405703310@sss.pgh.pa.us> X-Pg-Spam-Score: -1.9 (-) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-bugs Precedence: bulk Sender: pgsql-bugs-owner@postgresql.org "Arthur O'Dwyer" writes: > On Thu, Jul 17, 2014 at 10:04 AM, Tom Lane 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=1567e659a877d35ab4b85dafff41b2845d50990f 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