Skip to content

Commit 6d21721

Browse files
Reject infinity in decode_double to match encode_double
encode_double already rejects both NaN and infinity, but decode_double only rejected NaN. A malicious or buggy server could send infinity bytes that would be silently accepted. This adds the matching isinf() check. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent 15f9747 commit 6d21721

2 files changed

Lines changed: 18 additions & 0 deletions

File tree

src/dqlitewire/types.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,8 @@ def decode_double(data: bytes) -> float:
7474
result: float = struct.unpack("<d", data[:8])[0]
7575
if math.isnan(result):
7676
raise DecodeError("Received NaN double value, not supported by dqlite wire protocol")
77+
if math.isinf(result):
78+
raise DecodeError("Received infinity double value, not supported by dqlite wire protocol")
7779
return result
7880

7981

tests/test_types.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,22 @@ def test_decode_nan_rejected(self) -> None:
124124
with pytest.raises(DecodeError, match="NaN"):
125125
decode_double(nan_bytes)
126126

127+
def test_decode_positive_infinity_rejected(self) -> None:
128+
"""Decoding +inf should raise DecodeError, matching encode_double's rejection."""
129+
import struct
130+
131+
inf_bytes = struct.pack("<d", float("inf"))
132+
with pytest.raises(DecodeError, match="[Ii]nfinity"):
133+
decode_double(inf_bytes)
134+
135+
def test_decode_negative_infinity_rejected(self) -> None:
136+
"""Decoding -inf should raise DecodeError, matching encode_double's rejection."""
137+
import struct
138+
139+
inf_bytes = struct.pack("<d", float("-inf"))
140+
with pytest.raises(DecodeError, match="[Ii]nfinity"):
141+
decode_double(inf_bytes)
142+
127143

128144
class TestPadding:
129145
def test_pad_to_word_aligned(self) -> None:

0 commit comments

Comments
 (0)