Subject: pgsql: Fix copy-paste error in datum_to_jsonb_internal()

From: Amit Langote <[email-masked]>
Date: Thu, 13 Mar 2025 00:58:54 +0000

Fix copy-paste error in datum_to_jsonb_internal()

Commit 3c152a27b06 mistakenly repeated JSONTYPE_JSON in a condition,
omitting JSONTYPE_CAST. As a result, datum_to_jsonb_internal() failed
to reject inputs that were casts (e.g., from an enum to json as in the
example below) when used as keys in JSON constructors.

This led to a crash in cases like:

  SELECT JSON_OBJECT('happy'::mood: '123'::jsonb);

where 'happy'::mood is implicitly cast to json. The missing check
meant such casted values weren’t properly rejected as invalid
(non-scalar) JSON keys.

Reported-by: Maciek Sakrejda <[email-masked]>
Reviewed-by: Tender Wang <[email-masked]>
Reviewed-by: Alvaro Herrera <[email-masked]>
Reviewed-by: Maciek Sakrejda <[email-masked]>
Discussion: https://postgr.es/m/[email-masked]
Backpatch-through: 17

Branch
------
REL_17_STABLE

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

Modified Files
--------------
src/backend/utils/adt/jsonb.c         |  2 +-
src/test/regress/expected/sqljson.out | 12 ++++++++++++
src/test/regress/sql/sqljson.sql      | 11 +++++++++++
3 files changed, 24 insertions(+), 1 deletion(-)