Skip to content

Commit 9a5efa2

Browse files
authored
fix: correct match offset truncation for buffers larger than 4GB (#257)
1 parent eb1cb65 commit 9a5efa2

4 files changed

Lines changed: 49 additions & 2 deletions

File tree

pyproject.toml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,3 +185,8 @@ docs = [
185185
"mkdocs-material>=9.5",
186186
"pymdown-extensions>=10.9",
187187
]
188+
189+
[tool.pytest.ini_options]
190+
markers = [
191+
"slow: marks tests as slow",
192+
]

src/hyperscan/extension.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,7 @@ static int hs_match_handler(
238238
PyGILState_STATE gstate;
239239
gstate = PyGILState_Ensure();
240240
PyObject *rv = PyObject_CallFunction(
241-
cctx->callback, "IIIIO", id, from, to, flags, cctx->ctx);
241+
cctx->callback, "IKKIO", id, from, to, flags, cctx->ctx);
242242
int halt = 1;
243243
if (rv == NULL) {
244244
cctx->success = 0;
@@ -273,7 +273,7 @@ static int ch_match_handler(
273273
}
274274
PyObject *rv = PyObject_CallFunction(
275275
cctx->callback,
276-
"IIIIOO",
276+
"IKKIOO",
277277
id,
278278
from,
279279
to,

tests/conftest.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import pytest
2+
3+
def pytest_addoption(parser):
4+
parser.addoption(
5+
"--run-slow", action="store_true", default=False,
6+
help="Run slow tests"
7+
)
8+
9+
def pytest_collection_modifyitems(config, items):
10+
if not config.getoption("--run-slow"):
11+
skip_slow = pytest.mark.skip(reason="Pass --run-slow to run")
12+
for item in items:
13+
if "slow" in item.keywords:
14+
item.add_marker(skip_slow)

tests/test_hyperscan.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -506,3 +506,31 @@ def on_test_match(pattern_id, from_offset, to_offset, flags, context):
506506

507507
test_db.scan(b"test", match_event_handler=on_test_match)
508508
assert len(test_matches) > 0, "Basic pattern matching should work"
509+
510+
511+
@pytest.mark.slow
512+
def test_vectored_scan_large_offset(database_vector, mocker):
513+
callback = mocker.Mock(return_value=None)
514+
buffers = [
515+
b"x"*(2**32-1),
516+
b"fo"
517+
]
518+
database_vector.scan(buffers, match_event_handler=callback)
519+
callback.assert_has_calls(
520+
[
521+
mocker.call(0,0,2**32+1,0,None),
522+
],
523+
)
524+
525+
526+
@pytest.mark.slow
527+
def test_stream_scan_large_offset(database_stream, mocker):
528+
callback = mocker.Mock(return_value=None)
529+
with database_stream.stream(match_event_handler=callback) as stream:
530+
stream.scan(b"x"*(2**32-1))
531+
stream.scan(b"fo")
532+
callback.assert_has_calls(
533+
[
534+
mocker.call(0,0,2**32+1,0,None)
535+
],
536+
)

0 commit comments

Comments
 (0)