Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 486ea0b

Browse files
committed
Fix crash in json_to_record().
json_to_record() depends on get_call_result_type() for the tuple descriptor of the record that should be returned, but in some cases that cannot be determined. Add a guard to check if the tuple descriptor has been properly resolved, similar to other callers of get_call_result_type(). Also add guard for two other callers of get_call_result_type() in jsonfuncs.c. Although json_to_record() is the only actual bug, it's a good idea to follow convention.
1 parent fccebe4 commit 486ea0b

File tree

1 file changed

+17
-3
lines changed

1 file changed

+17
-3
lines changed

src/backend/utils/adt/jsonfuncs.c

+17-3
Original file line numberDiff line numberDiff line change
@@ -941,7 +941,11 @@ each_worker(PG_FUNCTION_ARGS, bool as_text)
941941

942942
rsi->returnMode = SFRM_Materialize;
943943

944-
(void) get_call_result_type(fcinfo, NULL, &tupdesc);
944+
if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE)
945+
ereport(ERROR,
946+
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
947+
errmsg("function returning record called in context "
948+
"that cannot accept type record")));
945949

946950
/* make these in a sufficiently long-lived memory context */
947951
old_cxt = MemoryContextSwitchTo(rsi->econtext->ecxt_per_query_memory);
@@ -1349,7 +1353,13 @@ populate_record_worker(PG_FUNCTION_ARGS, bool have_record_arg)
13491353

13501354
json = PG_GETARG_TEXT_P(0);
13511355

1352-
get_call_result_type(fcinfo, NULL, &tupdesc);
1356+
if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE)
1357+
ereport(ERROR,
1358+
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1359+
errmsg("function returning record called in context "
1360+
"that cannot accept type record"),
1361+
errhint("Try calling the function in the FROM clause "
1362+
"using a column definition list.")));
13531363
}
13541364

13551365
json_hash = get_json_object_as_hash(json, "json_populate_record",
@@ -1713,7 +1723,11 @@ populate_recordset_worker(PG_FUNCTION_ARGS, bool have_record_arg)
17131723
* get the tupdesc from the result set info - it must be a record type
17141724
* because we already checked that arg1 is a record type.
17151725
*/
1716-
(void) get_call_result_type(fcinfo, NULL, &tupdesc);
1726+
if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE)
1727+
ereport(ERROR,
1728+
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1729+
errmsg("function returning record called in context "
1730+
"that cannot accept type record")));
17171731

17181732
state = palloc0(sizeof(PopulateRecordsetState));
17191733
sem = palloc0(sizeof(JsonSemAction));

0 commit comments

Comments
 (0)