Skip to content

Commit

Permalink
Fix EthernetClient connect() and close() to check for NULL across yie…
Browse files Browse the repository at this point in the history
…ld()

It's possible for the internal connection object to become NULL.
  • Loading branch information
ssilverman committed Feb 2, 2023
1 parent fa2b68a commit 7724fbe
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 3 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ and this project adheres to
The function was always setting the TCP flag, regardless of the value of
the argument.
* Fixed printing unknown netif name characters in some debug messages.
* Fixed `EthernetClient::connect()` and `close()` operations to check the
internal connection object for NULL across `yield()` calls.

## [0.17.0]

Expand Down
8 changes: 5 additions & 3 deletions src/QNEthernetClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,11 +94,12 @@ int EthernetClient::connect(const ip_addr_t *ipaddr, uint16_t port, bool wait) {
// Wait for a connection
if (wait) {
elapsedMillis timer;
while (!conn_->connected && timer < connTimeout_) {
// NOTE: conn_ could be set to NULL somewhere during the yield
while (conn_ != nullptr && !conn_->connected && timer < connTimeout_) {
// NOTE: Depends on Ethernet loop being called from yield()
yield();
}
if (!conn_->connected) {
if (conn_ == nullptr || !conn_->connected) {
close();
return static_cast<int>(ConnectReturns::TIMED_OUT);
}
Expand Down Expand Up @@ -195,7 +196,8 @@ void EthernetClient::close(bool wait) {
tcp_abort(state->pcb);
} else if (wait) {
elapsedMillis timer;
while (conn_->connected && timer < connTimeout_) {
// NOTE: conn_ could be set to NULL somewhere during the yield
while (conn_ != nullptr && conn_->connected && timer < connTimeout_) {
// NOTE: Depends on Ethernet loop being called from yield()
yield();
}
Expand Down

0 comments on commit 7724fbe

Please sign in to comment.