Skip to content

Commit 4449420

Browse files
committed
Explicitly error when length of a searched term is larger than max
* bdx/index.py (DatabaseField.make_query): Raise an error when term is too long. * tests/test_index.py (test_searching_long_name): New test.
1 parent 4356b7f commit 4449420

File tree

2 files changed

+13
-0
lines changed

2 files changed

+13
-0
lines changed

bdx/index.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,13 @@ def make_query(self, value: str, wildcard: bool = False) -> xapian.Query:
9898
"""
9999
value = self.preprocess_value(value).decode()
100100
term = f"{self.prefix}{value}"
101+
if len(term) > MAX_TERM_SIZE:
102+
msg = (
103+
f"Term for '{self.name}' field is too long, max size "
104+
f"is {MAX_TERM_SIZE-len(self.prefix)}: '{value[:30]}'..."
105+
)
106+
raise ValueError(msg)
107+
101108
if wildcard:
102109
return xapian.Query(
103110
xapian.Query.OP_WILDCARD,

tests/test_index.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
SymbolNameField,
1313
index_binary_directory,
1414
)
15+
from bdx.query_parser import QueryParser
1516

1617
# isort: on
1718

@@ -415,6 +416,11 @@ def test_searching_by_basename(fixture_path, readonly_index):
415416
assert sym.path != fixture_path / "subdir" / "bar.cpp.o"
416417

417418

419+
def test_searching_long_name(fixture_path, readonly_index):
420+
with pytest.raises(QueryParser.Error, match="'name'.*too long"):
421+
readonly_index.search("a" * 300)
422+
423+
418424
def test_searching_cxx(readonly_index):
419425
symbols = readonly_index.search("cxx func")
420426
by_name = {x.name: x for x in symbols}

0 commit comments

Comments
 (0)