stmhal: Bug fix for usocket's accept and setsockopt methods.

accept might raise an exception, in which case the new socket is not
fully created.  It has a finaliser so will run close() method when GC'd.
Before this patch close would try to close an invalid socket.  Now
fixed.

setsockopt took address of stack value which became out of scope.  Now
fixed.
This commit is contained in:
Damien George 2015-01-24 15:07:50 +00:00
parent 91232d3850
commit 23342c09ff
1 changed files with 9 additions and 3 deletions

View File

@ -132,10 +132,11 @@ STATIC mp_obj_t socket_accept(mp_obj_t self_in) {
mod_network_socket_obj_t *self = self_in;
// create new socket object
// starts with empty NIC so that finaliser doesn't run close() method if accept() fails
mod_network_socket_obj_t *socket2 = m_new_obj_with_finaliser(mod_network_socket_obj_t);
socket2->base.type = (mp_obj_t)&socket_type;
socket2->nic = self->nic;
socket2->nic_type = self->nic_type;
socket2->nic = MP_OBJ_NULL;
socket2->nic_type = NULL;
// accept incoming connection
uint8_t ip[MOD_NETWORK_IPADDR_BUF_SIZE];
@ -145,6 +146,10 @@ STATIC mp_obj_t socket_accept(mp_obj_t self_in) {
nlr_raise(mp_obj_new_exception_arg1(&mp_type_OSError, MP_OBJ_NEW_SMALL_INT(_errno)));
}
// new socket has valid state, so set the NIC to the same as parent
socket2->nic = self->nic;
socket2->nic_type = self->nic_type;
// make the return value
mp_obj_tuple_t *client = mp_obj_new_tuple(2, NULL);
client->items[0] = socket2;
@ -281,8 +286,9 @@ STATIC mp_obj_t socket_setsockopt(mp_uint_t n_args, const mp_obj_t *args) {
const void *optval;
mp_uint_t optlen;
mp_int_t val;
if (mp_obj_is_integer(args[3])) {
mp_int_t val = mp_obj_int_get_truncated(args[3]);
val = mp_obj_int_get_truncated(args[3]);
optval = &val;
optlen = sizeof(val);
} else {