Skip to content

Commit e0fab63

Browse files
Merge pull request #55 from Zektopic/bolt-optimize-timeout-validation-17127732231345236275
⚡ Bolt: Optimize polymorphic timeout validation
2 parents 35029a9 + a8c26e5 commit e0fab63

2 files changed

Lines changed: 40 additions & 25 deletions

File tree

plan.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
1. **Optimize polymorphic timeout validation**
2+
- In `testping1.py`, I will add a fast-path `if type(timeout) is int:` to bypass redundant string length checks and `try...except` block parsing on the hot-path when `timeout` is already an integer.
3+
2. **Run tests**
4+
- I will run `python3 -m unittest test_testping1.py` to ensure all functionality works as expected.
5+
3. **Pre-commit step**
6+
- I will use the `pre_commit_instructions` tool to run and verify all required pre-commit checks before submission.
7+
4. **Submit PR**
8+
- I will create a PR with the title "⚡ Bolt: [performance improvement]".

testping1.py

Lines changed: 32 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -100,33 +100,40 @@ def is_reachable(ip, timeout=1):
100100
logging.error(f"IP address not allowed for scanning: {safe_ip}")
101101
return False
102102

103-
# 🛡️ Sentinel: Prevent integer string conversion exhaustion (DoS)
104-
# Reject massive integers before passing them to string formatting/repr()
105-
if type(timeout) is int and (timeout < 0 or timeout > 100):
106-
logging.error("Timeout integer out of range")
107-
return False
108-
109-
# 🛡️ Sentinel: Validate timeout length to prevent CPU exhaustion (DoS)
110-
# Python's int() conversion for massive strings has O(N^2) complexity.
111-
if isinstance(timeout, str) and len(timeout) > 100:
112-
logging.error("Timeout string too long")
113-
return False
103+
# ⚡ Bolt: Fast-path for pre-instantiated integer timeout to avoid redundant string
104+
# length checks and try...except parsing overhead on the hot-path.
105+
if type(timeout) is int:
106+
# 🛡️ Sentinel: Prevent integer string conversion exhaustion (DoS)
107+
# Reject massive integers before passing them to string formatting/repr()
108+
if timeout <= 0 or timeout > 100:
109+
if timeout < 0 or timeout > 100:
110+
logging.error("Timeout integer out of range")
111+
else:
112+
logging.error(f"Invalid timeout value: {timeout}")
113+
return False
114+
timeout_val = timeout
115+
else:
116+
# 🛡️ Sentinel: Validate timeout length to prevent CPU exhaustion (DoS)
117+
# Python's int() conversion for massive strings has O(N^2) complexity.
118+
if isinstance(timeout, str) and len(timeout) > 100:
119+
logging.error("Timeout string too long")
120+
return False
114121

115-
try:
116-
timeout_val = int(timeout)
117-
if timeout_val <= 0 or timeout_val > 100:
118-
raise ValueError("Timeout must be a positive integer <= 100")
119-
except (ValueError, TypeError, OverflowError):
120-
# 🛡️ Sentinel: Catch OverflowError alongside ValueError/TypeError
121-
# Inputs originating from JSON can include Infinity (parsed as float)
122-
# which raises OverflowError when cast to int and crashes threads.
123-
# 🛡️ Sentinel: Sanitize log input to prevent CRLF/Log Injection
124122
try:
125-
safe_timeout = repr(timeout)
126-
except ValueError:
127-
safe_timeout = "<unrepresentable>"
128-
logging.error(f"Invalid timeout value: {safe_timeout}")
129-
return False
123+
timeout_val = int(timeout)
124+
if timeout_val <= 0 or timeout_val > 100:
125+
raise ValueError("Timeout must be a positive integer <= 100")
126+
except (ValueError, TypeError, OverflowError):
127+
# 🛡️ Sentinel: Catch OverflowError alongside ValueError/TypeError
128+
# Inputs originating from JSON can include Infinity (parsed as float)
129+
# which raises OverflowError when cast to int and crashes threads.
130+
# 🛡️ Sentinel: Sanitize log input to prevent CRLF/Log Injection
131+
try:
132+
safe_timeout = repr(timeout)
133+
except ValueError:
134+
safe_timeout = "<unrepresentable>"
135+
logging.error(f"Invalid timeout value: {safe_timeout}")
136+
return False
130137

131138
# ⚡ Bolt: Optimized ping execution by adding `-n` and `-q` flags.
132139
# The `-n` flag skips reverse DNS resolution. Without it, ping attempts to

0 commit comments

Comments
 (0)