Skip to content

Commit

Permalink
extmod/modssl_mbedtls: Fix ioctl of a socket in closed/error state.
Browse files Browse the repository at this point in the history
Signed-off-by: Damien George <damien@micropython.org>
  • Loading branch information
dpgeorge committed Aug 9, 2023
1 parent 20d3a6b commit d529c20
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 5 deletions.
13 changes: 9 additions & 4 deletions extmod/modssl_mbedtls.c
Original file line number Diff line number Diff line change
Expand Up @@ -486,15 +486,20 @@ STATIC mp_uint_t socket_ioctl(mp_obj_t o_in, mp_uint_t request, uintptr_t arg, i
mp_uint_t ret = 0;
uintptr_t saved_arg = 0;
mp_obj_t sock = self->sock;
if (sock == MP_OBJ_NULL || (request != MP_STREAM_CLOSE && self->last_error != 0)) {
// Closed or error socket:
return MP_STREAM_POLL_NVAL;
}

if (request == MP_STREAM_CLOSE) {
if (sock == MP_OBJ_NULL) {
// Already closed socket, do nothing.
return 0;
}
self->sock = MP_OBJ_NULL;
mbedtls_ssl_free(&self->ssl);
} else if (request == MP_STREAM_POLL) {
if (sock == MP_OBJ_NULL || self->last_error != 0) {
// Closed or error socket, return NVAL flag.
return MP_STREAM_POLL_NVAL;
}

// If the library signaled us that it needs reading or writing, only check that direction,
// but save what the caller asked because we need to restore it later
if (self->poll_mask && (arg & MP_STREAM_POLL_RDWR)) {
Expand Down
2 changes: 1 addition & 1 deletion tests/extmod/ssl_ioctl.py.exp
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@
10 OSError
4 0
3 32
4 32
4 0

0 comments on commit d529c20

Please sign in to comment.