Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1mXRT3-0001BN-KA for pgsql-hackers@arkaria.postgresql.org; Mon, 04 Oct 2021 17:09:33 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1mXRT2-0006ny-Hj for pgsql-hackers@arkaria.postgresql.org; Mon, 04 Oct 2021 17:09:32 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1mXRT2-0006np-7L for pgsql-hackers@lists.postgresql.org; Mon, 04 Oct 2021 17:09:32 +0000 Received: from mail.postgrespro.ru ([93.174.131.139]) by magus.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1mXRSz-0001AP-Nl for pgsql-hackers@lists.postgresql.org; Mon, 04 Oct 2021 17:09:31 +0000 Received: from Sergeys-MacBook-Pro.local (cyclops.postgrespro.ru [93.174.131.138]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (Client did not present a certificate) by mail.postgrespro.ru (Postfix) with ESMTPSA id 4095021C314D; Mon, 4 Oct 2021 20:09:29 +0300 (MSK) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=postgrespro.ru; s=mail; t=1633367369; bh=KHdpaUtbIsSZILg3McGk6GA5vPdEpMw3nwsm8hU2Yyc=; h=To:From:Subject:Cc:Date; b=mz2SGarcIJrwScZg/nGGi+Ay5euDtPhraHU3p79x2LHZSg47h74TORuYSaqWp3V/d XMH6XQBOQ6HVBFYAFKRphrLetZzCZ0PK5ippe4MVor8nfPNf5pd5P7Bd1H7qlaG4FE cbT5qLmA7BrCLhiJOqapvyWQXETf3KNqc4125/Hk= To: PostgreSQL Developers From: Sergey Shinderuk Subject: Bug in DefineRange() with multiranges Cc: Alex Kozhemyakin Message-ID: Date: Mon, 4 Oct 2021 20:09:28 +0300 User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:78.0) Gecko/20100101 Thunderbird/78.14.0 MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 7bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Hi, My colleague, Alex Kozhemyakin, stumbled upon a bug in DefineRange(). The problem is here: @@ -1707,7 +1707,6 @@ DefineRange(ParseState *pstate, CreateRangeStmt *stmt) /* Create cast from the range type to its multirange type */ CastCreate(typoid, multirangeOid, castFuncOid, 'e', 'f', DEPENDENCY_INTERNAL); - pfree(multirangeTypeName); pfree(multirangeArrayName); return address; Given a query create type textrange1 as range(subtype=text, multirange_type_name=multirange_of_text, collation="C"); the string "multirange_of_text" in the parse tree is erroneously pfree'd. The corrupted parse tree is then passed to event triggers. There is another branch in DefineRange() that genereates a multirange type name which is fine to free. I wonder what is the proper fix. Just drop pfree() altogether or add pstrdup() instead? I see that makeMultirangeTypeName() doesn't bother freeing its buf. Here is a gdb session demonstating the bug: Breakpoint 1, ProcessUtilitySlow (pstate=0x5652e80c7730, pstmt=0x5652e80a6a40, queryString=0x5652e80a5790 "create type textrange1 as range(subtype=text, multirange_type_name=multirange_of_text, collation=\"C\");", context=PROCESS_UTILITY_TOPLEVEL, params=0x0, queryEnv=0x0, qc=0x7ffe835b4be0, dest=) at /pgwork/REL_14_STABLE/src/src/backend/tcop/utility.c:1621 1621 address = DefineRange((CreateRangeStmt *) parsetree); (gdb) p *(Value *)((TypeName *)((DefElem *)((CreateRangeStmt *)parsetree)->params->elements[1].ptr_value)->arg)->names->elements[0].ptr_value $1 = {type = T_String, val = {ival = -401972176, str = 0x5652e80a6430 "multirange_of_text"}} (gdb) n 1900 if (!commandCollected) (gdb) p *(Value *)((TypeName *)((DefElem *)((CreateRangeStmt *)parsetree)->params->elements[1].ptr_value)->arg)->names->elements[0].ptr_value $2 = {type = T_String, val = {ival = -401972176, str = 0x5652e80a6430 '\177' , "\020"}} Regards, -- Sergey Shinderuk https://postgrespro.com/