Skip to content

Commit 528ce5d

Browse files
fix: harden code
Fixes #87. Signed-off-by: Frederic Pillon <frederic.pillon@st.com> Co-Authored-by: raphael valentin <raphaelvalentin@users.noreply.github.com>
1 parent c7a49b7 commit 528ce5d

2 files changed

Lines changed: 14 additions & 10 deletions

File tree

src/EthernetClient.cpp

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -186,16 +186,18 @@ void EthernetClient::flush()
186186

187187
void EthernetClient::stop()
188188
{
189-
if (_tcp_client == NULL) {
190-
return;
191-
}
192-
193-
// close tcp connection if not closed yet
194-
if (status() != TCP_CLOSING) {
195-
tcp_connection_close(_tcp_client->pcb, _tcp_client);
189+
if (_tcp_client != NULL) {
190+
// close tcp connection if not closed yet
191+
if (status() != TCP_CLOSING) {
192+
if (_tcp_client->pcb == NULL) {
193+
_tcp_client->state = TCP_CLOSING;
194+
} else {
195+
tcp_connection_close(_tcp_client->pcb, _tcp_client);
196+
}
197+
}
198+
mem_free(_tcp_client);
199+
_tcp_client = NULL;
196200
}
197-
mem_free(_tcp_client);
198-
_tcp_client = NULL;
199201
}
200202

201203
uint8_t EthernetClient::connected()

src/utility/stm32_eth.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1079,7 +1079,9 @@ void tcp_connection_close(struct tcp_pcb *tpcb, struct tcp_struct *tcp)
10791079
tcp_accept(tpcb, NULL);
10801080

10811081
/* close tcp connection */
1082-
tcp_close(tpcb);
1082+
if (tcp_close(tpcb) != ERR_OK) {
1083+
tcp_abort(tpcb);
1084+
}
10831085

10841086
tcp->pcb = NULL;
10851087
tcp->state = TCP_CLOSING;

0 commit comments

Comments
 (0)