Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.98.2) (envelope-from ) id 1xHOj4-00000002yrn-1Jao for pgsql-bugs@arkaria.postgresql.org; Thu, 15 Oct 2026 16:54:42 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.98.2) (envelope-from ) id 1xHOj3-0000000A6qE-1NpO for pgsql-bugs@arkaria.postgresql.org; Thu, 15 Oct 2026 16:54:41 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.98.2) (envelope-from ) id 1xHN14-00000009Uzg-3f6U for pgsql-bugs@lists.postgresql.org; Thu, 15 Oct 2026 15:05:10 +0000 Received: from mahout.postgresql.org ([2001:4800:3e1:1::227]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.98.2) (envelope-from ) id 1xHN0z-00000002GMg-3Vs3 for pgsql-bugs@lists.postgresql.org; Thu, 15 Oct 2026 15:05:10 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=postgresql.org; s=20171124; h=Message-ID:Date:Reply-To:Cc:From:To:Subject: Content-Transfer-Encoding:MIME-Version:Content-Type:Sender:Content-ID: Content-Description:In-Reply-To:References; bh=eE9TYNTEXrHVQuJ5r/QQalAFnMcm3TjVNzE/eOuf7Ec=; b=JTkDIdTaI9ybXxKNOhGG+Nq5p1 kwaZqvlrLkMKBrQEz3KA4DE7ik7bxl4aYOLGB3H6nbJNNFXxY5RBo+iun8wrcrEFqZyVY+7PEV49+ UD7SBxSR3ickRSZvdzTLEB2ropFpgveh8PjrxtjQD1StA6RIbAq7nzSb4/zSAiNhmaWsSxSuWyV5a ne1unNueZS4hjJl9Ne4Ed2vnkTZcahwlPJvM+sQtr7bX3/HdbFX3MPH3s51dwW7i5kXz+dy/Zi5TY HFl0QUUHUU4qeHy8XIR5F5pPPC7hyvP3wkMZDJPlY2sTcI/bP+k4oG4M7szIF/7J9/SgV83AISjej 0eKj5BAQ==; Received: from wrigleys.postgresql.org ([2a02:16a8:dc51::60]) by mahout.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.98.2) (envelope-from ) id 1xHN0y-00000006Ivv-13bN for pgsql-bugs@lists.postgresql.org; Thu, 15 Oct 2026 15:05:04 +0000 Received: from localhost ([127.0.0.1] helo=wrigleys.postgresql.org) by wrigleys.postgresql.org with esmtp (Exim 4.98.2) (envelope-from ) id 1xHN0x-00000001iG5-0gw8 for pgsql-bugs@lists.postgresql.org; Thu, 15 Oct 2026 15:05:03 +0000 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Subject: BUG #19763: ManualBug2 NULL pointer dereference in make_build_data via examine_expression bypass of lookup_var_a To: pgsql-bugs@lists.postgresql.org From: PG Bug reporting form Cc: 1217816127@qq.com Reply-To: 1217816127@qq.com, pgsql-bugs@lists.postgresql.org Date: Thu, 15 Oct 2026 15:05:00 +0000 Message-ID: <19763-14da3f96c02f07e8@postgresql.org> X-Auto-Response-Suppress: All Auto-Submitted: auto-generated List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk The following bug has been logged on the website: Bug reference: 19763 Logged by: Yuelin Wang Email address: 1217816127@qq.com PostgreSQL version: 19rc1 Operating system: Linux (Ubuntu 24.04, x86_64) Description: =20 ### What happens? `make_build_data()` in `src/backend/statistics/extended_stats.c` (line 2586) calls `examine_expression(expr, stattarget)` for each expression in a statistics object without checking for a NULL return value. The function `examine_expression()` can return NULL when a typanalyze callback returns false for the given `attstattarget` value. The invariant violated is: every element of `data->stats[]` returned by `make_build_data()` must be a valid non-NULL `VacAttrStats` pointer before any stat builder iterates over the array. The upstream guard in `BuildRelationExtStatistics()` calls `lookup_var_attr_stats()` first (line 169) and returns early when it yields NULL. However, `lookup_var_attr_stats()` calls `examine_attribute(expr)` (line 776) which sets `attstattarget =3D -1`, whereas `make_build_data()` calls `examine_expression(expr, stattarget)` (line 2586) which sets `attstattarget =3D stattarget` (a positive integer). A typanalyze callback that behaves differently depending on `attstattarget` can therefore pass `examine_attribute` (returning true) but fail `examine_expression` (returning false, causing NULL). The NULL then propagates through `make_build_data()` into stat builders such as `statext_ndistinct_build()`, `statext_mcv_build()`, and `statext_dependencies_build()`, all of which dereference `data->stats[i]` without a NULL check. The crash path confirmed by ASAN is: ``` make_build_data extended_stats.c:2586 examine_expression returns NULL for stattarget-sensitive typanalyze data->stats[idx] =3D NULL BuildRelationExtStatistics extended_stats.c:206 calls statext_ndistinct_build(data) statext_ndistinct_build mvdistinct.c:125 iterates over data->stats[] ndistinct_for_combination mvdistinct.c:459 VacAttrStats *colstat =3D data->stats[combination[i]] =3D=3D NULL typid =3D colstat->attrtypid <- NULL dereference (SEGFAULT) ``` ### To Reproduce First build and install the `test_custom_types` extension with an additional `int_custom_typanalyze_stattarget_sensitive` function added to `src/test/modules/test_custom_types/test_custom_types.c`. The function returns true (with valid `compute_stats` and `minrows`) when `stats->attstattarget < 0` (the value set by `examine_attribute`), and returns false when `stats->attstattarget >=3D 0` (the value set by `examine_expression` with the actual statistics target). Start a fresh PostgreSQL ASAN instance and run: ```sql SELECT version(); CREATE EXTENSION test_custom_types; CREATE TABLE t1 (id int, data int_custom); INSERT INTO t1 SELECT i, (i % 100)::text::int_custom FROM generate_series(1, 1000) i; CREATE OR REPLACE FUNCTION func_ic(p int_custom) RETURNS int_custom LANGUAGE plpgsql AS $$ BEGIN RETURN p; END; $$; ALTER TYPE int_custom SET (ANALYZE =3D int_custom_typanalyze_stattarget_sensitive); CREATE STATISTICS st1 ON func_ic(data), (id) FROM t1; ANALYZE t1; SELECT 'survived' AS result; ``` Trigger output: ``` version -------------------------------------------------------------------- PostgreSQL 20devel on x86_64-linux, compiled by gcc-13.3.0, 64-bit (1 row) CREATE EXTENSION CREATE TABLE INSERT 0 1000 CREATE FUNCTION ALTER TYPE CREATE STATISTICS server closed the connection unexpectedly This probably means the server terminated abnormally before or while processing the request. connection to server was lost ``` pg.log: ``` 2026-10-15 08:38:12.423 +08 [2602104] LOG: starting PostgreSQL 20devel on x86_64-linux, compiled by gcc-13.3.0, 64-bit 2026-10-15 08:38:12.423 +08 [2602104] LOG: listening on IPv4 address "127.0.0.1", port 14064 2026-10-15 08:38:12.423 +08 [2602104] LOG: listening on Unix socket "/tmp/.s.PGSQL.14064" 2026-10-15 08:38:12.432 +08 [2602109] LOG: database system was shut down at 2026-10-15 08:38:11 +08 2026-10-15 08:38:12.440 +08 [2602104] LOG: database system is ready to accept connections AddressSanitizer:DEADLYSIGNAL 2026-10-15 08:38:14.788 +08 [2602104] LOG: client backend (PID 2602233) was terminated by signal 6: Aborted 2026-10-15 08:38:14.788 +08 [2602104] DETAIL: Failed process was running: ANALYZE t1; 2026-10-15 08:38:14.788 +08 [2602104] LOG: terminating any other active server processes 2026-10-15 08:38:14.793 +08 [2602104] LOG: all server processes terminated; reinitializing ``` ASAN report: ``` =3D=3D2602233=3D=3DERROR: AddressSanitizer: SEGV on unknown address 0x00000= 0000004 (pc 0x594bbf1a8535 bp 0x7ffcec007d70 sp 0x7ffcec007ca0 T0) =3D=3D2602233=3D=3DThe signal is caused by a READ memory access. =3D=3D2602233=3D=3DHint: address points to the zero page. #0 0x594bbf1a8535 in ndistinct_for_combination ../src/backend/statistics/mvdistinct.c:459 #1 0x594bbf1a8535 in statext_ndistinct_build ../src/backend/statistics/mvdistinct.c:125 #2 0x594bbf192013 in BuildRelationExtStatistics ../src/backend/statistics/extended_stats.c:206 #3 0x594bbeb807f7 in do_analyze_rel ../src/backend/commands/analyze.c:645 #4 0x594bbeb83d20 in analyze_rel ../src/backend/commands/analyze.c:299 #5 0x594bbecef696 in vacuum ../src/backend/commands/vacuum.c:653 #6 0x594bbecf0e6c in ExecVacuum ../src/backend/commands/vacuum.c:467 #7 0x594bbf286a15 in standard_ProcessUtility ../src/backend/tcop/utility.c:860 #8 0x594bbf282d46 in PortalRunUtility ../src/backend/tcop/pquery.c:1153 #9 0x594bbf282fa4 in PortalRunMulti ../src/backend/tcop/pquery.c:1311 #10 0x594bbf283c64 in PortalRun ../src/backend/tcop/pquery.c:788 #11 0x594bbf27a670 in exec_simple_query ../src/backend/tcop/postgres.c:1297 #12 0x594bbf27e970 in PostgresMain ../src/backend/tcop/postgres.c:4946 #13 0x594bbf27134c in BackendMain ../src/backend/tcop/backend_startup.c:124 #14 0x594bbf092b51 in postmaster_child_launch ../src/backend/postmaster/launch_backend.c:268 #15 0x594bbf099c76 in BackendStartup ../src/backend/postmaster/postmaster.c:3650 #16 0x594bbf099c76 in ServerLoop ../src/backend/postmaster/postmaster.c:1727 #17 0x594bbf09cb29 in PostmasterMain ../src/backend/postmaster/postmaster.c:1414 #18 0x594bbe88e20d in main ../src/backend/main/main.c:227 SUMMARY: AddressSanitizer: SEGV ../src/backend/statistics/mvdistinct.c:459 in ndistinct_for_combination =3D=3D2602233=3D=3DABORTING ``` ### Environment - OS: Ubuntu 24.04.2 LTS, x86_64 (Linux 6.8.0-55-generic) - PostgreSQL Version: PostgreSQL 20devel on x86_64-linux, compiled by gcc-13.3.0, 64-bit - Client: psql, ASAN instrumented build (built from source with -fsanitize=3Daddress) - Hardware: N/A ### Identity Disclosure - Full Name: Yuelin - Affiliation: Individual