Skip to content

Commit 918f48d

Browse files
Validate type codes against ValueType in encode_row_header
Previously only checked that type codes fit in 4 bits (<=15). Now also validates they are defined ValueType members, catching undefined codes like 0, 6, 7, 8, etc. that would fail on decode. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent 871f4ee commit 918f48d

2 files changed

Lines changed: 23 additions & 0 deletions

File tree

src/dqlitewire/tuples.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,9 +163,12 @@ def encode_row_header(types: Sequence[ValueType]) -> bytes:
163163
if not types:
164164
return b""
165165

166+
_valid_type_codes = frozenset(ValueType)
166167
for i, t in enumerate(types):
167168
if int(t) > 15:
168169
raise EncodeError(f"Value type {t} at index {i} exceeds 4-bit nibble range (max 15)")
170+
if int(t) not in _valid_type_codes:
171+
raise EncodeError(f"Invalid type code {int(t)} at index {i}: not a valid ValueType")
169172

170173
header = bytearray()
171174
for i in range(0, len(types), 2):

tests/test_tuples.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -365,6 +365,26 @@ def test_decode_insufficient_data(self) -> None:
365365
with pytest.raises(DecodeError, match="Not enough data for row header"):
366366
decode_row_header(b"\x01\x02\x03\x04", 1)
367367

368+
def test_encode_rejects_type_code_zero(self) -> None:
369+
"""encode_row_header should reject type code 0, which is not a valid ValueType."""
370+
import pytest
371+
372+
from dqlitewire.exceptions import EncodeError
373+
374+
with pytest.raises(EncodeError, match="[Ii]nvalid.*type"):
375+
encode_row_header([0])
376+
377+
def test_encode_rejects_undefined_type_codes(self) -> None:
378+
"""encode_row_header should reject type codes not defined in ValueType."""
379+
import pytest
380+
381+
from dqlitewire.exceptions import EncodeError
382+
383+
# Type codes 6, 7, 8 are undefined
384+
for code in [6, 7, 8, 12, 13, 14, 15]:
385+
with pytest.raises(EncodeError, match="[Ii]nvalid.*type"):
386+
encode_row_header([code])
387+
368388

369389
class TestRowValuesBlob:
370390
def test_roundtrip_with_blob(self) -> None:

0 commit comments

Comments
 (0)