From 70354e89fb1d5c26b2715b0f358ba10744df0444 Mon Sep 17 00:00:00 2001 From: MutableFire Date: Tue, 7 Jul 2026 15:58:46 +0300 Subject: [PATCH] Fix segfault: add location --- tea/gpext/tea_fdw.c | 29 ++++++++++++++++++++++++++++- tea/gpext/tea_fdw_options.c | 8 ++++++++ 2 files changed, 36 insertions(+), 1 deletion(-) diff --git a/tea/gpext/tea_fdw.c b/tea/gpext/tea_fdw.c index a444fa55..606121ab 100644 --- a/tea/gpext/tea_fdw.c +++ b/tea/gpext/tea_fdw.c @@ -8,6 +8,7 @@ #include "access/reloptions.h" #include "access/sysattr.h" #include "catalog/namespace.h" +#include "catalog/pg_foreign_table.h" #include "cdb/cdbsreh.h" #include "cdb/cdbutil.h" #include "cdb/cdbvars.h" @@ -1033,5 +1034,31 @@ Datum tea_fdw_handler(PG_FUNCTION_ARGS) { * USER MAPPING or FOREIGN TABLE that uses tea_fdw. * * Raise an ERROR if the option or its value is considered invalid. + * Specifically ensures 'location' is present for foreign tables to prevent segfaults. */ -Datum tea_fdw_validator(PG_FUNCTION_ARGS) { PG_RETURN_VOID(); } +Datum tea_fdw_validator(PG_FUNCTION_ARGS) { + List* options_list = untransformRelOptions(PG_GETARG_DATUM(0)); + Oid catalog = PG_GETARG_OID(1); + ListCell* cell; + bool location_found = false; + + foreach (cell, options_list) { + DefElem* def = (DefElem*)lfirst(cell); + char* val = def->arg ? strVal(def->arg) : NULL; + + if (strcmp(def->defname, "location") == 0) { + if (!val || !*val) + ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE), errmsg("option \"location\" value cannot be empty"))); + location_found = true; + } else { + ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE), errmsg("invalid option \"%s\"", def->defname), + errhint("Valid options for tea_fdw are: location"))); + } + } + + if (catalog == ForeignTableRelationId && !location_found) + ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE), errmsg("missing required option \"location\""), + errdetail("The TEA foreign data wrapper requires the \"location\" option."))); + + PG_RETURN_VOID(); +} diff --git a/tea/gpext/tea_fdw_options.c b/tea/gpext/tea_fdw_options.c index 6353de10..fb2d1443 100644 --- a/tea/gpext/tea_fdw_options.c +++ b/tea/gpext/tea_fdw_options.c @@ -2,6 +2,8 @@ #include "commands/defrem.h" #include "foreign/foreign.h" +#include "utils/elog.h" +#include "utils/errcodes.h" char* TeaGetLocation(Oid foreigntableid) { ForeignTable* table; @@ -16,5 +18,11 @@ char* TeaGetLocation(Oid foreigntableid) { return defGetString(def); } } + + ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE), + errmsg("missing required option \"location\" for foreign table %u", foreigntableid), + errdetail("The TEA foreign data wrapper requires the \"location\" option to be specified."), + errhint("Recreate the foreign table with OPTIONS (location '...')."))); + return NULL; }