Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit583b17e

Browse files
committed
extmod/modlwip.c: Fix crash when calling recv on listening socket.
Add check to prevent calling recv on a socket in the listening state.This prevents a crash/hard fault when user code mistakenly tries torecv on the listening socket instead of on the accepted connection.Add corresponding test case to demonstrate the bug.Signed-off-by: Andrew Leech <andrew.leech@planetinnovation.com.au>
1 parent186caf9 commit583b17e

File tree

2 files changed

+45
-0
lines changed

2 files changed

+45
-0
lines changed

‎extmod/modlwip.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -854,6 +854,12 @@ static mp_uint_t lwip_tcp_receive(lwip_socket_obj_t *socket, byte *buf, mp_uint_
854854
}
855855
}
856856

857+
if (socket->state==STATE_LISTENING) {
858+
// original socket in listening state, not the accepted connection.
859+
*_errno=MP_ENOTCONN;
860+
return-1;
861+
}
862+
857863
MICROPY_PY_LWIP_ENTER
858864

859865
assert(socket->pcb.tcp!= NULL);
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# Test recv on listening socket after accept without saving connection
2+
# This demonstrates a bug where MicroPython would hard fault instead of raising
3+
# the proper exception when trying to recv() on a listening socket after accept()
4+
5+
importsocket
6+
7+
PORT=8000
8+
9+
10+
# Server
11+
definstance0():
12+
multitest.globals(IP=multitest.get_network_ip())
13+
s=socket.socket(socket.AF_INET)
14+
s.bind(("0.0.0.0",PORT))
15+
s.listen()
16+
multitest.next()
17+
18+
# Accept a connection but don't save the returned values
19+
s.accept()
20+
21+
# Try to recv on the listening socket - this should raise an OSError
22+
# with errno 107 (ENOTCONN) instead of crashing
23+
try:
24+
print("recv",s.recv(128))# should raise ENOTCONN
25+
exceptOSErroraser:
26+
# Print whether errno is 107 (ENOTCONN) or 128 (ENOTSOCK on some systems)
27+
print(f"errno={er.errno}")
28+
print(er.errnoin (107,128))
29+
30+
s.close()
31+
32+
33+
# Client
34+
definstance1():
35+
multitest.next()
36+
s=socket.socket(socket.AF_INET)
37+
s.connect(socket.getaddrinfo(IP,PORT)[0][-1])
38+
s.send(b"Hello, world!")
39+
s.close()

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp