Skip to content

Commit db00be6

Browse files
committed
Don't crash if cursor_to_xmlschema is used on a non-data-returning Portal.
cursor_to_xmlschema() assumed that any Portal must have a tupDesc, which is not so. Add a defensive check. It's plausible that this mistake occurred because of the rather poorly chosen name of the lookup function SPI_cursor_find(), which in such cases is returning something that isn't very much like a cursor. Add some documentation to try to forestall future errors of the same ilk. Report and patch by Boyu Yang (docs changes by me). Back-patch to all supported branches. Discussion: https://postgr.es/m/dd343010-c637-434c-a8cb-418f53bda3b8.yangboyu.yby@alibaba-inc.com
1 parent a374f6c commit db00be6

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed

doc/src/sgml/spi.sgml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2083,6 +2083,19 @@ Portal SPI_cursor_find(const char * <parameter>name</parameter>)
20832083
<symbol>NULL</symbol> if none was found
20842084
</para>
20852085
</refsect1>
2086+
2087+
<refsect1>
2088+
<title>Notes</title>
2089+
2090+
<para>
2091+
Beware that this function can return a <type>Portal</type> object
2092+
that does not have cursor-like properties; for example it might not
2093+
return tuples. If you simply pass the <type>Portal</type> pointer
2094+
to other SPI functions, they can defend themselves against such
2095+
cases, but caution is appropriate when directly inspecting
2096+
the <type>Portal</type>.
2097+
</para>
2098+
</refsect1>
20862099
</refentry>
20872100

20882101
<!-- *********************************************** -->

src/backend/utils/adt/xml.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2745,6 +2745,10 @@ cursor_to_xmlschema(PG_FUNCTION_ARGS)
27452745
ereport(ERROR,
27462746
(errcode(ERRCODE_UNDEFINED_CURSOR),
27472747
errmsg("cursor \"%s\" does not exist", name)));
2748+
if (portal->tupDesc == NULL)
2749+
ereport(ERROR,
2750+
(errcode(ERRCODE_INVALID_CURSOR_STATE),
2751+
errmsg("portal \"%s\" does not return tuples", name)));
27482752

27492753
xmlschema = _SPI_strdup(map_sql_table_to_xmlschema(portal->tupDesc,
27502754
InvalidOid, nulls,

0 commit comments

Comments
 (0)