Skip to content

Commit

Permalink
Fix issue #1389 - Server crash on using null operand for access opera…
Browse files Browse the repository at this point in the history
…tors (#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
  • Loading branch information
Zainab-Saad authored and jrgemignani committed Dec 13, 2023
1 parent 125ede2 commit 0f01e27
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 3 deletions.
48 changes: 48 additions & 0 deletions regress/expected/jsonb_operators.out
Original file line number Diff line number Diff line change
Expand Up @@ -938,6 +938,30 @@ SELECT '{"a": 9, "b": 11, "c": {"ca": [[], {}, null]}, "d": true, "1": false}'::

(1 row)

SELECT '{"n":null,"a":1,"b":[1,2],"c":{"1":2},"automatically":{"1":[2,3]}}'::agtype -> '"n"'::agtype -> '"1"'::agtype;
?column?
----------

(1 row)

SELECT '{"n":null,"a":1,"b":[1,2],"c":{"1":2},"automatically":{"1":[2,3]}}'::agtype -> '"n"'::agtype -> 1::text;
?column?
----------

(1 row)

SELECT '{"n":null,"a":1,"b":[1,2],"c":{"1":2},"automatically":{"1":[2,3]}}'::agtype -> '"n"'::agtype -> '"a"';
?column?
----------

(1 row)

SELECT 'null'::agtype -> '"1"';
?column?
----------

(1 row)

-- LHS is an array
SELECT '["a","b","c",[1,2],null]'::agtype -> '0'::agtype;
?column?
Expand Down Expand Up @@ -1241,6 +1265,30 @@ SELECT '{"a": [{"b": "c"}, {"b": "cc"}]}'::agtype ->> null::int;

(1 row)

SELECT '{"n":null,"a":1,"b":[1,2],"c":{"1":2},"automatically":{"1":[2,3]}}'::agtype -> '"n"'::agtype ->> '"1"'::agtype;
?column?
----------

(1 row)

SELECT '{"n":null,"a":1,"b":[1,2],"c":{"1":2},"automatically":{"1":[2,3]}}'::agtype -> '"n"'::agtype ->> 1::text;
?column?
----------

(1 row)

SELECT '{"n":null,"a":1,"b":[1,2],"c":{"1":2},"automatically":{"1":[2,3]}}'::agtype -> '"n"'::agtype ->> '"a"';
?column?
----------

(1 row)

SELECT 'null'::agtype ->> '"1"';
?column?
----------

(1 row)

-- LHS is an array
SELECT '["a","b","c",[1,2],null]'::agtype ->> 0;
?column?
Expand Down
12 changes: 12 additions & 0 deletions regress/sql/jsonb_operators.sql
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,12 @@ SELECT '{"a": [{"b": "c"}, {"b": "cc"}]}'::agtype -> null::int;
SELECT '{"a": [-1, -2, -3]}'::agtype -> '"a"'::text;
SELECT '{"a": 9, "b": 11, "c": {"ca": [[], {}, null]}, "d": true, "1": false}'::agtype -> '1'::text::agtype;

SELECT '{"n":null,"a":1,"b":[1,2],"c":{"1":2},"automatically":{"1":[2,3]}}'::agtype -> '"n"'::agtype -> '"1"'::agtype;
SELECT '{"n":null,"a":1,"b":[1,2],"c":{"1":2},"automatically":{"1":[2,3]}}'::agtype -> '"n"'::agtype -> 1::text;
SELECT '{"n":null,"a":1,"b":[1,2],"c":{"1":2},"automatically":{"1":[2,3]}}'::agtype -> '"n"'::agtype -> '"a"';

SELECT 'null'::agtype -> '"1"';

-- LHS is an array
SELECT '["a","b","c",[1,2],null]'::agtype -> '0'::agtype;
SELECT '["a","b","c",[1,2],null]'::agtype -> 1;
Expand Down Expand Up @@ -288,6 +294,12 @@ SELECT '{"1": -1.99, "a": 1, "b": 2, "c": {"d": [{}, [[[], [9]]]]}, "1": true}':
SELECT '{"a": [{"b": "c"}, {"b": "cc"}]}'::agtype ->> null::text;
SELECT '{"a": [{"b": "c"}, {"b": "cc"}]}'::agtype ->> null::int;

SELECT '{"n":null,"a":1,"b":[1,2],"c":{"1":2},"automatically":{"1":[2,3]}}'::agtype -> '"n"'::agtype ->> '"1"'::agtype;
SELECT '{"n":null,"a":1,"b":[1,2],"c":{"1":2},"automatically":{"1":[2,3]}}'::agtype -> '"n"'::agtype ->> 1::text;
SELECT '{"n":null,"a":1,"b":[1,2],"c":{"1":2},"automatically":{"1":[2,3]}}'::agtype -> '"n"'::agtype ->> '"a"';

SELECT 'null'::agtype ->> '"1"';

-- LHS is an array
SELECT '["a","b","c",[1,2],null]'::agtype ->> 0;
SELECT '["a","b","c",[1,2],null]'::agtype ->> '1'::agtype;
Expand Down
11 changes: 8 additions & 3 deletions src/backend/utils/adt/agtype.c
Original file line number Diff line number Diff line change
Expand Up @@ -3644,9 +3644,14 @@ Datum agtype_object_field_impl(FunctionCallInfo fcinfo, agtype *agtype_in,

if (AGT_ROOT_IS_SCALAR(agtype_in))
{
process_agtype =
agtype_value_to_agtype(extract_entity_properties(agtype_in,
false));
agtype_value *process_agtv = extract_entity_properties(agtype_in,
false);
if (!process_agtv)
{
PG_RETURN_NULL();
}

process_agtype = agtype_value_to_agtype(process_agtv);
}
else
{
Expand Down

0 comments on commit 0f01e27

Please sign in to comment.