Skip to content

Commit cf184fd

Browse files
committed
Add checks on socket validity before operations
1 parent 817b47a commit cf184fd

File tree

1 file changed

+18
-9
lines changed

1 file changed

+18
-9
lines changed

libraries/Ethernet/src/EthernetClient.cpp

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,10 @@ void arduino::EthernetClient::setSocket(Socket* _sock) {
1919
}
2020

2121
void arduino::EthernetClient::getStatus() {
22-
if (sock == nullptr)
22+
if (sock == nullptr) {
23+
_status = false;
2324
return;
25+
}
2426

2527
uint8_t data[256];
2628
int ret = sock->recv(data, rxBuffer.availableForStore());
@@ -33,7 +35,7 @@ void arduino::EthernetClient::getStatus() {
3335
}
3436

3537
int arduino::EthernetClient::connect(SocketAddress socketAddress) {
36-
if (sock == NULL) {
38+
if (sock == nullptr) {
3739
sock = new TCPSocket();
3840
if(static_cast<TCPSocket*>(sock)->open(Ethernet.getNetwork()) != NSAPI_ERROR_OK){
3941
return 0;
@@ -62,7 +64,7 @@ int arduino::EthernetClient::connect(const char *host, uint16_t port) {
6264
}
6365

6466
int arduino::EthernetClient::connectSSL(SocketAddress socketAddress){
65-
if (sock == NULL) {
67+
if (sock == nullptr) {
6668
sock = new TLSSocket();
6769
if(static_cast<TLSSocket*>(sock)->open(Ethernet.getNetwork()) != NSAPI_ERROR_OK){
6870
return 0;
@@ -88,11 +90,17 @@ int arduino::EthernetClient::connectSSL(const char *host, uint16_t port) {
8890
}
8991

9092
size_t arduino::EthernetClient::write(uint8_t c) {
91-
sock->send(&c, 1);
93+
if (sock == nullptr)
94+
return -1;
95+
auto ret = sock->send(&c, 1);
96+
return ret;
9297
}
9398

9499
size_t arduino::EthernetClient::write(const uint8_t *buf, size_t size) {
95-
sock->send(buf, size);
100+
if (sock == nullptr)
101+
return -1;
102+
auto ret = sock->send(buf, size);
103+
return ret;
96104
}
97105

98106
int arduino::EthernetClient::available() {
@@ -137,15 +145,16 @@ void arduino::EthernetClient::flush() {
137145
}
138146

139147
void arduino::EthernetClient::stop() {
140-
if (sock != NULL) {
148+
if (sock != nullptr) {
141149
sock->close();
142-
sock = NULL;
143-
_status = false;
150+
sock = nullptr;
144151
}
152+
_status = false;
145153
}
146154

147155
uint8_t arduino::EthernetClient::connected() {
148-
return _status == true;
156+
getStatus();
157+
return _status;
149158
}
150159

151160
IPAddress arduino::EthernetClient::remoteIP() {

0 commit comments

Comments
 (0)