Skip to content

Commit c0181bd

Browse files
Reject non-None values when encoding with explicit ValueType.NULL
encode_value() with an explicit ValueType.NULL silently discarded non-None values, returning 8 zero bytes. Since value=None is already caught at the top of the function, reaching the NULL branch with a non-None value is always a caller bug. Now raises EncodeError instead of silently losing data. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent f5dda2a commit c0181bd

2 files changed

Lines changed: 15 additions & 1 deletion

File tree

src/dqlitewire/types.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,12 @@ def encode_value(value: Any, value_type: ValueType | None = None) -> tuple[bytes
215215
raise EncodeError(f"Expected bytes for BLOB, got {type(value).__name__}")
216216
return encode_blob(bytes(value)), value_type
217217
elif value_type == ValueType.NULL:
218-
return b"\x00" * 8, value_type
218+
# value is None is already handled at line 157-158 above, so reaching
219+
# here means value is not None with explicit NULL type — always a bug.
220+
raise EncodeError(
221+
f"Cannot encode non-None value {value!r} as NULL. "
222+
f"Pass value=None or use the appropriate ValueType."
223+
)
219224
else:
220225
raise EncodeError(f"Unknown value type: {value_type}")
221226

tests/test_types.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -682,3 +682,12 @@ def test_decode_null_truncated(self) -> None:
682682
def test_decode_null_empty(self) -> None:
683683
with pytest.raises(DecodeError, match="8 bytes"):
684684
decode_value(b"", ValueType.NULL)
685+
686+
def test_encode_value_null_type_with_non_none_raises(self) -> None:
687+
"""Explicit ValueType.NULL with a non-None value should raise EncodeError."""
688+
with pytest.raises(EncodeError, match="Cannot encode non-None value"):
689+
encode_value(42, ValueType.NULL)
690+
691+
def test_encode_value_null_type_with_string_raises(self) -> None:
692+
with pytest.raises(EncodeError, match="Cannot encode non-None value"):
693+
encode_value("hello", ValueType.NULL)

0 commit comments

Comments
 (0)