Skip to content

Commit 8a4a799

Browse files
committed
esp32/modsocket: Add error checking for creating and closing sockets.
1 parent facb68b commit 8a4a799

File tree

1 file changed

+15
-6
lines changed

1 file changed

+15
-6
lines changed

esp32/modsocket.c

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -58,18 +58,24 @@ typedef struct _socket_obj_t {
5858
uint8_t proto;
5959
} socket_obj_t;
6060

61+
NORETURN static void exception_from_errno(int _errno) {
62+
// XXX add more specific exceptions
63+
mp_raise_OSError(_errno);
64+
}
65+
6166
STATIC mp_obj_t socket_close(const mp_obj_t arg0) {
6267
socket_obj_t *self = MP_OBJ_TO_PTR(arg0);
63-
lwip_close_r(self->fd);
68+
if (self->fd >= 0) {
69+
int ret = lwip_close_r(self->fd);
70+
if (ret != 0) {
71+
exception_from_errno(errno);
72+
}
73+
self->fd = -1;
74+
}
6475
return mp_const_none;
6576
}
6677
STATIC MP_DEFINE_CONST_FUN_OBJ_1(socket_close_obj, socket_close);
6778

68-
NORETURN static void exception_from_errno(int _errno) {
69-
// XXX add more specific exceptions
70-
mp_raise_OSError(_errno);
71-
}
72-
7379
static int _socket_getaddrinfo2(const mp_obj_t host, const mp_obj_t portx, struct addrinfo **resp) {
7480
const struct addrinfo hints = {
7581
.ai_family = AF_INET,
@@ -341,6 +347,9 @@ STATIC mp_obj_t get_socket(mp_uint_t n_args, const mp_obj_t *args) {
341347
sock->type = SOCK_STREAM;
342348
sock->proto = IPPROTO_TCP;
343349
sock->fd = lwip_socket(sock->domain, sock->type, sock->proto);
350+
if (sock->fd < 0) {
351+
exception_from_errno(errno);
352+
}
344353
return MP_OBJ_FROM_PTR(sock);
345354
}
346355
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(get_socket_obj, 0, 3, get_socket);

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