Skip to content

Commit 4368ae3

Browse files
committed
extmod/modussl_axtls: Allow to close ssl stream multiple times.
Make sure that 2nd close has no effect and operations on closed streams are handled properly.
1 parent 4662006 commit 4368ae3

File tree

3 files changed

+29
-4
lines changed

3 files changed

+29
-4
lines changed

extmod/modussl_axtls.c

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,11 @@ STATIC void socket_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kin
102102
STATIC mp_uint_t socket_read(mp_obj_t o_in, void *buf, mp_uint_t size, int *errcode) {
103103
mp_obj_ssl_socket_t *o = MP_OBJ_TO_PTR(o_in);
104104

105+
if (o->ssl_sock == NULL) {
106+
*errcode = EBADF;
107+
return MP_STREAM_ERROR;
108+
}
109+
105110
while (o->bytes_left == 0) {
106111
mp_int_t r = ssl_read(o->ssl_sock, &o->buf);
107112
if (r == SSL_OK) {
@@ -131,6 +136,12 @@ STATIC mp_uint_t socket_read(mp_obj_t o_in, void *buf, mp_uint_t size, int *errc
131136

132137
STATIC mp_uint_t socket_write(mp_obj_t o_in, const void *buf, mp_uint_t size, int *errcode) {
133138
mp_obj_ssl_socket_t *o = MP_OBJ_TO_PTR(o_in);
139+
140+
if (o->ssl_sock == NULL) {
141+
*errcode = EBADF;
142+
return MP_STREAM_ERROR;
143+
}
144+
134145
mp_int_t r = ssl_write(o->ssl_sock, buf, size);
135146
if (r < 0) {
136147
*errcode = r;
@@ -151,9 +162,14 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_2(socket_setblocking_obj, socket_setblocking);
151162

152163
STATIC mp_obj_t socket_close(mp_obj_t self_in) {
153164
mp_obj_ssl_socket_t *self = MP_OBJ_TO_PTR(self_in);
154-
ssl_free(self->ssl_sock);
155-
ssl_ctx_free(self->ssl_ctx);
156-
return mp_stream_close(self->sock);
165+
if (self->ssl_sock != NULL) {
166+
ssl_free(self->ssl_sock);
167+
ssl_ctx_free(self->ssl_ctx);
168+
self->ssl_sock = NULL;
169+
return mp_stream_close(self->sock);
170+
}
171+
172+
return mp_const_none;
157173
}
158174
STATIC MP_DEFINE_CONST_FUN_OBJ_1(socket_close_obj, socket_close);
159175

tests/extmod/ussl_basic.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,14 @@
4343

4444
# close
4545
ss.close()
46+
# close 2nd time
47+
ss.close()
48+
49+
# read on closed socket
50+
try:
51+
ss.read(10)
52+
except OSError as er:
53+
print('read:', repr(er))
4654

4755
# write on closed socket
4856
try:

tests/extmod/ussl_basic.py.exp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,5 @@ setblocking: NotImplementedError
55
4
66
b''
77
read: OSError(-261,)
8-
write: OSError(-256,)
8+
read: OSError(9,)
9+
write: OSError(9,)

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy