Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.92) (envelope-from ) id 1jHqLp-0000vy-9G for pgsql-bugs@arkaria.postgresql.org; Fri, 27 Mar 2020 14:52:49 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1jHqLn-00071r-On for pgsql-bugs@arkaria.postgresql.org; Fri, 27 Mar 2020 14:52:47 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1jHqLn-00070J-Cy for pgsql-bugs@lists.postgresql.org; Fri, 27 Mar 2020 14:52:47 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1jHqLg-0006hy-Nu for pgsql-bugs@lists.postgresql.org; Fri, 27 Mar 2020 14:52:46 +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 02REqcjT029213; Fri, 27 Mar 2020 10:52:38 -0400 From: Tom Lane To: hghwng@gmail.com cc: pgsql-bugs@lists.postgresql.org Subject: Re: BUG #16321: Memory leaks in PostmasterMain In-reply-to: <16321-c12957af6e8c231c@postgresql.org> References: <16321-c12957af6e8c231c@postgresql.org> Comments: In-reply-to PG Bug reporting form message dated "Fri, 27 Mar 2020 03:21:40 -0000" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <29211.1585320758.1@sss.pgh.pa.us> Content-Transfer-Encoding: quoted-printable Date: Fri, 27 Mar 2020 10:52:38 -0400 Message-ID: <29212.1585320758@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk PG Bug reporting form writes: > The argument parsing duplicates strings, but never frees them. This hardly amounts to enough of a problem to worry about. The string might be leaked, or it might not, but tracking whether it is is more trouble than it's worth. Generally we only worry about memory leaks if they (a) can waste a lot of memory or (b) can repeat, and thereby accumulate to waste a lot of memory. Surely neither one applies to postmaster argument parsing. > For example, when you pass "-D $DATA_DIR" to postmaster, postmaster > duplicates the string here: > https://github.com/postgres/postgres/blob/master/src/backend/postmaster/= postmaster.c#L698 > The duplicated string is passed to `SelectConfigFiles`, which does > everything except freeing the string. This is a great example of a case where the cure is likely to be worse than the disease. SelectConfigFiles surely has little business freeing its input string (indeed, it couldn't do so without casting away the "const"). On the other hand, the caller doesn't really know whether SelectConfigFiles is going to stash away a copy of the pointer; it wouldn't be unreasonable for it to do so. So in order to not perhaps-leak a few dozen bytes, we'd have to make that API more complicated and more fragile. It's not a win. As for why we strdup the argument in the first place, see here: https://www.postgresql.org/message-id/flat/20121008184026.GA28752%40momjia= n.us regards, tom lane