Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitd4f7986

Browse files
committed
Fix copy-paste error in datum_to_jsonb_internal()
Commit3c152a2 mistakenly repeated JSONTYPE_JSON in a condition,omitting JSONTYPE_CAST. As a result, datum_to_jsonb_internal() failedto reject inputs that were casts (e.g., from an enum to json as in theexample 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 checkmeant such casted values weren’t properly rejected as invalid(non-scalar) JSON keys.Reported-by: Maciek Sakrejda <maciek@pganalyze.com>Reviewed-by: Tender Wang <tndrwang@gmail.com>Reviewed-by: Alvaro Herrera <alvherre@alvh.no-ip.org>Reviewed-by: Maciek Sakrejda <maciek@pganalyze.com>Discussion:https://postgr.es/m/CADXhmgTJtJZK9A3Na_ry+Xrq-ghjcejBRhcRMzWZvbd__QdgJA@mail.gmail.comBackpatch-through: 17
1 parent4ecdd41 commitd4f7986

File tree

3 files changed

+24
-1
lines changed

3 files changed

+24
-1
lines changed

‎src/backend/utils/adt/jsonb.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -657,7 +657,7 @@ datum_to_jsonb_internal(Datum val, bool is_null, JsonbInState *result,
657657
tcategory==JSONTYPE_COMPOSITE||
658658
tcategory==JSONTYPE_JSON||
659659
tcategory==JSONTYPE_JSONB||
660-
tcategory==JSONTYPE_JSON))
660+
tcategory==JSONTYPE_CAST))
661661
{
662662
ereport(ERROR,
663663
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),

‎src/test/regress/expected/sqljson.out

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -573,6 +573,18 @@ SELECT JSON_OBJECT(1: 1, '2': NULL, '3': 1, 4: NULL, '5': 'a' ABSENT ON NULL WIT
573573
{"1": 1, "3": 1, "5": "a"}
574574
(1 row)
575575

576+
-- BUG: https://postgr.es/m/CADXhmgTJtJZK9A3Na_ry%2BXrq-ghjcejBRhcRMzWZvbd__QdgJA%40mail.gmail.com
577+
-- datum_to_jsonb_internal() didn't catch keys that are casts instead of a simple scalar
578+
CREATE TYPE mood AS ENUM ('happy', 'sad', 'neutral');
579+
CREATE FUNCTION mood_to_json(mood) RETURNS json AS $$
580+
SELECT to_json($1::text);
581+
$$ LANGUAGE sql IMMUTABLE;
582+
CREATE CAST (mood AS json) WITH FUNCTION mood_to_json(mood) AS IMPLICIT;
583+
SELECT JSON_OBJECT('happy'::mood: '123'::jsonb);
584+
ERROR: key value must be scalar, not array, composite, or json
585+
DROP CAST (mood AS json);
586+
DROP FUNCTION mood_to_json;
587+
DROP TYPE mood;
576588
-- JSON_ARRAY()
577589
SELECT JSON_ARRAY();
578590
json_array

‎src/test/regress/sql/sqljson.sql

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,17 @@ SELECT JSON_OBJECT(1: 1, '2': NULL, '1': 1 ABSENT ON NULL WITH UNIQUE RETURNING
152152
SELECT JSON_OBJECT(1:1,'2':NULL,'1':1 ABSENTONNULL WITHOUT UNIQUE RETURNING jsonb);
153153
SELECT JSON_OBJECT(1:1,'2':NULL,'3':1,4:NULL,'5':'a' ABSENTONNULL WITH UNIQUE RETURNING jsonb);
154154

155+
-- BUG: https://postgr.es/m/CADXhmgTJtJZK9A3Na_ry%2BXrq-ghjcejBRhcRMzWZvbd__QdgJA%40mail.gmail.com
156+
-- datum_to_jsonb_internal() didn't catch keys that are casts instead of a simple scalar
157+
CREATETYPEmoodAS ENUM ('happy','sad','neutral');
158+
CREATEFUNCTIONmood_to_json(mood) RETURNS jsonAS $$
159+
SELECT to_json($1::text);
160+
$$ LANGUAGE sql IMMUTABLE;
161+
CREATE CAST (moodAS json) WITH FUNCTION mood_to_json(mood)AS IMPLICIT;
162+
SELECT JSON_OBJECT('happy'::mood:'123'::jsonb);
163+
DROP CAST (moodAS json);
164+
DROPFUNCTION mood_to_json;
165+
DROPTYPE mood;
155166

156167
-- JSON_ARRAY()
157168
SELECT JSON_ARRAY();

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp