Skip to content

Fix inverted SPI result checks that could crash the backend - #69

Merged
darold merged 1 commit into
darold:masterfrom
virendercse:fix-spi-result-checks
Nov 5, 2026
Merged

darold merged 1 commit into
darold:masterfrom
virendercse:fix-spi-result-checks

Conversation

@virendercse

Copy link
Copy Markdown
Contributor

After creating the "template" table, gtt_create_table_statement() and gtt_create_table_as() look up the OID of the new relation by name with SPI and then read SPI_tuptable->vals[0]. The result was validated with

if (result != SPI_OK_SELECT && SPI_processed != 1)

which only raises an error when both conditions are true. When the SELECT succeeds but returns no row (for example when an event trigger renames the freshly created table, or in any other case where the name lookup misses), the check passes and vals[0] is read from an empty result set. This dereferences an uninitialized pointer and crashes the backend with a segmentation fault, which makes the postmaster restart all the sessions of the cluster.

Use || so that any unexpected result is reported as a regular error. Also fix the validity check of the temporary table OID in the WITH DATA path of CREATE TABLE AS, which was testing gttOid (already validated) instead of the value that was just fetched, gtt.temp_relid.

Add regression test 20_spi_lookup that forces the lookup to return no row for both CREATE GLOBAL TEMPORARY TABLE and CREATE GLOBAL TEMPORARY TABLE ... AS. Without the fix the backend crashes; with the fix both statements fail cleanly and the session remains usable.

After creating the "template" table, gtt_create_table_statement() and
gtt_create_table_as() look up the OID of the new relation by name with
SPI and then read SPI_tuptable->vals[0]. The result was validated with

    if (result != SPI_OK_SELECT && SPI_processed != 1)

which only raises an error when *both* conditions are true. When the
SELECT succeeds but returns no row (for example when an event trigger
renames the freshly created table, or in any other case where the name
lookup misses), the check passes and vals[0] is read from an empty
result set. This dereferences an uninitialized pointer and crashes the
backend with a segmentation fault, which makes the postmaster restart
all the sessions of the cluster.

Use || so that any unexpected result is reported as a regular error.
Also fix the validity check of the temporary table OID in the WITH DATA
path of CREATE TABLE AS, which was testing gttOid (already validated)
instead of the value that was just fetched, gtt.temp_relid.

Add regression test 20_spi_lookup that forces the lookup to return no
row for both CREATE GLOBAL TEMPORARY TABLE and CREATE GLOBAL TEMPORARY
TABLE ... AS. Without the fix the backend crashes; with the fix both
statements fail cleanly and the session remains usable.
@darold
darold merged commit bc7b05f into darold:master Nov 5, 2026
7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants