From 7d3648d3d3fbe836e03fe587b33c26e39403b934 Mon Sep 17 00:00:00 2001 From: Tanzeel Khan <140405735+tanscorpio7@users.noreply.github.com> Date: Thu, 2 Jan 2025 18:34:24 +0530 Subject: [PATCH] Fix crash due to null current memory context after error in openjson WITH (#3339) Any variable we set in try, may not be visible to catch block. Fix such a case where we set oldcontext variable in try block the use it in catch block, which leads to setting current memory context as NULL. Task: BABEL-5020 Signed-off-by: Tanzeel Khan --- contrib/babelfishpg_tsql/src/json_funcs.c | 5 +++-- test/JDBC/expected/BABEL-OPENJSON.out | 19 +++++++++++++++++++ test/JDBC/input/BABEL-OPENJSON.sql | 15 +++++++++++++++ 3 files changed, 37 insertions(+), 2 deletions(-) diff --git a/contrib/babelfishpg_tsql/src/json_funcs.c b/contrib/babelfishpg_tsql/src/json_funcs.c index ffabf0fe86..932d332d84 100644 --- a/contrib/babelfishpg_tsql/src/json_funcs.c +++ b/contrib/babelfishpg_tsql/src/json_funcs.c @@ -306,6 +306,7 @@ tsql_openjson_with_internal(PG_FUNCTION_ARGS) funcctx = SRF_FIRSTCALL_INIT(); prev_sql_dialect = sql_dialect; + oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx); PG_TRY(); { Jsonb *sub_jb; @@ -317,7 +318,6 @@ tsql_openjson_with_internal(PG_FUNCTION_ARGS) * processing */ sql_dialect = SQL_DIALECT_TSQL; - oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx); /* * Get information about return type. Used to build return message @@ -377,9 +377,10 @@ tsql_openjson_with_internal(PG_FUNCTION_ARGS) PG_FINALLY(); { sql_dialect = prev_sql_dialect; - MemoryContextSwitchTo(oldcontext); } PG_END_TRY(); + + MemoryContextSwitchTo(oldcontext); } funcctx = SRF_PERCALL_SETUP(); diff --git a/test/JDBC/expected/BABEL-OPENJSON.out b/test/JDBC/expected/BABEL-OPENJSON.out index 6dfe5f91c5..5cc6d1cf4b 100644 --- a/test/JDBC/expected/BABEL-OPENJSON.out +++ b/test/JDBC/expected/BABEL-OPENJSON.out @@ -316,3 +316,22 @@ int#!#nvarchar#!#nvarchar#!#int#!#datetime2 5#!#Jane#!#Smith#!##!#2005-11-04 12:00:00.0000000 ~~END~~ + +-- BABEL-5020 +CREATE PROC babel_5020_p +AS +SELECT * INTO babel_5020_t FROM openjson(N'{"a":1}', 'strict $.a') with (a nvarchar(20)) +GO + +EXEC babel_5020_p +GO +~~ERROR (Code: 33557097)~~ + +~~ERROR (Message: Value referenced by JSON path is not an array or object and cannot be opened with OPENJSON.)~~ + + +DROP PROC babel_5020_p +GO + +DROP TABLE IF EXISTS babel_5020_t +GO diff --git a/test/JDBC/input/BABEL-OPENJSON.sql b/test/JDBC/input/BABEL-OPENJSON.sql index 702d86b1e7..3a3fcf56c4 100644 --- a/test/JDBC/input/BABEL-OPENJSON.sql +++ b/test/JDBC/input/BABEL-OPENJSON.sql @@ -139,3 +139,18 @@ SELECT * FROM OPENJSON(@json) WITH ( id INT 'strict $.id', firstName NVARCHAR(50) '$.info.name', lastName NVARCHAR(50) '$.info.surname', age INT, dateOfBirth DATETIME2 '$.dob' ); go + +-- BABEL-5020 +CREATE PROC babel_5020_p +AS +SELECT * INTO babel_5020_t FROM openjson(N'{"a":1}', 'strict $.a') with (a nvarchar(20)) +GO + +EXEC babel_5020_p +GO + +DROP PROC babel_5020_p +GO + +DROP TABLE IF EXISTS babel_5020_t +GO \ No newline at end of file