Skip to content

Commit 3215f6c

Browse files
committed
SSLSocket: handle exceptions during protocol read/write operations
These protocol operations should not raise exceptions, but sometimes they do. Catch the exception and extract the errno value if available. At the same time, harmonize the argument types for the underlying C routines
1 parent 49a6120 commit 3215f6c

File tree

3 files changed

+24
-13
lines changed

3 files changed

+24
-13
lines changed

shared-bindings/ssl/SSLSocket.c

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -248,24 +248,35 @@ static const mp_rom_map_elem_t ssl_sslsocket_locals_dict_table[] = {
248248

249249
static MP_DEFINE_CONST_DICT(ssl_sslsocket_locals_dict, ssl_sslsocket_locals_dict_table);
250250

251-
static mp_uint_t sslsocket_read(mp_obj_t self_in, void *buf, mp_uint_t size, int *errorcode) {
251+
typedef mp_uint_t (*readwrite_func)(ssl_sslsocket_obj_t *, const uint8_t *, mp_uint_t);
252+
253+
static mp_int_t readwrite_common(mp_obj_t self_in, readwrite_func fn, const uint8_t *buf, size_t size, int *errorcode) {
252254
ssl_sslsocket_obj_t *self = MP_OBJ_TO_PTR(self_in);
253-
mp_int_t ret = common_hal_ssl_sslsocket_recv_into(self, buf, size);
255+
mp_int_t ret;
256+
nlr_buf_t nlr;
257+
if (nlr_push(&nlr) == 0) {
258+
ret = fn(self, buf, size);
259+
nlr_pop();
260+
} else {
261+
mp_obj_t exc = MP_OBJ_FROM_PTR(nlr.ret_val);
262+
if (nlr_push(&nlr) == 0) {
263+
ret = -mp_obj_get_int(mp_load_attr(exc, MP_QSTR_errno));
264+
nlr_pop();
265+
}
266+
}
254267
if (ret < 0) {
255268
*errorcode = -ret;
256269
return MP_STREAM_ERROR;
257270
}
258271
return ret;
259272
}
260273

274+
static mp_uint_t sslsocket_read(mp_obj_t self_in, void *buf, mp_uint_t size, int *errorcode) {
275+
return readwrite_common(self_in, (readwrite_func)common_hal_ssl_sslsocket_recv_into, buf, size, errorcode);
276+
}
277+
261278
static mp_uint_t sslsocket_write(mp_obj_t self_in, const void *buf, mp_uint_t size, int *errorcode) {
262-
ssl_sslsocket_obj_t *self = MP_OBJ_TO_PTR(self_in);
263-
mp_int_t ret = common_hal_ssl_sslsocket_send(self, buf, size);
264-
if (ret < 0) {
265-
*errorcode = -ret;
266-
return MP_STREAM_ERROR;
267-
}
268-
return ret;
279+
return readwrite_common(self_in, common_hal_ssl_sslsocket_send, buf, size, errorcode);
269280
}
270281

271282
static mp_uint_t sslsocket_ioctl(mp_obj_t self_in, mp_uint_t request, mp_uint_t arg, int *errcode) {

shared-bindings/ssl/SSLSocket.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ bool common_hal_ssl_sslsocket_get_connected(ssl_sslsocket_obj_t *self);
2323
bool common_hal_ssl_sslsocket_readable(ssl_sslsocket_obj_t *self);
2424
bool common_hal_ssl_sslsocket_writable(ssl_sslsocket_obj_t *self);
2525
void common_hal_ssl_sslsocket_listen(ssl_sslsocket_obj_t *self, int backlog);
26-
mp_uint_t common_hal_ssl_sslsocket_recv_into(ssl_sslsocket_obj_t *self, uint8_t *buf, uint32_t len);
27-
mp_uint_t common_hal_ssl_sslsocket_send(ssl_sslsocket_obj_t *self, const uint8_t *buf, uint32_t len);
26+
mp_uint_t common_hal_ssl_sslsocket_recv_into(ssl_sslsocket_obj_t *self, uint8_t *buf, mp_uint_t len);
27+
mp_uint_t common_hal_ssl_sslsocket_send(ssl_sslsocket_obj_t *self, const uint8_t *buf, mp_uint_t len);
2828
void common_hal_ssl_sslsocket_settimeout(ssl_sslsocket_obj_t *self, mp_obj_t timeout_obj);
2929
void common_hal_ssl_sslsocket_setsockopt(ssl_sslsocket_obj_t *self, mp_obj_t level, mp_obj_t optname, mp_obj_t optval);

shared-module/ssl/SSLSocket.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -333,7 +333,7 @@ ssl_sslsocket_obj_t *common_hal_ssl_sslcontext_wrap_socket(ssl_sslcontext_obj_t
333333
}
334334
}
335335

336-
mp_uint_t common_hal_ssl_sslsocket_recv_into(ssl_sslsocket_obj_t *self, uint8_t *buf, uint32_t len) {
336+
mp_uint_t common_hal_ssl_sslsocket_recv_into(ssl_sslsocket_obj_t *self, uint8_t *buf, mp_uint_t len) {
337337
self->poll_mask = 0;
338338
int ret = mbedtls_ssl_read(&self->ssl, buf, len);
339339
DEBUG_PRINT("recv_into mbedtls_ssl_read() -> %d\n", ret);
@@ -353,7 +353,7 @@ mp_uint_t common_hal_ssl_sslsocket_recv_into(ssl_sslsocket_obj_t *self, uint8_t
353353
mbedtls_raise_error(ret);
354354
}
355355

356-
mp_uint_t common_hal_ssl_sslsocket_send(ssl_sslsocket_obj_t *self, const uint8_t *buf, uint32_t len) {
356+
mp_uint_t common_hal_ssl_sslsocket_send(ssl_sslsocket_obj_t *self, const uint8_t *buf, mp_uint_t len) {
357357
self->poll_mask = 0;
358358
int ret = mbedtls_ssl_write(&self->ssl, buf, len);
359359
DEBUG_PRINT("send mbedtls_ssl_write() -> %d\n", ret);

0 commit comments

Comments
 (0)