Skip to content

Commit

Permalink
Query parsing: fix incompatible objects comparison
Browse files Browse the repository at this point in the history
* When using the queries GJSON syntax #() and #()# ensure any exception
  raised while comparing incompatible objects is catched and raise as a
  GJSONError.
* Found with the Python fuzzing engine Atheris.
  • Loading branch information
volans- committed Jun 11, 2022
1 parent 9850885 commit 2a8ebf5
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 4 deletions.
11 changes: 7 additions & 4 deletions gjson/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -572,10 +572,13 @@ def not_match_op(obj_a: Any, obj_b: Any) -> bool:
return re.match(obj_b, obj_a) is None
oper = not_match_op

if key:
ret = [i for i in obj if key in i and oper(i[key], value)]
else: # Query on an array of non-objects, match them directly
ret = [i for i in obj if oper(i, value)]
try:
if key:
ret = [i for i in obj if key in i and oper(i[key], value)]
else: # Query on an array of non-objects, match them directly
ret = [i for i in obj if oper(i, value)]
except TypeError:
ret = []

if all_items:
return ret
Expand Down
1 change: 1 addition & 0 deletions tests/unit/test_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ def setup_method(self):
('friends.#(first!%"D*").last', 'Craig'),
('friends.#(first!%"D???").last', 'Craig'),
('friends.#(%0)#', []),
('friends.#(>40)#', []),
('children.#(!%"*a*")', 'Alex'),
('children.#(%"*a*")#', ['Sara', 'Jack']),
# Nested queries (TODO)
Expand Down

0 comments on commit 2a8ebf5

Please sign in to comment.