8000 extmod/mbedtls: Remove mbedtls_raise_error again. · micropython/micropython@de583a7 · GitHub
[go: up one dir, main page]

Skip to content

Commit de583a7

Browse files
committed
extmod/mbedtls: Remove mbedtls_raise_error again.
1 parent 8db6b88 commit de583a7

File tree

2 files changed

+2
-42
lines changed

2 files changed

+2
-42
lines changed

extmod/modussl_mbedtls.c

Lines changed: 1 addition & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -87,46 +87,6 @@ STATIC void mbedtls_debug(void *ctx, int level, const char *file, int line, cons
8787
}
8888
#endif
8989

90-
STATIC NORETURN void mbedtls_raise_error(int err) {
91-
// _mbedtls_ssl_send and _mbedtls_ssl_recv (below) turn positive error codes from the
92-
// underlying socket into negative codes to pass them through mbedtls. Here we turn them
93-
// positive again so they get interpreted as the OSError they really are. The
94-
// cut-off of -256 is a bit hacky, sigh.
95-
if (err < 0 && err > -256) {
96-
mp_raise_OSError(-err);
97-
}
98-
99-
#if defined(MBEDTLS_ERROR_C)
100-
// Including mbedtls_strerror takes about 1.5KB due to the error strings.
101-
// MBEDTLS_ERROR_C is the define used by mbedtls to conditionally include mbedtls_strerror.
102-
// It is set/unset in the MBEDTLS_CONFIG_FILE which is defined in the Makefile.
103-
104-
// Try to allocate memory for the message
105-
#define ERR_STR_MAX 80 // mbedtls_strerror truncates if it doesn't fit
106-
mp_obj_str_t *o_str = m_new_obj_maybe(mp_obj_str_t);
107-
byte *o_str_buf = m_new_maybe(byte, ERR_STR_MAX);
108-
if (o_str == NULL || o_str_buf == NULL) {
109-
mp_raise_OSError(err);
110-
}
111-
112-
// print the error message into the allocated buffer
113-
mbedtls_strerror(err, (char *)o_str_buf, ERR_STR_MAX);
114-
size_t len = strlen((char *)o_str_buf);
115-
116-
// Put the exception object together
117-
o_str->base.type = &mp_type_str;
118-
o_str->data = o_str_buf;
119-
o_str->len = len;
120-
o_str->hash = qstr_compute_hash(o_str->data, o_str->len);
121-
// raise
122-
mp_obj_t args[2] = { MP_OBJ_NEW_SMALL_INT(err), MP_OBJ_FROM_PTR(o_str)};
123-
nlr_raise(mp_obj_exception_make_new(&mp_type_OSError, 2, 0, args));
124-
#else
125-
// mbedtls is compiled without error strings so we simply return the err number
126-
mp_raise_OSError(err); // err is typically a large negative number
127-
#endif
128-
}
129-
13090
// mod_ssl_errstr returns the error string corresponding to the error code found in an OSError,
13191
// such as returned by read/write.
13292
STATIC mp_obj_t mod_ssl_errstr(mp_obj_t err_in) {
@@ -299,7 +259,7 @@ STATIC mp_obj_ssl_socket_t *socket_new(mp_obj_t sock, struct ssl_args *args) {
299259
} else if (ret == MBEDTLS_ERR_X509_BAD_INPUT_DATA) {
300260
mp_raise_ValueError(MP_ERROR_TEXT("invalid cert"));
301261
} else {
302-
mbedtls_raise_error(ret);
262+
mp_raise_OSError(-ret);
303263
}
304264
}
305265

ports/esp32/modsocket.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -358,7 +358,7 @@ STATIC mp_obj_t socket_connect(const mp_obj_t arg0, const mp_obj_t arg1) {
358358
// side-note: LwIP internally doesn't seem to have an error code for ECONNREFUSED and
359359
// so refused connections show up as ECONNRESET. Could be band-aided for blocking connect,
360360
// harder to do for nonblocking.
361-
exception_from_errno(errno);
361+
mp_raise_OSError(errno);
362362
}
363363

364364
return mp_const_none;

0 commit comments

Comments
 (0)
0