Skip to content

Commit 7baf09b

Browse files
Validate schema parameter in params tuple encode/decode functions
encode_params_tuple and decode_params_tuple silently fell through to V0 encoding for any schema value other than 1 (e.g., 2, -1). Now raises EncodeError/DecodeError for unsupported schema values. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent c0181bd commit 7baf09b

2 files changed

Lines changed: 47 additions & 0 deletions

File tree

src/dqlitewire/tuples.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ def encode_params_tuple(params: Sequence[Any], schema: int = 0, buffer_offset: i
3737
message body. Used to compute padding from the absolute position,
3838
matching Go's putNamedValues which pads based on m.Offset.
3939
"""
40+
if schema not in (0, 1):
41+
raise EncodeError(f"Unsupported params tuple schema version: {schema} (expected 0 or 1)")
42+
4043
if not params:
4144
# Go writes nothing for empty params
4245
return b""
@@ -98,6 +101,9 @@ def decode_params_tuple(
98101
buffer_offset: Absolute byte offset where this tuple starts in the
99102
message body. Used for padding calculation matching Go's behavior.
100103
"""
104+
if schema not in (0, 1):
105+
raise DecodeError(f"Unsupported params tuple schema version: {schema} (expected 0 or 1)")
106+
101107
# Go writes nothing for empty params
102108
if len(data) == 0:
103109
if count is not None and count > 0:

tests/test_tuples.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -386,6 +386,47 @@ def test_decode_invalid_type_code_raises_decode_error(self) -> None:
386386
decode_params_tuple(data)
387387

388388

389+
class TestParamsTupleSchemaValidation:
390+
def test_encode_rejects_schema_2(self) -> None:
391+
import pytest
392+
393+
from dqlitewire.exceptions import EncodeError
394+
395+
with pytest.raises(EncodeError, match="Unsupported params tuple schema"):
396+
encode_params_tuple([42], schema=2)
397+
398+
def test_encode_rejects_negative_schema(self) -> None:
399+
import pytest
400+
401+
from dqlitewire.exceptions import EncodeError
402+
403+
with pytest.raises(EncodeError, match="Unsupported params tuple schema"):
404+
encode_params_tuple([42], schema=-1)
405+
406+
def test_decode_rejects_schema_2(self) -> None:
407+
import pytest
408+
409+
from dqlitewire.exceptions import DecodeError
410+
411+
data = encode_params_tuple([42], schema=0)
412+
with pytest.raises(DecodeError, match="Unsupported params tuple schema"):
413+
decode_params_tuple(data, schema=2)
414+
415+
def test_decode_rejects_negative_schema(self) -> None:
416+
import pytest
417+
418+
from dqlitewire.exceptions import DecodeError
419+
420+
data = encode_params_tuple([42], schema=0)
421+
with pytest.raises(DecodeError, match="Unsupported params tuple schema"):
422+
decode_params_tuple(data, schema=-1)
423+
424+
def test_encode_accepts_schema_0_and_1(self) -> None:
425+
"""Schema 0 and 1 should work normally."""
426+
encode_params_tuple([42], schema=0)
427+
encode_params_tuple([42], schema=1)
428+
429+
389430
class TestParamsTupleErrors:
390431
def test_decode_insufficient_data_for_header(self) -> None:
391432
"""Data shorter than 8 bytes should raise DecodeError."""

0 commit comments

Comments
 (0)