Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 108d2ad

Browse files
Remove dependence on -fwrapv semantics in jsonb.
This commit updates a couple of places in the jsonb code to no longer rely on signed integer wrapping for correctness. Like commit 9e9a2b7, this is intended to move us closer towards removing -fwrapv, which may enable some compiler optimizations. However, there is presently no plan to actually remove that compiler option in the near future. This commit makes use of the newly introduced pg_abs_s32() routine to negate a signed integer (that is known to be negative) for comparison with an unsigned integer. In passing, change one use of INT_MIN to the more portable PG_INT32_MIN. Reported-by: Alexander Lakhin Author: Joseph Koshakow Reviewed-by: Jian He Discussion: https://postgr.es/m/CAAvxfHdBPOyEGS7s%2Bxf4iaW0-cgiq25jpYdWBqQqvLtLe_t6tw%40mail.gmail.com
1 parent 95b856d commit 108d2ad

File tree

3 files changed

+25
-3
lines changed

3 files changed

+25
-3
lines changed

src/backend/utils/adt/jsonfuncs.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
#include "access/htup_details.h"
2020
#include "catalog/pg_type.h"
21+
#include "common/int.h"
2122
#include "common/jsonapi.h"
2223
#include "common/string.h"
2324
#include "fmgr.h"
@@ -946,7 +947,7 @@ jsonb_array_element(PG_FUNCTION_ARGS)
946947
{
947948
uint32 nelements = JB_ROOT_COUNT(jb);
948949

949-
if (-element > nelements)
950+
if (pg_abs_s32(element) > nelements)
950951
PG_RETURN_NULL();
951952
else
952953
element += nelements;
@@ -5426,7 +5427,7 @@ setPathArray(JsonbIterator **it, Datum *path_elems, bool *path_nulls,
54265427

54275428
if (idx < 0)
54285429
{
5429-
if (-idx > nelems)
5430+
if (pg_abs_s32(idx) > nelems)
54305431
{
54315432
/*
54325433
* If asked to keep elements position consistent, it's not allowed
@@ -5438,7 +5439,7 @@ setPathArray(JsonbIterator **it, Datum *path_elems, bool *path_nulls,
54385439
errmsg("path element at position %d is out of range: %d",
54395440
level + 1, idx)));
54405441
else
5441-
idx = INT_MIN;
5442+
idx = PG_INT32_MIN;
54425443
}
54435444
else
54445445
idx = nelems + idx;

src/test/regress/expected/jsonb.out

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -680,6 +680,12 @@ select '"foo"'::jsonb -> 'z';
680680

681681
(1 row)
682682

683+
select '[]'::jsonb -> -2147483648;
684+
?column?
685+
----------
686+
687+
(1 row)
688+
683689
select '{"a": [{"b": "c"}, {"b": "cc"}]}'::jsonb ->> null::text;
684690
?column?
685691
----------
@@ -746,6 +752,12 @@ select '"foo"'::jsonb ->> 'z';
746752

747753
(1 row)
748754

755+
select '[]'::jsonb ->> -2147483648;
756+
?column?
757+
----------
758+
759+
(1 row)
760+
749761
-- equality and inequality
750762
SELECT '{"x":"y"}'::jsonb = '{"x":"y"}'::jsonb;
751763
?column?
@@ -4575,6 +4587,12 @@ select jsonb_delete_path('{"n":null, "a":1, "b":[1,2], "c":{"1":2}, "d":{"1":[2,
45754587
{"a": 1, "b": [1, 2], "c": {"1": 2}, "d": {"1": [3]}, "n": null}
45764588
(1 row)
45774589

4590+
select jsonb_delete_path('{"a":[]}', '{"a",-2147483648}');
4591+
jsonb_delete_path
4592+
-------------------
4593+
{"a": []}
4594+
(1 row)
4595+
45784596
select '{"n":null, "a":1, "b":[1,2], "c":{"1":2}, "d":{"1":[2,3]}}'::jsonb #- '{n}';
45794597
?column?
45804598
----------------------------------------------------------

src/test/regress/sql/jsonb.sql

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,7 @@ select '[{"b": "c"}, {"b": "cc"}]'::jsonb -> 'z';
204204
select '{"a": "c", "b": null}'::jsonb -> 'b';
205205
select '"foo"'::jsonb -> 1;
206206
select '"foo"'::jsonb -> 'z';
207+
select '[]'::jsonb -> -2147483648;
207208

208209
select '{"a": [{"b": "c"}, {"b": "cc"}]}'::jsonb ->> null::text;
209210
select '{"a": [{"b": "c"}, {"b": "cc"}]}'::jsonb ->> null::int;
@@ -216,6 +217,7 @@ select '[{"b": "c"}, {"b": "cc"}]'::jsonb ->> 'z';
216217
select '{"a": "c", "b": null}'::jsonb ->> 'b';
217218
select '"foo"'::jsonb ->> 1;
218219
select '"foo"'::jsonb ->> 'z';
220+
select '[]'::jsonb ->> -2147483648;
219221

220222
-- equality and inequality
221223
SELECT '{"x":"y"}'::jsonb = '{"x":"y"}'::jsonb;
@@ -1185,6 +1187,7 @@ select jsonb_set('{"n":null, "a":1, "b":[1,2], "c":{"1":2}, "d":{"1":[2,3]}}'::j
11851187
select jsonb_delete_path('{"n":null, "a":1, "b":[1,2], "c":{"1":2}, "d":{"1":[2,3]}}', '{n}');
11861188
select jsonb_delete_path('{"n":null, "a":1, "b":[1,2], "c":{"1":2}, "d":{"1":[2,3]}}', '{b,-1}');
11871189
select jsonb_delete_path('{"n":null, "a":1, "b":[1,2], "c":{"1":2}, "d":{"1":[2,3]}}', '{d,1,0}');
1190+
select jsonb_delete_path('{"a":[]}', '{"a",-2147483648}');
11881191

11891192
select '{"n":null, "a":1, "b":[1,2], "c":{"1":2}, "d":{"1":[2,3]}}'::jsonb #- '{n}';
11901193
select '{"n":null, "a":1, "b":[1,2], "c":{"1":2}, "d":{"1":[2,3]}}'::jsonb #- '{b,-1}';

0 commit comments

Comments
 (0)