Skip to content

Commit b50c092

Browse files
committed
Reject non-finite floats (nan, inf) in parameter substitution
1 parent e23f553 commit b50c092

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed

tests/test_cursor.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,18 @@ def test_non_primitive_uses_str(self):
7878

7979
assert _quote_value(date(2024, 1, 15)) == "'2024-01-15'"
8080

81+
def test_nan_raises(self):
82+
with pytest.raises(ProgrammingError, match="Cannot convert float"):
83+
_quote_value(float("nan"))
84+
85+
def test_inf_raises(self):
86+
with pytest.raises(ProgrammingError, match="Cannot convert float"):
87+
_quote_value(float("inf"))
88+
89+
def test_negative_inf_raises(self):
90+
with pytest.raises(ProgrammingError, match="Cannot convert float"):
91+
_quote_value(float("-inf"))
92+
8193

8294
# ---------------------------------------------------------------------------
8395
# cursor.execute() end-to-end tests

wherobots/db/cursor.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import math
12
import queue
23
import re
34
from typing import Any, List, Tuple, Dict
@@ -21,6 +22,10 @@ def _quote_value(value: Any) -> str:
2122
if isinstance(value, bool):
2223
return "TRUE" if value else "FALSE"
2324
if isinstance(value, (int, float)):
25+
if isinstance(value, float) and (math.isnan(value) or math.isinf(value)):
26+
raise ProgrammingError(
27+
f"Cannot convert float value {value!r} to SQL literal"
28+
)
2429
return str(value)
2530
if isinstance(value, bytes):
2631
return "X'" + value.hex() + "'"

0 commit comments

Comments
 (0)