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

Commit78bc5f7

Browse files
committed
Fix unique key checks in JSON object constructors
When building a JSON object, the code builds a hash table of keys, toallow checking if the keys are unique. The uniqueness check and addingthe new key happens in json_unique_check_key(), but this assumes thepointer to the key remains valid.Unfortunately, two places passed pointers to keys in a buffer, whilealso appending more data (additional key/value pairs) to the buffer.With enough data the buffer is resized by enlargeStringInfo(), whichcalls repalloc(), invalidating the earlier key pointers.Due to this the uniqueness check may fail with both false negatives andfalse positives, producing JSON objects with duplicate keys or failingto produce a perfectly valid JSON object.This affects multiple functions that enforce uniqueness of keys, allintroduced in PG16 with the new SQL/JSON:- json_object_agg_unique / jsonb_object_agg_unique- json_object / jsonb_objectaggExisting regression tests did not detect the issue, simply because theinitial buffer size is 1024 and the objects were small enough not torequire the repalloc.With a sufficiently large object, AddressSanitizer reported the accessto invalid memory immediately. So would valgrind, of course.Fixed by copying the key into the hash table memory context, and addingregression tests with enough data to repalloc the buffer. Backpatch to16, where the functions were introduced.Reported by Alexander Lakhin. Investigation and initial fix by JunwangZhao, with various improvements and tests by me.Reported-by: Alexander LakhinAuthor: Junwang Zhao, Tomas VondraBackpatch-through: 16Discussion:https://postgr.es/m/18598-3279ed972a2347c7@postgresql.orgDiscussion:https://postgr.es/m/CAEG8a3JjH0ReJF2_O7-8LuEbO69BxPhYeXs95_x7+H9AMWF1gw@mail.gmail.com
1 parent946f150 commit78bc5f7

File tree

5 files changed

+32
-3
lines changed

5 files changed

+32
-3
lines changed

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

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1102,7 +1102,14 @@ json_object_agg_transfn_worker(FunctionCallInfo fcinfo,
11021102

11031103
if (unique_keys)
11041104
{
1105-
constchar*key=&out->data[key_offset];
1105+
/*
1106+
* Copy the key first, instead of pointing into the buffer. It will be
1107+
* added to the hash table, but the buffer may get reallocated as
1108+
* we're appending more data to it. That would invalidate pointers to
1109+
* keys in the current buffer.
1110+
*/
1111+
constchar*key=MemoryContextStrdup(aggcontext,
1112+
&out->data[key_offset]);
11061113

11071114
if (!json_unique_check_key(&state->unique_check.check,key,0))
11081115
ereport(ERROR,
@@ -1265,8 +1272,15 @@ json_build_object_worker(int nargs, const Datum *args, const bool *nulls, const
12651272

12661273
if (unique_keys)
12671274
{
1268-
/* check key uniqueness after key appending */
1269-
constchar*key=&out->data[key_offset];
1275+
/*
1276+
* check key uniqueness after key appending
1277+
*
1278+
* Copy the key first, instead of pointing into the buffer. It
1279+
* will be added to the hash table, but the buffer may get
1280+
* reallocated as we're appending more data to it. That would
1281+
* invalidate pointers to keys in the current buffer.
1282+
*/
1283+
constchar*key=pstrdup(&out->data[key_offset]);
12701284

12711285
if (!json_unique_check_key(&unique_check.check,key,0))
12721286
ereport(ERROR,

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2282,6 +2282,9 @@ select json_object('{a,b,"","d e f"}','{1,2,3,"a b c"}');
22822282
{"a" : "1", "b" : "2", "" : "3", "d e f" : "a b c"}
22832283
(1 row)
22842284

2285+
-- json_object_agg_unique requires unique keys
2286+
select json_object_agg_unique(mod(i,100), i) from generate_series(0, 199) i;
2287+
ERROR: duplicate JSON object key value: "0"
22852288
-- json_to_record and json_to_recordset
22862289
select * from json_to_record('{"a":1,"b":"foo","c":"bar"}')
22872290
as x(a int, b text, d text);

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -537,6 +537,8 @@ SELECT JSON_OBJECT('a': '1', 'b': NULL, 'c': 2 ABSENT ON NULL);
537537
{"a" : "1", "c" : 2}
538538
(1 row)
539539

540+
SELECT JSON_OBJECT(1: 1, '2': NULL, '3': 1, repeat('x', 1000): 1, 2: repeat('a', 100) WITH UNIQUE);
541+
ERROR: duplicate JSON object key value: "2"
540542
SELECT JSON_OBJECT(1: 1, '1': NULL WITH UNIQUE);
541543
ERROR: duplicate JSON object key value: "1"
542544
SELECT JSON_OBJECT(1: 1, '1': NULL ABSENT ON NULL WITH UNIQUE);
@@ -921,6 +923,9 @@ FROM (VALUES (1, 1), (0, NULL),(4, null), (5, null),(6, null),(2, 2)) foo(k, v);
921923
{"1": 1, "2": 2}
922924
(1 row)
923925

926+
SELECT JSON_OBJECTAGG(mod(i,100): (i)::text FORMAT JSON WITH UNIQUE)
927+
FROM generate_series(0, 199) i;
928+
ERROR: duplicate JSON object key value: "0"
924929
-- Test JSON_OBJECT deparsing
925930
EXPLAIN (VERBOSE, COSTS OFF)
926931
SELECT JSON_OBJECT('foo' : '1' FORMAT JSON, 'bar' : 'baz' RETURNING json);

‎src/test/regress/sql/json.sql

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -748,6 +748,8 @@ select json_object('{a,b,NULL,"d e f"}','{1,2,3,"a b c"}');
748748

749749
select json_object('{a,b,"","d e f"}','{1,2,3,"a b c"}');
750750

751+
-- json_object_agg_unique requires unique keys
752+
select json_object_agg_unique(mod(i,100), i)from generate_series(0,199) i;
751753

752754
-- json_to_record and json_to_recordset
753755

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,8 @@ SELECT JSON_OBJECT('a': '1', 'b': NULL, 'c': 2);
138138
SELECT JSON_OBJECT('a':'1','b':NULL,'c':2NULLONNULL);
139139
SELECT JSON_OBJECT('a':'1','b':NULL,'c':2 ABSENTONNULL);
140140

141+
SELECT JSON_OBJECT(1:1,'2':NULL,'3':1, repeat('x',1000):1,2: repeat('a',100) WITH UNIQUE);
142+
141143
SELECT JSON_OBJECT(1:1,'1':NULL WITH UNIQUE);
142144
SELECT JSON_OBJECT(1:1,'1':NULL ABSENTONNULL WITH UNIQUE);
143145
SELECT JSON_OBJECT(1:1,'1':NULLNULLONNULL WITH UNIQUE RETURNING jsonb);
@@ -283,6 +285,9 @@ FROM (VALUES (1, 1), (1, NULL), (2, 2)) foo(k, v);
283285
SELECT JSON_OBJECTAGG(k: v ABSENTONNULL WITH UNIQUE KEYS RETURNING jsonb)
284286
FROM (VALUES (1,1), (0,NULL),(4,null), (5,null),(6,null),(2,2)) foo(k, v);
285287

288+
SELECT JSON_OBJECTAGG(mod(i,100): (i)::text FORMAT JSON WITH UNIQUE)
289+
FROM generate_series(0,199) i;
290+
286291
-- Test JSON_OBJECT deparsing
287292
EXPLAIN (VERBOSE, COSTS OFF)
288293
SELECT JSON_OBJECT('foo' :'1' FORMAT JSON,'bar' :'baz' RETURNING json);

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp