forked frompostgres/postgres
- Notifications
You must be signed in to change notification settings - Fork6
Commit8422656
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.com1 parent6b25c57 commit8422656
File tree
5 files changed
+32
-3
lines changed- src
- backend/utils/adt
- test/regress
- expected
- sql
5 files changed
+32
-3
lines changedLines changed: 17 additions & 3 deletions
Original file line number | Diff line number | Diff line change | |
---|---|---|---|
| |||
1111 | 1111 |
| |
1112 | 1112 |
| |
1113 | 1113 |
| |
1114 |
| - | |
| 1114 | + | |
| 1115 | + | |
| 1116 | + | |
| 1117 | + | |
| 1118 | + | |
| 1119 | + | |
| 1120 | + | |
| 1121 | + | |
1115 | 1122 |
| |
1116 | 1123 |
| |
1117 | 1124 |
| |
| |||
1274 | 1281 |
| |
1275 | 1282 |
| |
1276 | 1283 |
| |
1277 |
| - | |
1278 |
| - | |
| 1284 | + | |
| 1285 | + | |
| 1286 | + | |
| 1287 | + | |
| 1288 | + | |
| 1289 | + | |
| 1290 | + | |
| 1291 | + | |
| 1292 | + | |
1279 | 1293 |
| |
1280 | 1294 |
| |
1281 | 1295 |
| |
|
Lines changed: 3 additions & 0 deletions
Original file line number | Diff line number | Diff line change | |
---|---|---|---|
| |||
2330 | 2330 |
| |
2331 | 2331 |
| |
2332 | 2332 |
| |
| 2333 | + | |
| 2334 | + | |
| 2335 | + | |
2333 | 2336 |
| |
2334 | 2337 |
| |
2335 | 2338 |
| |
|
Lines changed: 5 additions & 0 deletions
Original file line number | Diff line number | Diff line change | |
---|---|---|---|
| |||
537 | 537 |
| |
538 | 538 |
| |
539 | 539 |
| |
| 540 | + | |
| 541 | + | |
540 | 542 |
| |
541 | 543 |
| |
542 | 544 |
| |
| |||
921 | 923 |
| |
922 | 924 |
| |
923 | 925 |
| |
| 926 | + | |
| 927 | + | |
| 928 | + | |
924 | 929 |
| |
925 | 930 |
| |
926 | 931 |
| |
|
Lines changed: 2 additions & 0 deletions
Original file line number | Diff line number | Diff line change | |
---|---|---|---|
| |||
755 | 755 |
| |
756 | 756 |
| |
757 | 757 |
| |
| 758 | + | |
| 759 | + | |
758 | 760 |
| |
759 | 761 |
| |
760 | 762 |
| |
|
Lines changed: 5 additions & 0 deletions
Original file line number | Diff line number | Diff line change | |
---|---|---|---|
| |||
138 | 138 |
| |
139 | 139 |
| |
140 | 140 |
| |
| 141 | + | |
| 142 | + | |
141 | 143 |
| |
142 | 144 |
| |
143 | 145 |
| |
| |||
283 | 285 |
| |
284 | 286 |
| |
285 | 287 |
| |
| 288 | + | |
| 289 | + | |
| 290 | + | |
286 | 291 |
| |
287 | 292 |
| |
288 | 293 |
| |
|
0 commit comments
Comments
(0)