Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1eJL26-0003U6-V4 for pgsql-bugs@arkaria.postgresql.org; Mon, 27 Nov 2017 15:09:19 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1eJL26-0001cO-E5 for pgsql-bugs@arkaria.postgresql.org; Mon, 27 Nov 2017 15:09:18 +0000 Received: from makus.postgresql.org ([2001:4800:1501:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1eJL26-0001cE-5J for pgsql-bugs@lists.postgresql.org; Mon, 27 Nov 2017 15:09:18 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1eJL1z-0001Ug-Dw for pgsql-bugs@postgresql.org; Mon, 27 Nov 2017 15:09:16 +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 vARF8wBk017001; Mon, 27 Nov 2017 10:08:58 -0500 From: Tom Lane To: PanBian cc: Michael Paquier , Amit Langote , pgsql-bugs@postgresql.org Subject: Re: BUG #14929: Unchecked AllocateDir() return value in restoreTwoPhaseData() In-reply-to: <20171127114410.GA30545@bp> References: <20171127093107.1473.70477@wrigleys.postgresql.org> <20171127114410.GA30545@bp> Comments: In-reply-to PanBian message dated "Mon, 27 Nov 2017 19:44:10 +0800" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <16999.1511795338.1@sss.pgh.pa.us> Content-Transfer-Encoding: quoted-printable Date: Mon, 27 Nov 2017 10:08:58 -0500 Message-ID: <17000.1511795338@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk PanBian writes: > On Mon, Nov 27, 2017 at 07:53:30PM +0900, Michael Paquier wrote: >> You are missing the fact that ReadDir goes through ReadDirExtended, >> which drops an ERROR log if the folder allocated is NULL. > You are right. Its my carelessness. ReadDir will not return back on a > NULL dir parameter. The code is bug free. Sorry for the trouble. There are some issues here, though: 1. twophase.c does this: cldir =3D AllocateDir(TWOPHASE_DIR); LWLockAcquire(TwoPhaseStateLock, LW_EXCLUSIVE); while ((clde =3D ReadDir(cldir, TWOPHASE_DIR)) !=3D NULL) { which is flat out wrong because LWLockAcquire might well clobber errno. I don't see any good reason why we couldn't just swap the order of those two calls. 2. basebackup.c and some other places do things like dir =3D AllocateDir("pg_wal"); if (!dir) ereport(ERROR, (errmsg("could not open directory \"%s\": %m", "pg_wal= "))); while ((de =3D ReadDir(dir, "pg_wal")) !=3D NULL) Not only is this a waste of code, because the error message is no better than what ReadDir would provide, but it's wrong because it omits errcode_for_file_access(), causing the SQLSTATE to be reported as XX000. There are other places that are even lazier and use elog(), failing translatability as well as the errcode test. There might be some other problems I missed in a quick scan. So there's definitely room for a cleanup patch here, but the originally proposed change isn't it. regards, tom lane