postgres.git / summary / log / commit / refs
commit 4a3177299f678043e36f6fad075ce3d69301b4a5
Author: John Naylor <john.naylor@postgresql.org>
Date: Mon Oct 19 04:29:41 2026 +0000
First released in: 14.25, 15.20, 16.16, 17.12, 18.7, 19.0
Fix use-after-free in jsonb_set_lax for Const NULL new_value
Commit a83586b5543 decided to scribble on the caller's fcinfo but failed
to take into account that const arguments are written once at expression
init and assumed never to change.
The use_json_null branch overwrote fcinfo->args[2] with a jsonb null
built in the per-tuple expression context. In later rows,
args[2].isnull is still false and args[2].value still holds that
pointer, but the per-tuple context has been reset. Later rows find
args[2].isnull = false, short-circuit past the NULL handling at the top
of the function, and jump straight to jsonb_set with the stale pointer
regardless of their own null_value_treatment argument, which is never
read.
To fix, call jsonb_set through its own call frame instead. Arguments 0,
1 and 3 were already checked non-NULL above, so we can use
DirectFunctionCall4.
Add regression tests covering use_json_null followed by delete_key and
return_target, and a multi-row default-treatment query.
Reported-by: Evan Si <evsi@amazon.com>
Discussion: https://postgr.es/m/CANWCAZY-PS=ev8TiiV1j=NoR2xLopEL8fDq0fyQZahHiP5b+Pw@mail.gmail.com
Backpatch-through: 14
src/backend/utils/adt/jsonfuncs.c | 8 +++++---
src/test/regress/expected/jsonb.out | 28 ++++++++++++++++++++++++++++
src/test/regress/sql/jsonb.sql | 10 ++++++++++
3 files changed, 43 insertions(+), 3 deletions(-)
diff --git a/src/backend/utils/adt/jsonfuncs.c b/src/backend/utils/adt/jsonfuncs.c
index 42b886c621a..d97af3788fa 100644
--- a/src/backend/utils/adt/jsonfuncs.c
+++ b/src/backend/utils/adt/jsonfuncs.c
@@ -4654,9 +4654,11 @@ jsonb_set_lax(PG_FUNCTION_ARGS)
newval = DirectFunctionCall1(jsonb_in, CStringGetDatum("null"));
- fcinfo->args[2].value = newval;
- fcinfo->args[2].isnull = false;
- return jsonb_set(fcinfo);
+ return DirectFunctionCall4(jsonb_set,
+ PG_GETARG_DATUM(0),
+ PG_GETARG_DATUM(1),
+ newval,
+ PG_GETARG_DATUM(3));
}
else if (strcmp(handle_val, "delete_key") == 0)
{
diff --git a/src/test/regress/expected/jsonb.out b/src/test/regress/expected/jsonb.out
index 78f49b25964..8c0c9394e35 100644
--- a/src/test/regress/expected/jsonb.out
+++ b/src/test/regress/expected/jsonb.out
@@ -4699,6 +4699,34 @@ select jsonb_set_lax('{"a":1,"b":2}', '{b}', null, null_value_treatment => 'use_
{"a": 1, "b": null}
(1 row)
+-- multi-row cases with a constant NULL new_value
+select jsonb_set_lax(j, '{a}', null, true, t)
+from (values ('{"a":1}'::jsonb, 'use_json_null'),
+ ('{"a":1}'::jsonb, 'delete_key')) v(j, t);
+ jsonb_set_lax
+---------------
+ {"a": null}
+ {}
+(2 rows)
+
+select jsonb_set_lax(j, '{a}', null, true, t)
+from (values ('{"a":1}'::jsonb, 'use_json_null'),
+ ('{"a":1}'::jsonb, 'return_target')) v(j, t);
+ jsonb_set_lax
+---------------
+ {"a": null}
+ {"a": 1}
+(2 rows)
+
+select jsonb_set_lax(j, '{a}', null)
+from (values ('{"a":1}'::jsonb), ('{"a":2}'::jsonb), ('{"a":3}'::jsonb)) v(j);
+ jsonb_set_lax
+---------------
+ {"a": null}
+ {"a": null}
+ {"a": null}
+(3 rows)
+
\pset null ''
-- jsonb_insert
select jsonb_insert('{"a": [0,1,2]}', '{a, 1}', '"new_value"');
diff --git a/src/test/regress/sql/jsonb.sql b/src/test/regress/sql/jsonb.sql
index 0b02b9c2783..cc05a351663 100644
--- a/src/test/regress/sql/jsonb.sql
+++ b/src/test/regress/sql/jsonb.sql
@@ -1218,6 +1218,16 @@ select jsonb_set_lax('{"a":1,"b":2}', '{b}', null, null_value_treatment => 'retu
select jsonb_set_lax('{"a":1,"b":2}', '{b}', null, null_value_treatment => 'delete_key') as delete_key;
select jsonb_set_lax('{"a":1,"b":2}', '{b}', null, null_value_treatment => 'use_json_null') as use_json_null;
+-- multi-row cases with a constant NULL new_value
+select jsonb_set_lax(j, '{a}', null, true, t)
+from (values ('{"a":1}'::jsonb, 'use_json_null'),
+ ('{"a":1}'::jsonb, 'delete_key')) v(j, t);
+select jsonb_set_lax(j, '{a}', null, true, t)
+from (values ('{"a":1}'::jsonb, 'use_json_null'),
+ ('{"a":1}'::jsonb, 'return_target')) v(j, t);
+select jsonb_set_lax(j, '{a}', null)
+from (values ('{"a":1}'::jsonb), ('{"a":2}'::jsonb), ('{"a":3}'::jsonb)) v(j);
+
\pset null ''
-- jsonb_insert
[parent: d87257b2cddc]