Skip to content

Commit 815ab52

Browse files
committed
Fix stubs error, out of sockets error, invalid TLS leak
1 parent 9f34ec7 commit 815ab52

File tree

4 files changed

+14
-7
lines changed

4 files changed

+14
-7
lines changed

ports/esp32s2/common-hal/socketpool/Socket.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,8 @@ void socket_reset(void) {
4444
for (size_t i = 0; i < MP_ARRAY_SIZE(open_socket_handles); i++) {
4545
if (open_socket_handles[i]) {
4646
if (open_socket_handles[i]->num > 0) {
47+
// Close automatically clears socket handle
4748
common_hal_socketpool_socket_close(open_socket_handles[i]);
48-
open_socket_handles[i] = NULL;
4949
} else {
5050
open_socket_handles[i] = NULL;
5151
}
@@ -136,6 +136,12 @@ void common_hal_socketpool_socket_close(socketpool_socket_obj_t* self) {
136136
lwip_close(self->num);
137137
self->num = -1;
138138
}
139+
// Remove socket record
140+
for (size_t i = 0; i < MP_ARRAY_SIZE(open_socket_handles); i++) {
141+
if (open_socket_handles[i] == self) {
142+
open_socket_handles[i] = NULL;
143+
}
144+
}
139145
}
140146

141147
bool common_hal_socketpool_socket_connect(socketpool_socket_obj_t* self,

ports/esp32s2/common-hal/ssl/SSLContext.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,15 @@ void common_hal_ssl_sslcontext_construct(ssl_sslcontext_obj_t* self) {
3838
ssl_sslsocket_obj_t* common_hal_ssl_sslcontext_wrap_socket(ssl_sslcontext_obj_t* self,
3939
socketpool_socket_obj_t* socket, bool server_side, const char* server_hostname) {
4040

41+
if (socket->type != SOCK_STREAM || socket->num != -1) {
42+
mp_raise_RuntimeError(translate("Invalid socket for TLS"));
43+
}
44+
4145
ssl_sslsocket_obj_t *sock = m_new_obj_with_finaliser(ssl_sslsocket_obj_t);
4246
sock->base.type = &ssl_sslsocket_type;
4347
sock->ssl_context = self;
4448
sock->sock = socket;
4549

46-
if (socket->type != SOCK_STREAM || socket->num != -1) {
47-
mp_raise_RuntimeError(translate("Invalid socket for TLS"));
48-
}
4950
esp_tls_t* tls_handle = esp_tls_init();
5051
if (tls_handle == NULL) {
5152
mp_raise_espidf_MemoryError();

shared-bindings/ssl/SSLContext.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ STATIC mp_obj_t ssl_sslcontext_make_new(const mp_obj_type_t *type, size_t n_args
5151
return MP_OBJ_FROM_PTR(s);
5252
}
5353

54-
//| def wrap_socket(sock: socketpool.Socket, *, server_side: bool = False, server_hostname: Optional[str] = None) -> socketpool.Socket:
54+
//| def wrap_socket(sock: socketpool.Socket, *, server_side: bool = False, server_hostname: Optional[str] = None) -> ssl.SSLSocket:
5555
//| """Wraps the socket into a socket-compatible class that handles SSL negotiation.
5656
//| The socket must be of type SOCK_STREAM."""
5757
//| ...

shared-bindings/ssl/SSLSocket.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
//| recv that do not allocate bytes objects."""
4646
//|
4747

48-
//| def __enter__(self) -> Socket:
48+
//| def __enter__(self) -> SSLSocket:
4949
//| """No-op used by Context Managers."""
5050
//| ...
5151
//|
@@ -63,7 +63,7 @@ STATIC mp_obj_t ssl_sslsocket___exit__(size_t n_args, const mp_obj_t *args) {
6363
}
6464
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(ssl_sslsocket___exit___obj, 4, 4, ssl_sslsocket___exit__);
6565

66-
//| def accept(self) -> Tuple[Socket, Tuple[str, int]]:
66+
//| def accept(self) -> Tuple[SSLSocket, Tuple[str, int]]:
6767
//| """Accept a connection on a listening socket of type SOCK_STREAM,
6868
//| creating a new socket of type SOCK_STREAM.
6969
//| Returns a tuple of (new_socket, remote_address)"""

0 commit comments

Comments
 (0)