Skip to content

Commit 16374c3

Browse files
authored
Fix issue #1389 - Server crash on using null operand for access operators (#1390)
- The issue was due to code not handling null values as LHS operands for access operators(->, ->>) Added check for this case and included regression tests
1 parent 689fa75 commit 16374c3

File tree

3 files changed

+68
-3
lines changed

3 files changed

+68
-3
lines changed

regress/expected/jsonb_operators.out

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -938,6 +938,30 @@ SELECT '{"a": 9, "b": 11, "c": {"ca": [[], {}, null]}, "d": true, "1": false}'::
938938

939939
(1 row)
940940

941+
SELECT '{"n":null,"a":1,"b":[1,2],"c":{"1":2},"automatically":{"1":[2,3]}}'::agtype -> '"n"'::agtype -> '"1"'::agtype;
942+
?column?
943+
----------
944+
945+
(1 row)
946+
947+
SELECT '{"n":null,"a":1,"b":[1,2],"c":{"1":2},"automatically":{"1":[2,3]}}'::agtype -> '"n"'::agtype -> 1::text;
948+
?column?
949+
----------
950+
951+
(1 row)
952+
953+
SELECT '{"n":null,"a":1,"b":[1,2],"c":{"1":2},"automatically":{"1":[2,3]}}'::agtype -> '"n"'::agtype -> '"a"';
954+
?column?
955+
----------
956+
957+
(1 row)
958+
959+
SELECT 'null'::agtype -> '"1"';
960+
?column?
961+
----------
962+
963+
(1 row)
964+
941965
-- LHS is an array
942966
SELECT '["a","b","c",[1,2],null]'::agtype -> '0'::agtype;
943967
?column?
@@ -1241,6 +1265,30 @@ SELECT '{"a": [{"b": "c"}, {"b": "cc"}]}'::agtype ->> null::int;
12411265

12421266
(1 row)
12431267

1268+
SELECT '{"n":null,"a":1,"b":[1,2],"c":{"1":2},"automatically":{"1":[2,3]}}'::agtype -> '"n"'::agtype ->> '"1"'::agtype;
1269+
?column?
1270+
----------
1271+
1272+
(1 row)
1273+
1274+
SELECT '{"n":null,"a":1,"b":[1,2],"c":{"1":2},"automatically":{"1":[2,3]}}'::agtype -> '"n"'::agtype ->> 1::text;
1275+
?column?
1276+
----------
1277+
1278+
(1 row)
1279+
1280+
SELECT '{"n":null,"a":1,"b":[1,2],"c":{"1":2},"automatically":{"1":[2,3]}}'::agtype -> '"n"'::agtype ->> '"a"';
1281+
?column?
1282+
----------
1283+
1284+
(1 row)
1285+
1286+
SELECT 'null'::agtype ->> '"1"';
1287+
?column?
1288+
----------
1289+
1290+
(1 row)
1291+
12441292
-- LHS is an array
12451293
SELECT '["a","b","c",[1,2],null]'::agtype ->> 0;
12461294
?column?

regress/sql/jsonb_operators.sql

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,12 @@ SELECT '{"a": [{"b": "c"}, {"b": "cc"}]}'::agtype -> null::int;
223223
SELECT '{"a": [-1, -2, -3]}'::agtype -> '"a"'::text;
224224
SELECT '{"a": 9, "b": 11, "c": {"ca": [[], {}, null]}, "d": true, "1": false}'::agtype -> '1'::text::agtype;
225225

226+
SELECT '{"n":null,"a":1,"b":[1,2],"c":{"1":2},"automatically":{"1":[2,3]}}'::agtype -> '"n"'::agtype -> '"1"'::agtype;
227+
SELECT '{"n":null,"a":1,"b":[1,2],"c":{"1":2},"automatically":{"1":[2,3]}}'::agtype -> '"n"'::agtype -> 1::text;
228+
SELECT '{"n":null,"a":1,"b":[1,2],"c":{"1":2},"automatically":{"1":[2,3]}}'::agtype -> '"n"'::agtype -> '"a"';
229+
230+
SELECT 'null'::agtype -> '"1"';
231+
226232
-- LHS is an array
227233
SELECT '["a","b","c",[1,2],null]'::agtype -> '0'::agtype;
228234
SELECT '["a","b","c",[1,2],null]'::agtype -> 1;
@@ -288,6 +294,12 @@ SELECT '{"1": -1.99, "a": 1, "b": 2, "c": {"d": [{}, [[[], [9]]]]}, "1": true}':
288294
SELECT '{"a": [{"b": "c"}, {"b": "cc"}]}'::agtype ->> null::text;
289295
SELECT '{"a": [{"b": "c"}, {"b": "cc"}]}'::agtype ->> null::int;
290296

297+
SELECT '{"n":null,"a":1,"b":[1,2],"c":{"1":2},"automatically":{"1":[2,3]}}'::agtype -> '"n"'::agtype ->> '"1"'::agtype;
298+
SELECT '{"n":null,"a":1,"b":[1,2],"c":{"1":2},"automatically":{"1":[2,3]}}'::agtype -> '"n"'::agtype ->> 1::text;
299+
SELECT '{"n":null,"a":1,"b":[1,2],"c":{"1":2},"automatically":{"1":[2,3]}}'::agtype -> '"n"'::agtype ->> '"a"';
300+
301+
SELECT 'null'::agtype ->> '"1"';
302+
291303
-- LHS is an array
292304
SELECT '["a","b","c",[1,2],null]'::agtype ->> 0;
293305
SELECT '["a","b","c",[1,2],null]'::agtype ->> '1'::agtype;

src/backend/utils/adt/agtype.c

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3644,9 +3644,14 @@ Datum agtype_object_field_impl(FunctionCallInfo fcinfo, agtype *agtype_in,
36443644

36453645
if (AGT_ROOT_IS_SCALAR(agtype_in))
36463646
{
3647-
process_agtype =
3648-
agtype_value_to_agtype(extract_entity_properties(agtype_in,
3649-
false));
3647+
agtype_value *process_agtv = extract_entity_properties(agtype_in,
3648+
false);
3649+
if (!process_agtv)
3650+
{
3651+
PG_RETURN_NULL();
3652+
}
3653+
3654+
process_agtype = agtype_value_to_agtype(process_agtv);
36503655
}
36513656
else
36523657
{

0 commit comments

Comments
 (0)