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.89) (envelope-from ) id 1gvpX8-0001RX-B8 for pgsql-hackers@arkaria.postgresql.org; Mon, 18 Feb 2019 20:28:58 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1gvpW8-0003Xy-QR for pgsql-hackers@arkaria.postgresql.org; Mon, 18 Feb 2019 20:27:56 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1gvpW8-0003Xr-Ir for pgsql-hackers@lists.postgresql.org; Mon, 18 Feb 2019 20:27:56 +0000 Received: from mail-qk1-x743.google.com ([2607:f8b0:4864:20::743]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1gvpW5-0001Kr-L4 for pgsql-hackers@lists.postgresql.org; Mon, 18 Feb 2019 20:27:55 +0000 Received: by mail-qk1-x743.google.com with SMTP id i5so10715315qkd.13 for ; Mon, 18 Feb 2019 12:27:53 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=2ndquadrant-com.20150623.gappssmtp.com; s=20150623; h=date:from:to:subject:message-id:mime-version:content-disposition :content-transfer-encoding:user-agent; bh=IdUO8yyDbKhq1koe6M1gMS8o3CLC9vmLppHDQRrgP8I=; b=kc46A2QOwtGWPKe8jm5wUVtmxUBev4rzD3VFYYdHrMC3M12HByl1quE+tUDkWIXLNK Dgj6sRCPjGxwO9IUedjrwQuxgVuvMvBH3YVnAqys4v525c+2j9Z/boxqYBachuUeGJKr rageegfgief4J+qheRdjMQmY5QVPR2Cm6xPEC6QGsK4RMHapErcbBVu6wJfKJMKkFS53 YTVCzSxk+cReUKSdf3qLz/YsVU48DnJY3jhOYLXnw9AT7DULX+qkzRm+TjU1QLiD8GbR LkAYsKCKI5G0n4RC6j+jWRJe4DJDvYK1lMWibgBpoZpsBbh6K717VJVR56tTWXoMiJ3f jSEw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:subject:message-id:mime-version :content-disposition:content-transfer-encoding:user-agent; bh=IdUO8yyDbKhq1koe6M1gMS8o3CLC9vmLppHDQRrgP8I=; b=p2MOoErBHk7ya36kEkYQd0oQ2Cy9tpzvHt2oszjMHm1feykkegCThVJDQEVAFj9Kvb Y8GJnmApQ9gf+AYW1ZjzWvDNzUnlV8UsdQ+AJly+CX0sHc8z2LMEbPwht1HYlUNmV0nJ Kr1WyRD1fFUujL14xeMVKGuCplxB3O2DWPLyUIe9fEzHKZLxBZw75S6Eq66L+ubdofZ+ siOTYz+YdBLqt7aYYnSgpvncGe8RI8R/CdFwknYKPctuKnYvKYy2TlS5oZmIdRFE7UiC L5q78YkGi76iWawMDt0cKXC4zrNMZLnppKFt+CYqEzo6K8Fd/MpJO4KYNbAWchoXbEn4 aPWQ== X-Gm-Message-State: AHQUAuZt0ElW3XMzk5wdFGHDOMsx/O/oi2dPCpmjzicYPj17lgVpaCcm BevODrK2UqilFErCtvbzteeNNDd7DJ80WoFpgHx6jm84pIxzMZNnu4ZO5e1QLsDGUEIti/yIHVI CHF6nWAfp6PLD6M/dwFKs1uNwK/Wihjcsx00OI3Jqy+8PJ5bwnKnE0XFM4EQp4FjC7W4scnQx1h GurAFmOmDvAieb7/ht X-Google-Smtp-Source: AHgI3IZRCSA6GrYVuIqenEw3IcBOmWFIhNWB+LZLZ2U3tRBrGJH8WY7FSxiENFPEE6c7vbhGrGDlrg== X-Received: by 2002:a37:4b49:: with SMTP id y70mr17952376qka.134.1550521670446; Mon, 18 Feb 2019 12:27:50 -0800 (PST) Received: from nimloth.alvh.no-ip.org ([191.126.99.81]) by smtp.gmail.com with ESMTPSA id q12sm7942134qtc.20.2019.02.18.12.27.49 for (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Mon, 18 Feb 2019 12:27:49 -0800 (PST) Received: by nimloth.alvh.no-ip.org (Postfix, from userid 1000) id F143C1238D6; Mon, 18 Feb 2019 17:27:43 -0300 (-03) Date: Mon, 18 Feb 2019 17:27:43 -0300 From: Alvaro Herrera To: Pg Hackers Subject: crash in pg_identify_object_as_address Message-ID: <20190218202743.GA12392@alvherre.pgsql> MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="AqsLC8rIMeq19msA" Content-Disposition: inline Content-Transfer-Encoding: 8bit User-Agent: Mutt/1.9.4 (2018-02-28) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk --AqsLC8rIMeq19msA Content-Type: text/plain; charset=iso-8859-1 Content-Disposition: inline Content-Transfer-Encoding: 8bit Hello I just came across a crash while debugging some corrupted system catalogs; pg_identify_object_as_address fails to cope with some NULL input, causing a crash. Attached patch fixes it. Naturally, the output array will contain NULL members in the output, but that's better than crashing ... (The first hunk is purely speculative; I haven't seen anything that requires that. The actual fix is in the other hunks. But seems better to be defensive.) The crash can be reproduced thusly create function f() returns int language plpgsql as $$ begin return 1; end; $$; update pg_proc set pronamespace = 9999 where proname = 'f' returning oid \gset select * from pg_identify_object_as_address('pg_proc'::regclass, :oid, 0); After the patch, the last line returns: type | object_names | object_args ----------+--------------+------------- function | {NULL,f} | {} where the NULL obviously corresponds to the bogus pg_namespace OID being referenced. The patch is on 9.6. I checked 10 and it applies fine there. My intention is to apply to all branches since 9.5. -- Álvaro Herrera PostgreSQL Expert, https://www.2ndQuadrant.com/ --AqsLC8rIMeq19msA Content-Type: text/x-diff; charset=us-ascii Content-Disposition: attachment; filename="pg_identify_object_as_address-crash.patch" diff --git a/src/backend/catalog/objectaddress.c b/src/backend/catalog/objectaddress.c index bd55dfd08e3..479087592da 100644 --- a/src/backend/catalog/objectaddress.c +++ b/src/backend/catalog/objectaddress.c @@ -3576,7 +3576,10 @@ pg_identify_object_as_address(PG_FUNCTION_ARGS) pfree(identity); /* object_names */ - values[1] = PointerGetDatum(strlist_to_textarray(names)); + if (names != NIL) + values[1] = PointerGetDatum(strlist_to_textarray(names)); + else + values[1] = PointerGetDatum(construct_empty_array(TEXTOID)); nulls[1] = false; /* object_args */ @@ -4803,10 +4806,12 @@ strlist_to_textarray(List *list) { ArrayType *arr; Datum *datums; + bool *nulls; int j = 0; ListCell *cell; MemoryContext memcxt; MemoryContext oldcxt; + int one = 1; memcxt = AllocSetContextCreate(CurrentMemoryContext, "strlist to array", @@ -4814,17 +4819,24 @@ strlist_to_textarray(List *list) oldcxt = MemoryContextSwitchTo(memcxt); datums = palloc(sizeof(text *) * list_length(list)); + nulls = palloc(sizeof(bool) * list_length(list)); foreach(cell, list) { char *name = lfirst(cell); - datums[j++] = CStringGetTextDatum(name); + if (name) + { + nulls[j] = false; + datums[j++] = CStringGetTextDatum(name); + } + else + nulls[j++] = true; } MemoryContextSwitchTo(oldcxt); - arr = construct_array(datums, list_length(list), - TEXTOID, -1, false, 'i'); + arr = construct_md_array(datums, nulls, 1, &j, + &one, TEXTOID, -1, false, 'i'); MemoryContextDelete(memcxt); return arr; --AqsLC8rIMeq19msA--