Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1X7p71-0005TK-BY for pgsql-bugs@arkaria.postgresql.org; Thu, 17 Jul 2014 17:04:55 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.80) (envelope-from ) id 1X7p70-0006HP-R2 for pgsql-bugs@arkaria.postgresql.org; Thu, 17 Jul 2014 17:04:54 +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 1X7p6z-0006EP-Io for pgsql-bugs@postgresql.org; Thu, 17 Jul 2014 17:04:53 +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 1X7p6w-0008Sp-Kr for pgsql-bugs@postgresql.org; Thu, 17 Jul 2014 17:04:51 +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 s6HH4m71005865; Thu, 17 Jul 2014 13:04:48 -0400 From: Tom Lane To: arthur.j.odwyer@gmail.com cc: pgsql-bugs@postgresql.org Subject: Re: BUG #10976: Two memory leaks in regcomp cleanup In-reply-to: <20140716193828.2503.81394@wrigleys.postgresql.org> References: <20140716193828.2503.81394@wrigleys.postgresql.org> Comments: In-reply-to arthur.j.odwyer@gmail.com message dated "Wed, 16 Jul 2014 19:38:28 -0000" Date: Thu, 17 Jul 2014 13:04:48 -0400 Message-ID: <5864.1405616688@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.j.odwyer@gmail.com writes: > When MALLOC fails, pg_regcomp leaks memory in at least two places: > (A) In freev(), the line > freesubre(info, v, v->tree); > should be > freesubre(info, NULL, v->tree); > as otherwise the "freed" subres will end up on v->treefree, which is leaked > by the cleanst() two lines later. Hmm ... what version of the code are you looking at exactly? There's no "info" argument here in Postgres. > That is, given the precondition that there are things in v->tree that aren't > in v->treechain. 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. It looks to me like there are two different operating regimes in this code: one where all the subres are still in the treechain, and one where we've marked as INUSE all the ones that are reachable from v->tree and garbage-collected the rest. The markst/cleanst steps in pg_regcomp are where the conversion is made. freev needs to work correctly either before or after that. In short, I think you're right that there's a bug here, but this is not a good fix for it. I'm not sure freev has enough info to do the right thing; we may need to rethink the data structure invariants, so that there's not two different ways to clean up. > (B) newlacon() leaks memory if REALLOC returns NULL on this line: > v->lacons = (struct subre *) REALLOC(v->lacons, > (v->nlacons + 1) * sizeof(struct subre)); > The fix is to use the same idiom already used everywhere else REALLOC is > called in this module. A temp variable, you mean. Yeah, that's a bug. 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