fix: reject a configuration entry with an empty value1
A line such as "=z" parsed into the pair ("", "z"). An empty value1
matches at every position and consumes nothing, so the scan in
replace_line never advanced and the program looped forever, printing
nothing and growing its output buffer until it was killed.
Reject the entry while parsing, reporting the offending line the same
way as a line with no "=" at all, and document the termination
invariant the loop depends on.
This commit is contained in:
@@ -18,6 +18,15 @@ def load_config(filename: str) -> list[tuple[str, str]]:
|
|||||||
)
|
)
|
||||||
|
|
||||||
value1, value2 = line.split("=", 1)
|
value1, value2 = line.split("=", 1)
|
||||||
|
|
||||||
|
# An empty value1 would match at every position without
|
||||||
|
# consuming anything, leaving replace_line unable to advance.
|
||||||
|
if not value1:
|
||||||
|
raise ValueError(
|
||||||
|
f"Invalid configuration at line {line_number}: {line!r} "
|
||||||
|
f"(the value to replace must not be empty)"
|
||||||
|
)
|
||||||
|
|
||||||
pairs.append((value1, value2))
|
pairs.append((value1, value2))
|
||||||
|
|
||||||
return pairs
|
return pairs
|
||||||
@@ -24,6 +24,8 @@ def replace_line(line: str, pairs: list[tuple[str, str]]) -> tuple[str, int]:
|
|||||||
replaced_symbols = 0
|
replaced_symbols = 0
|
||||||
position = 0
|
position = 0
|
||||||
|
|
||||||
|
# Terminates because every branch below advances `position`, which
|
||||||
|
# holds only while no value1 is empty. `load_config` rejects those.
|
||||||
while position < len(line):
|
while position < len(line):
|
||||||
match = None
|
match = None
|
||||||
|
|
||||||
|
|||||||
@@ -55,4 +55,27 @@ def test_empty_config_file(tmp_path):
|
|||||||
config_file = tmp_path / "config.txt"
|
config_file = tmp_path / "config.txt"
|
||||||
config_file.write_text("", encoding="utf-8")
|
config_file.write_text("", encoding="utf-8")
|
||||||
|
|
||||||
assert load_config(str(config_file)) == []
|
assert load_config(str(config_file)) == []
|
||||||
|
|
||||||
|
def test_empty_value1_raises_value_error(tmp_path):
|
||||||
|
"""A line such as '=z' has no value1 and must be rejected.
|
||||||
|
|
||||||
|
An empty value1 matches at every position and consumes nothing, so
|
||||||
|
the scan in replace_line would never advance past it. Rejecting the
|
||||||
|
pair here keeps that invariant a parsing concern rather than
|
||||||
|
something the replacement loop has to defend against.
|
||||||
|
"""
|
||||||
|
config_file = tmp_path / "config.txt"
|
||||||
|
config_file.write_text("a=z\n=x\n", encoding="utf-8")
|
||||||
|
|
||||||
|
with pytest.raises(ValueError):
|
||||||
|
load_config(str(config_file))
|
||||||
|
|
||||||
|
|
||||||
|
def test_line_that_is_only_an_equals_sign_raises_value_error(tmp_path):
|
||||||
|
"""'=' alone has neither value1 nor value2."""
|
||||||
|
config_file = tmp_path / "config.txt"
|
||||||
|
config_file.write_text("=\n", encoding="utf-8")
|
||||||
|
|
||||||
|
with pytest.raises(ValueError):
|
||||||
|
load_config(str(config_file))
|
||||||
|
|||||||
Reference in New Issue
Block a user