Skip to content

Commit f97890d

Browse files
authored
Fix check when closing native L3 windows socket (#4820)
* Update native.py * Add test for L3WinSocket.close() with partial init Introduces a test to ensure L3WinSocket.close() does not raise AttributeError when called on a partially initialized object. This improves robustness by verifying correct handling of incomplete initialization.
1 parent d6a25bb commit f97890d

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

scapy/arch/windows/native.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ def recv_raw(self, x=MTU):
222222

223223
def close(self):
224224
# type: () -> None
225-
if not self.closed and self.promisc:
225+
if not self.closed and self.promisc and hasattr(self, 'ins'):
226226
self.ins.ioctl(socket.SIO_RCVALL, socket.RCVALL_OFF)
227227
super(L3WinSocket, self).close()
228228

test/windows.uts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,27 @@ def _test():
117117

118118
retry_test(_test)
119119

120+
= Test L3WinSocket close() with partial initialization
121+
~ windows
122+
123+
from scapy.arch.windows.native import L3WinSocket
124+
import socket
125+
126+
# Create partially initialized L3WinSocket
127+
ws = object.__new__(L3WinSocket)
128+
ws.closed = False
129+
ws.promisc = True
130+
# Note: ws.ins is intentionally not set
131+
132+
# This should not raise AttributeError
133+
try:
134+
ws.close()
135+
test_passed = True
136+
except AttributeError:
137+
test_passed = False
138+
139+
assert test_passed, "L3WinSocket.close() raised AttributeError on partially initialized object"
140+
120141
= Leave native mode
121142

122143
conf.use_pcap = True

0 commit comments

Comments
 (0)