Backend crash in expanded_record_info functions

Started by Tender Wang12 days ago4 messageshackers
Jump to latest
#1Tender Wang
tndrwang@gmail.com

Hi,

While testing the current master with SQLsmith, I found a query that crashes
the backend. After running the regression tests, the issue can be
reproduced in the regression database with:

postgres=# \c regression
You are now connected to database "regression" as user "ubuntu".
regression=# SELECT expanded_record_info(2);
server closed the connection unexpectedly
This probably means the server terminated abnormally
before or while processing the request.
The connection to the server was lost. Attempting reset: Succeeded.

(gdb) bt
#0 0x000062d8180991a3 in VARATT_IS_EXTERNAL (PTR=0x2) at
../../../../src/include/varatt.h:403
#1 0x000062d8180991c5 in VARATT_IS_EXTERNAL_EXPANDED_RW (PTR=0x2) at
../../../../src/include/varatt.h:436
#2 0x000062d81809a654 in DatumGetExpandedRecord (d=2) at expandedrecord.c:847
#3 0x000076332abce2b7 in expanded_record_info (fcinfo=0x62d845e6d230)
at regress.c:475
#4 0x000062d817cce8ba in ExecInterpExpr (state=0x62d845e6cca0,
econtext=0x62d845e6c480, isnull=0x7ffcbda7663f) at
execExprInterp.c:987
#5 0x000062d817cd1502 in ExecInterpExprStillValid
(state=0x62d845e6cca0, econtext=0x62d845e6c480, isNull=0x7ffcbda7663f)
at execExprInterp.c:2309
#6 0x000062d817d3bae2 in ExecEvalExprSwitchContext
(state=0x62d845e6cca0, econtext=0x62d845e6c480, isNull=0x7ffcbda7663f)
at ../../../src/include/executor/executor.h:452
#7 0x000062d817d3bc33 in ExecQual (state=0x62d845e6cca0,
econtext=0x62d845e6c480) at
../../../src/include/executor/executor.h:539
#8 0x000062d817d3c01b in ExecScanExtended (node=0x62d845e6c370,
accessMtd=0x62d817d3c08a <SeqNext>, recheckMtd=0x62d817d3c16f
<SeqRecheck>,

The function has a polymorphic argument, so a scalar value such as an
integer is accepted during function resolution. However,
expanded_record_info() passes the argument directly to
DatumGetExpandedRecord() through PG_GETARG_EXPANDED_RECORD(), assuming
that it is a composite Datum. The integer value 2 is consequently treated
as a pointer, causing an access at address 0x2 and crashing the backend.

The same issue exists in expanded_record_identity() and
expanded_record_field_text(), since they make the same assumption about
their polymorphic arguments.

The attached patch checks that the resolved argument type is a row
type before converting the Datum.

--
Thanks,
Tender Wang

Attachments:

t258379_1
0001-Validate-expanded-record-test-function-arguments.patchtext/plain; charset=US-ASCII; name=0001-Validate-expanded-record-test-function-arguments.patchDownload+14-4
#2Joao Rito
joaovictor.rito@gmail.com
In reply to: Tender Wang (#1)
Re: Backend crash in expanded_record_info functions

Hi Tender,

Thank you for the patch and for the clear report. I may be wrong on some points, so please correct me.

I tested this on current master (commit 8303f2e, cassert and debug enabled, in a container). The patch applied cleanly.

Without the patch, `SELECT expanded_record_info(2);` terminated the backend ("server closed the connection unexpectedly"). With the patch, the same statement returns:

ERROR: argument must be of a composite type

and the session stays alive. `make check` passes with the patch (243 tests).

I also tried a few other calls with the patch. `expanded_record_identity(2)`, `expanded_record_field_text(2,1)`, a text argument, and an integer array return the same error. A table row type and a domain over a composite type give the same output with and without the patch. `ROW(1,'a')` also works with the patch (I did not run it without the patch). NULL input of a non-composite type returns NULL, because the function is STRICT in my setup.

One small question. The error message could mention the function name, or use ereport with ERRCODE_DATATYPE_MISMATCH. This is only a test helper, so elog may be fine. I do not have a strong opinion.

What I did not test:
- I did not run this against the real regression database. I created the three functions by hand in an empty database, so their declarations may differ a little from test_setup.sql.
- On unpatched master I confirmed the crash only for `expanded_record_info(2)`. I did not run `expanded_record_identity(2)` and `expanded_record_field_text(2,1)` on unpatched master. I only read the code, which uses the same macro, so I did not see them crash.
- I did not run make check on unpatched master, run other test suites, or test other platforms or build options (no non-cassert build).
- I did not check whether other test functions in regress.c have the same issue.

Thanks again for your work on this.

Best regards,
Joao Rito

#3Tender Wang
tndrwang@gmail.com
In reply to: Joao Rito (#2)
Re: Backend crash in expanded_record_info functions

Hi,

Joao Rito <joaovictor.rito@gmail.com> 于2026年11月19日周四 07:58写道:

Hi Tender,

Thank you for the patch and for the clear report. I may be wrong on some points, so please correct me.

I tested this on current master (commit 8303f2e, cassert and debug enabled, in a container). The patch applied cleanly.

Without the patch, `SELECT expanded_record_info(2);` terminated the backend ("server closed the connection unexpectedly"). With the patch, the same statement returns:

ERROR: argument must be of a composite type

and the session stays alive. `make check` passes with the patch (243 tests).

I also tried a few other calls with the patch. `expanded_record_identity(2)`, `expanded_record_field_text(2,1)`, a text argument, and an integer array return the same error. A table row type and a domain over a composite type give the same output with and without the patch. `ROW(1,'a')` also works with the patch (I did not run it without the patch). NULL input of a non-composite type returns NULL, because the function is STRICT in my setup.

One small question. The error message could mention the function name, or use ereport with ERRCODE_DATATYPE_MISMATCH. This is only a test helper, so elog may be fine. I do not have a strong opinion.

Thanks for the review.
I suspect this case is rare enough in practice that it may not be
worth spending more time on the patch.

--
Tender Wang

#4Michael Paquier
michael@paquier.xyz
In reply to: Tender Wang (#3)
Re: Backend crash in expanded_record_info functions

On Sat, Nov 21, 2026 at 11:33:00AM +0800, Tender Wang wrote:

I suspect this case is rare enough in practice that it may not be
worth spending more time on the patch.

It is a function introduced for the only sake of testing low-level
facilities in the regression tests, for stuff that cannot be reached
via normal SQL.

In short, let's spend time on more useful things, and there is already
more than enough to do. (If a committer is willing to adjust that,
feel free of course.)
--
Michael