From 9130f943eec34b8f2df2fd287a991120b277f0ec Mon Sep 17 00:00:00 2001 From: Tender Wang Date: Tue, 10 Nov 2026 09:59:40 +0800 Subject: [PATCH] Validate expanded record test function arguments The expanded-record regression test functions accept polymorphic arguments, allowing them to be called with scalar values. However, their C implementations passed such values directly to DatumGetExpandedRecord(), which treats the Datum as a pointer. For example, passing the integer 2 caused the backend to dereference address 0x2 and crash. Check that the resolved argument type is a row type before converting the Datum. Apply the check to expanded_record_identity(), expanded_record_info(), and expanded_record_field_text(), all of which have the same issue. --- src/test/regress/regress.c | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/src/test/regress/regress.c b/src/test/regress/regress.c index 6e61c0d201b..9c65b723a8a 100644 --- a/src/test/regress/regress.c +++ b/src/test/regress/regress.c @@ -440,6 +440,17 @@ make_tuple_indirect(PG_FUNCTION_ARGS) PG_RETURN_POINTER(newtup->t_data); } +static ExpandedRecordHeader * +get_test_expanded_record(FunctionCallInfo fcinfo, int argno) +{ + Oid argtype = get_fn_expr_argtype(fcinfo->flinfo, argno); + + if (!OidIsValid(argtype) || !type_is_rowtype(argtype)) + elog(ERROR, "argument must be of a composite type"); + + return DatumGetExpandedRecord(fcinfo->args[argno].value); +} + /* * Functions for testing expanded records built from a flat composite datum. * @@ -456,7 +467,7 @@ PG_FUNCTION_INFO_V1(expanded_record_identity); Datum expanded_record_identity(PG_FUNCTION_ARGS) { - ExpandedRecordHeader *erh = PG_GETARG_EXPANDED_RECORD(0); + ExpandedRecordHeader *erh = get_test_expanded_record(fcinfo, 0); PG_RETURN_EXPANDED_RECORD(erh); } @@ -472,7 +483,7 @@ PG_FUNCTION_INFO_V1(expanded_record_info); Datum expanded_record_info(PG_FUNCTION_ARGS) { - ExpandedRecordHeader *erh = PG_GETARG_EXPANDED_RECORD(0); + ExpandedRecordHeader *erh = get_test_expanded_record(fcinfo, 0); TupleDesc tupdesc = expanded_record_get_tupdesc(erh); PG_RETURN_TEXT_P(cstring_to_text(psprintf("type %s, typmod is default: %s, natts %d", @@ -489,7 +500,7 @@ PG_FUNCTION_INFO_V1(expanded_record_field_text); Datum expanded_record_field_text(PG_FUNCTION_ARGS) { - ExpandedRecordHeader *erh = PG_GETARG_EXPANDED_RECORD(0); + ExpandedRecordHeader *erh = get_test_expanded_record(fcinfo, 0); int fnum = PG_GETARG_INT32(1); TupleDesc tupdesc = expanded_record_get_tupdesc(erh); Datum val; -- 2.43.0