Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1TvAJr-0000YP-5T for pgsql-bugs@arkaria.postgresql.org; Tue, 15 Jan 2013 17:29:03 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.72) (envelope-from ) id 1TvAJq-0003La-KA for pgsql-bugs@arkaria.postgresql.org; Tue, 15 Jan 2013 17:29:02 +0000 Received: from makus.postgresql.org ([98.129.198.125]) by malur.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1TvAJp-0003Kc-D1 for pgsql-bugs@postgresql.org; Tue, 15 Jan 2013 17:29:01 +0000 Received: from smtp-outbound-2.vmware.com ([208.91.2.13]) by makus.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1TvAJn-0001zO-OY for pgsql-bugs@postgresql.org; Tue, 15 Jan 2013 17:29:00 +0000 Received: from sc9-mailhost1.vmware.com (sc9-mailhost1.vmware.com [10.113.161.71]) by smtp-outbound-2.vmware.com (Postfix) with ESMTP id 7E98B28520; Tue, 15 Jan 2013 09:28:58 -0800 (PST) Received: from zcs-prod-ext-mta-1.vmware.com (zcs-prod-ext-mta-1.vmware.com [10.113.62.223]) by sc9-mailhost1.vmware.com (Postfix) with ESMTP id 7B10A18543; Tue, 15 Jan 2013 09:28:58 -0800 (PST) Received: from localhost (localhost.localdomain [127.0.0.1]) by zcs-prod-ext-mta-1.vmware.com (Postfix) with ESMTP id 88A311219C5; Tue, 15 Jan 2013 09:31:08 -0800 (PST) X-Virus-Scanned: amavisd-new at zcs-prod-ext-mta-1.vmware.com Received: from zcs-prod-ext-mta-1.vmware.com ([127.0.0.1]) by localhost (zcs-prod-ext-mta-1.vmware.com [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id EMePaEyiiMBo; Tue, 15 Jan 2013 09:31:08 -0800 (PST) Received: from zcs-prod-ext-mta-1.vmware.com (localhost.localdomain [127.0.0.1]) by zcs-prod-ext-mta-1.vmware.com (Postfix) with ESMTP id 63B6B12199A; Tue, 15 Jan 2013 09:31:08 -0800 (PST) Received: from [192.168.1.90] (unknown [10.113.62.14]) by zcs-prod-ext-mta-1.vmware.com (Postfix) with ESMTPSA; Tue, 15 Jan 2013 09:31:07 -0800 (PST) Message-ID: <50F591E4.2080508@vmware.com> Date: Tue, 15 Jan 2013 19:29:08 +0200 From: Heikki Linnakangas Organization: VMware User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:10.0.11) Gecko/20121122 Icedove/10.0.11 MIME-Version: 1.0 To: 1584171677@qq.com CC: pgsql-bugs@postgresql.org Subject: Re: BUG #7811: strlen(NULL) cause psql crash References: In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: quoted-printable X-Pg-Spam-Score: -6.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 On 15.01.2013 16:18, 1584171677@qq.com wrote: > I give you a description about how to trigger this bug first=EF=BC= =9A > (1) start the server with the command "postgres -D pgdata" > (2) start the client with the command "psql" > (3) close the server > (4) execute a query from the client "slect *from t; ". At this > time, the client detected that it lost the connection with the server. > (5) execute the following command from the client "\?", then t= he > client will crash. > > I have found the reason which caused that. > > (1) When the client execute "slect *from t; ", it execute the > function "ResetCancelConn()" at line 364 in src\bin\psql\common.c ,and the > function set pset.db to NULL. > (2) When the client execute "\?", it execute the function fpri= ntf > at line 254 in help.c. The value returned by PQdb(pset.db) is an argument= of > fprintf, and at this time PQdb returned NULL. > (3) This NULL was finally passed to strlen at line 779 in > snprintf.c through several simple fuction calls, so psql crashed. Thanks for the report and debugging! > I hava fixed the bug in the following way which may be not the bes= t: > > (1) add a string named strofnull, and in the function "dopr" in > file src\port\snprintf.c > char *strofnull=3D"(null)"; > > (2) add an if statment before calling fmtstr at about line 720= in > file src\port\snprintf.c > > if (strvalue=3D=3DNULL) > { > strvalue=3Dstrofnull; > } That'd change the behavior of all sprintf calls, not sure we want to go=20 there. Might not be a bad idea to avoid crashes if there are more bugs=20 like this, but one really should not pass NULL to sprintf to begin with.=20 I committed a local fix to help.c to print "none" as the database name=20 when not connected. - Heikki --=20 Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-bugs