pg.ddx.io  pgsql-committers@postgresql.org mailing list archive  
help / color / mirror / Atom feed
pgsql: SQL/JSON: Fix ON EMPTY/ERROR state leakage on NULL input
4+ messages / 1 participants
[nested] [flat]

* pgsql: SQL/JSON: Fix ON EMPTY/ERROR state leakage on NULL input
@ 2026-11-04 23:56 Amit Langote <amitlan@postgresql.org>
  0 siblings, 0 replies; 4+ messages in thread

From: Amit Langote @ 2026-11-04 23:56 UTC (permalink / raw)
  To: pgsql-committers@lists.postgresql.org

SQL/JSON: Fix ON EMPTY/ERROR state leakage on NULL input

ExecInitJsonExpr() emitted JUMP_IF_NULL steps to skip the
EEOP_JSONEXPR_PATH step when either the context item or the path
expression was NULL, landing on a CONST step that returned NULL.
However, ExecEvalJsonExprPath(), which implements EEOP_JSONEXPR_PATH,
was also the only place where JsonExprState's empty and error flags
got reset for a new row.  Skipping it left the flags as the previous
row had set them, and since execution then fell through the
ON EMPTY / ON ERROR steps that check those flags, a NULL-input row
returned the previous row's ON EMPTY / ON ERROR expression instead of
NULL, making the result depend on the order of the input rows.

This could only happen when ExecInitJsonExpr() had emitted the
ON EMPTY / ON ERROR steps for the NULL-input row to fall through,
which it does unless the corresponding expression is a NULL constant
and the RETURNING type is not a domain.  So JSON_VALUE and JSON_QUERY
with their default NULL ON EMPTY / NULL ON ERROR behaviors were not
affected, but JSON_EXISTS, whose default is FALSE ON ERROR, was.

Fix by pointing those JUMP_IF_NULL steps at EEOP_JSONEXPR_PATH itself
and dropping the CONST NULL step.  ExecEvalJsonExprPath() now returns
NULL for NULL input after resetting the flags, skipping jsonpath
evaluation and the ON EMPTY / ON ERROR steps as before, but still
jumping to the coercion steps when RETURNING a domain type so that its
constraints are checked.

Oversight in commit 6185c9737cf, so back-patch to 17.

Bug: #19621
Reported-by: Suyang Zhong <syzhong16@gmail.com>
Author: jian he <jian.universality@gmail.com>
Author: Andrey Rachitskiy <pl0h0yp1@gmail.com>
Reviewed-by: Srinath Reddy Sadipiralla <srinath2133@gmail.com>
Tested-by: Manu <manuelreyesbravo@gmail.com>
Discussion: https://postgr.es/m/19621-0a480d2dc74e6bd5@postgresql.org
Backpatch-through: 17

Branch
------
REL_17_STABLE

Details
-------
https://git.postgresql.org/pg/commitdiff/775e03191fa663b523363e6fa1eb7c3f1d565350

Modified Files
--------------
src/backend/executor/execExpr.c                  | 24 ++++----
src/backend/executor/execExprInterp.c            | 34 +++++++++--
src/test/regress/expected/sqljson_queryfuncs.out | 73 ++++++++++++++++++++++++
src/test/regress/sql/sqljson_queryfuncs.sql      | 29 ++++++++++
4 files changed, 141 insertions(+), 19 deletions(-)



^ permalink  raw  reply  [nested|flat] 4+ messages in thread

* pgsql: SQL/JSON: Fix ON EMPTY/ERROR state leakage on NULL input
@ 2026-11-04 23:57 Amit Langote <amitlan@postgresql.org>
  0 siblings, 0 replies; 4+ messages in thread

From: Amit Langote @ 2026-11-04 23:57 UTC (permalink / raw)
  To: pgsql-committers@lists.postgresql.org

SQL/JSON: Fix ON EMPTY/ERROR state leakage on NULL input

ExecInitJsonExpr() emitted JUMP_IF_NULL steps to skip the
EEOP_JSONEXPR_PATH step when either the context item or the path
expression was NULL, landing on a CONST step that returned NULL.
However, ExecEvalJsonExprPath(), which implements EEOP_JSONEXPR_PATH,
was also the only place where JsonExprState's empty and error flags
got reset for a new row.  Skipping it left the flags as the previous
row had set them, and since execution then fell through the
ON EMPTY / ON ERROR steps that check those flags, a NULL-input row
returned the previous row's ON EMPTY / ON ERROR expression instead of
NULL, making the result depend on the order of the input rows.

This could only happen when ExecInitJsonExpr() had emitted the
ON EMPTY / ON ERROR steps for the NULL-input row to fall through,
which it does unless the corresponding expression is a NULL constant
and the RETURNING type is not a domain.  So JSON_VALUE and JSON_QUERY
with their default NULL ON EMPTY / NULL ON ERROR behaviors were not
affected, but JSON_EXISTS, whose default is FALSE ON ERROR, was.

Fix by pointing those JUMP_IF_NULL steps at EEOP_JSONEXPR_PATH itself
and dropping the CONST NULL step.  ExecEvalJsonExprPath() now returns
NULL for NULL input after resetting the flags, skipping jsonpath
evaluation and the ON EMPTY / ON ERROR steps as before, but still
jumping to the coercion steps when RETURNING a domain type so that its
constraints are checked.

Oversight in commit 6185c9737cf, so back-patch to 17.

Bug: #19621
Reported-by: Suyang Zhong <syzhong16@gmail.com>
Author: jian he <jian.universality@gmail.com>
Author: Andrey Rachitskiy <pl0h0yp1@gmail.com>
Reviewed-by: Srinath Reddy Sadipiralla <srinath2133@gmail.com>
Tested-by: Manu <manuelreyesbravo@gmail.com>
Discussion: https://postgr.es/m/19621-0a480d2dc74e6bd5@postgresql.org
Backpatch-through: 17

Branch
------
REL_18_STABLE

Details
-------
https://git.postgresql.org/pg/commitdiff/52033b8871b8ff3c5fe34ffbdef1db38f46586b8

Modified Files
--------------
src/backend/executor/execExpr.c                  | 24 ++++----
src/backend/executor/execExprInterp.c            | 34 +++++++++--
src/test/regress/expected/sqljson_queryfuncs.out | 73 ++++++++++++++++++++++++
src/test/regress/sql/sqljson_queryfuncs.sql      | 29 ++++++++++
4 files changed, 141 insertions(+), 19 deletions(-)



^ permalink  raw  reply  [nested|flat] 4+ messages in thread

* pgsql: SQL/JSON: Fix ON EMPTY/ERROR state leakage on NULL input
@ 2026-11-04 23:57 Amit Langote <amitlan@postgresql.org>
  0 siblings, 0 replies; 4+ messages in thread

From: Amit Langote @ 2026-11-04 23:57 UTC (permalink / raw)
  To: pgsql-committers@lists.postgresql.org

SQL/JSON: Fix ON EMPTY/ERROR state leakage on NULL input

ExecInitJsonExpr() emitted JUMP_IF_NULL steps to skip the
EEOP_JSONEXPR_PATH step when either the context item or the path
expression was NULL, landing on a CONST step that returned NULL.
However, ExecEvalJsonExprPath(), which implements EEOP_JSONEXPR_PATH,
was also the only place where JsonExprState's empty and error flags
got reset for a new row.  Skipping it left the flags as the previous
row had set them, and since execution then fell through the
ON EMPTY / ON ERROR steps that check those flags, a NULL-input row
returned the previous row's ON EMPTY / ON ERROR expression instead of
NULL, making the result depend on the order of the input rows.

This could only happen when ExecInitJsonExpr() had emitted the
ON EMPTY / ON ERROR steps for the NULL-input row to fall through,
which it does unless the corresponding expression is a NULL constant
and the RETURNING type is not a domain.  So JSON_VALUE and JSON_QUERY
with their default NULL ON EMPTY / NULL ON ERROR behaviors were not
affected, but JSON_EXISTS, whose default is FALSE ON ERROR, was.

Fix by pointing those JUMP_IF_NULL steps at EEOP_JSONEXPR_PATH itself
and dropping the CONST NULL step.  ExecEvalJsonExprPath() now returns
NULL for NULL input after resetting the flags, skipping jsonpath
evaluation and the ON EMPTY / ON ERROR steps as before, but still
jumping to the coercion steps when RETURNING a domain type so that its
constraints are checked.

Oversight in commit 6185c9737cf, so back-patch to 17.

Bug: #19621
Reported-by: Suyang Zhong <syzhong16@gmail.com>
Author: jian he <jian.universality@gmail.com>
Author: Andrey Rachitskiy <pl0h0yp1@gmail.com>
Reviewed-by: Srinath Reddy Sadipiralla <srinath2133@gmail.com>
Tested-by: Manu <manuelreyesbravo@gmail.com>
Discussion: https://postgr.es/m/19621-0a480d2dc74e6bd5@postgresql.org
Backpatch-through: 17

Branch
------
REL_19_STABLE

Details
-------
https://git.postgresql.org/pg/commitdiff/8e5c3d3db18dd875846970385a74b05d2d827217

Modified Files
--------------
src/backend/executor/execExpr.c                  | 24 ++++----
src/backend/executor/execExprInterp.c            | 34 +++++++++--
src/test/regress/expected/sqljson_queryfuncs.out | 73 ++++++++++++++++++++++++
src/test/regress/sql/sqljson_queryfuncs.sql      | 29 ++++++++++
4 files changed, 141 insertions(+), 19 deletions(-)



^ permalink  raw  reply  [nested|flat] 4+ messages in thread

* pgsql: SQL/JSON: Fix ON EMPTY/ERROR state leakage on NULL input
@ 2026-11-04 23:57 Amit Langote <amitlan@postgresql.org>
  0 siblings, 0 replies; 4+ messages in thread

From: Amit Langote @ 2026-11-04 23:57 UTC (permalink / raw)
  To: pgsql-committers@lists.postgresql.org

SQL/JSON: Fix ON EMPTY/ERROR state leakage on NULL input

ExecInitJsonExpr() emitted JUMP_IF_NULL steps to skip the
EEOP_JSONEXPR_PATH step when either the context item or the path
expression was NULL, landing on a CONST step that returned NULL.
However, ExecEvalJsonExprPath(), which implements EEOP_JSONEXPR_PATH,
was also the only place where JsonExprState's empty and error flags
got reset for a new row.  Skipping it left the flags as the previous
row had set them, and since execution then fell through the
ON EMPTY / ON ERROR steps that check those flags, a NULL-input row
returned the previous row's ON EMPTY / ON ERROR expression instead of
NULL, making the result depend on the order of the input rows.

This could only happen when ExecInitJsonExpr() had emitted the
ON EMPTY / ON ERROR steps for the NULL-input row to fall through,
which it does unless the corresponding expression is a NULL constant
and the RETURNING type is not a domain.  So JSON_VALUE and JSON_QUERY
with their default NULL ON EMPTY / NULL ON ERROR behaviors were not
affected, but JSON_EXISTS, whose default is FALSE ON ERROR, was.

Fix by pointing those JUMP_IF_NULL steps at EEOP_JSONEXPR_PATH itself
and dropping the CONST NULL step.  ExecEvalJsonExprPath() now returns
NULL for NULL input after resetting the flags, skipping jsonpath
evaluation and the ON EMPTY / ON ERROR steps as before, but still
jumping to the coercion steps when RETURNING a domain type so that its
constraints are checked.

Oversight in commit 6185c9737cf, so back-patch to 17.

Bug: #19621
Reported-by: Suyang Zhong <syzhong16@gmail.com>
Author: jian he <jian.universality@gmail.com>
Author: Andrey Rachitskiy <pl0h0yp1@gmail.com>
Reviewed-by: Srinath Reddy Sadipiralla <srinath2133@gmail.com>
Tested-by: Manu <manuelreyesbravo@gmail.com>
Discussion: https://postgr.es/m/19621-0a480d2dc74e6bd5@postgresql.org
Backpatch-through: 17

Branch
------
master

Details
-------
https://git.postgresql.org/pg/commitdiff/ee7cfb99a34cfba1cffdd456dc269243ddc69f3b

Modified Files
--------------
src/backend/executor/execExpr.c                  | 24 ++++----
src/backend/executor/execExprInterp.c            | 34 +++++++++--
src/test/regress/expected/sqljson_queryfuncs.out | 73 ++++++++++++++++++++++++
src/test/regress/sql/sqljson_queryfuncs.sql      | 29 ++++++++++
4 files changed, 141 insertions(+), 19 deletions(-)



^ permalink  raw  reply  [nested|flat] 4+ messages in thread


end of thread, other threads:[~2026-11-04 23:57 UTC | newest]

Thread overview: 4+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2026-11-04 23:56 pgsql: SQL/JSON: Fix ON EMPTY/ERROR state leakage on NULL input Amit Langote <amitlan@postgresql.org>
2026-11-04 23:57 pgsql: SQL/JSON: Fix ON EMPTY/ERROR state leakage on NULL input Amit Langote <amitlan@postgresql.org>
2026-11-04 23:57 pgsql: SQL/JSON: Fix ON EMPTY/ERROR state leakage on NULL input Amit Langote <amitlan@postgresql.org>
2026-11-04 23:57 pgsql: SQL/JSON: Fix ON EMPTY/ERROR state leakage on NULL input Amit Langote <amitlan@postgresql.org>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox