Skip to content

Commit

Permalink
SocketNoTimeout needs to check for 0 and negative timeouts
Browse files Browse the repository at this point in the history
The SocketNoTimeout rule also needs to check for timeout values
of 0 or less than 0 which also equate to no timeout. The value
can also be of type float.

Signed-off-by: Eric Brown <[email protected]>
  • Loading branch information
ericwb committed Sep 26, 2024
1 parent 53bd258 commit 21391a8
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 0 deletions.
6 changes: 6 additions & 0 deletions precli/rules/python/stdlib/socket_no_timeout.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,12 @@ def analyze_call(self, context: dict, call: Call) -> Result | None:
fix_node = argument.node
result_node = argument.node
content = "5"
elif (
isinstance(timeout, int) or isinstance(timeout, float)
) and timeout <= 0:
fix_node = argument.node
result_node = argument.node
content = "5"
else:
return

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# level: WARNING
# start_line: 9
# end_line: 9
# start_column: 56
# end_column: 57
import socket


s = socket.create_connection(("127.0.0.1", 80), timeout=0)
s.recv(1024)
s.close()
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ def test_rule_meta(self):
"filename",
[
"socket_create_connection.py",
"socket_create_connection_timeout_0.py",
"socket_create_connection_timeout_5.py",
"socket_create_connection_timeout_none.py",
],
Expand Down

0 comments on commit 21391a8

Please sign in to comment.