Skip to content
This repository was archived by the owner on Jul 22, 2023. It is now read-only.
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion src/bsp/platform/posix/iotc_bsp_io_net_posix.c
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,13 @@ iotc_bsp_io_net_state_t iotc_bsp_io_net_socket_connect(
hints.ai_flags = 0;
hints.ai_protocol = 0;

// in case of error, we want *iotc_socket initialized to a non-valid
// value so that iotc_bsp_io_net_close_socket can check before
// closing a socket to ensure that the socket was genuinely opened;
// otherwise iotc_bsp_io_net_close_socket can be at risk of
// incorrectly closing STDIN_FILENO
*iotc_socket = -1;

// Address resolution.
status = getaddrinfo(host, NULL, &hints, &result);
if (0 != status) {
Expand Down Expand Up @@ -90,6 +97,7 @@ iotc_bsp_io_net_state_t iotc_bsp_io_net_socket_connect(
return IOTC_BSP_IO_NET_STATE_OK;
} else {
close(*iotc_socket);
*iotc_socket = -1;
}
}
}
Expand Down Expand Up @@ -200,11 +208,16 @@ iotc_bsp_io_net_state_t iotc_bsp_io_net_close_socket(
return IOTC_BSP_IO_NET_STATE_ERROR;
}

if (*iotc_socket < 0)
// no need to close a socket that was never opened in the first
// place
return IOTC_BSP_IO_NET_STATE_OK;

shutdown(*iotc_socket, SHUT_RDWR);

close(*iotc_socket);

*iotc_socket = 0;
*iotc_socket = -1;

return IOTC_BSP_IO_NET_STATE_OK;
}
Expand Down