From dbd677e6e76f10ebff38b9a1ca0188532f96fdc7 Mon Sep 17 00:00:00 2001 From: Sara Damiano Date: Wed, 15 Apr 2020 08:34:44 -0400 Subject: [PATCH] Fix several potential null pointers Signed-off-by: Sara Damiano --- src/TinyGsmClientESP8266.h | 10 +++++++-- src/TinyGsmClientSIM5360.h | 15 +++++++++++-- src/TinyGsmClientSIM7600.h | 8 ++++++- src/TinyGsmClientSequansMonarch.h | 37 ++++++++++++++++++------------- 4 files changed, 49 insertions(+), 21 deletions(-) diff --git a/src/TinyGsmClientESP8266.h b/src/TinyGsmClientESP8266.h index c574028..3678a97 100644 --- a/src/TinyGsmClientESP8266.h +++ b/src/TinyGsmClientESP8266.h @@ -315,7 +315,10 @@ class TinyGsmESP8266 : public TinyGsmModem, // if the status is anything but 3, there are no connections open waitResponse(); // Returns an OK after the status for (int muxNo = 0; muxNo < TINY_GSM_MUX_COUNT; muxNo++) { - sockets[muxNo]->sock_connected = false; + GsmClientESP8266* sock = sockets[muxNo]; + if (sock) { + sock->sock_connected = false; + } } return false; } @@ -336,7 +339,10 @@ class TinyGsmESP8266 : public TinyGsmModem, if (has_status == 2) break; // once we get to the ok, stop } for (int muxNo = 0; muxNo < TINY_GSM_MUX_COUNT; muxNo++) { - sockets[muxNo]->sock_connected = verified_connections[muxNo]; + GsmClientESP8266* sock = sockets[muxNo]; + if (sock) { + sock->sock_connected = verified_connections[muxNo]; + } } return verified_connections[mux]; } diff --git a/src/TinyGsmClientSIM5360.h b/src/TinyGsmClientSIM5360.h index d282ae0..9192c1d 100644 --- a/src/TinyGsmClientSIM5360.h +++ b/src/TinyGsmClientSIM5360.h @@ -217,8 +217,13 @@ class TinyGsmSim5360 : public TinyGsmModem, bool restartImpl() { if (!testAT()) { return false; } sendAT(GF("+REBOOT")); + // Should return an 'OK' after reboot command is sent if (waitResponse(10000L) != 1) { return false; } - delay(3000L); // TODO(?): Test this delay! + // After booting, modem sends out messages as each of its + // internal modules loads. The final message is "PB DONE". + if (waitResponse(40000L, GF(GSM_NL "PB DONE")) != 1) { + return false; + } return init(); } @@ -574,7 +579,13 @@ class TinyGsmSim5360 : public TinyGsmModem, if (waitResponse(GF("+CIPCLOSE:")) != 1) { return false; } for (int muxNo = 0; muxNo < TINY_GSM_MUX_COUNT; muxNo++) { // +CIPCLOSE:,,..., - sockets[muxNo]->sock_connected = stream.parseInt(); + bool thisMuxState = stream.parseInt(); + // Need to make sure a socket instace for the socket number exists + // before setting its state + GsmClientSim5360* sock = sockets[muxNo]; + if (sock) { + sock->sock_connected = thisMuxState; + } } waitResponse(); // Should be an OK at the end return sockets[mux]->sock_connected; diff --git a/src/TinyGsmClientSIM7600.h b/src/TinyGsmClientSIM7600.h index 99138a0..e44106e 100644 --- a/src/TinyGsmClientSIM7600.h +++ b/src/TinyGsmClientSIM7600.h @@ -672,7 +672,13 @@ class TinyGsmSim7600 : public TinyGsmModem, } for (int muxNo = 0; muxNo < TINY_GSM_MUX_COUNT; muxNo++) { // +CIPCLOSE:,,..., - sockets[muxNo]->sock_connected = stream.parseInt(); + bool thisMuxState = stream.parseInt(); + // Need to make sure a socket instace for the socket number exists + // before setting its state + GsmClientSim7600* sock = sockets[muxNo]; + if (sock) { + sock->sock_connected = thisMuxState; + } } waitResponse(); // Should be an OK at the end return sockets[mux]->sock_connected; diff --git a/src/TinyGsmClientSequansMonarch.h b/src/TinyGsmClientSequansMonarch.h index d8790a7..f644c33 100644 --- a/src/TinyGsmClientSequansMonarch.h +++ b/src/TinyGsmClientSequansMonarch.h @@ -571,7 +571,9 @@ class TinyGsmSequansMonarch // six possible sockets. sendAT(GF("+SQNSS")); for (int muxNo = 1; muxNo <= TINY_GSM_MUX_COUNT; muxNo++) { - if (waitResponse(GFP(GSM_OK), GF(GSM_NL "+SQNSS: ")) != 2) { break; } + if (waitResponse(GFP(GSM_OK), GF(GSM_NL "+SQNSS: ")) != 2) { + break; + } uint8_t status = 0; // if (streamGetIntBefore(',') != muxNo) { // check the mux no // DBG("### Warning: misaligned mux numbers!"); @@ -588,28 +590,31 @@ class TinyGsmSequansMonarch // SOCK_LISTENING = 4, // SOCK_INCOMING = 5, // SOCK_OPENING = 6, - sockets[muxNo % TINY_GSM_MUX_COUNT]->sock_connected = - ((status != SOCK_CLOSED) && (status != SOCK_INCOMING) && - (status != SOCK_OPENING)); + GsmClientSequansMonarch* sock = sockets[mux % TINY_GSM_MUX_COUNT]; + if (sock) { + sock->sock_connected = + ((status != SOCK_CLOSED) && (status != SOCK_INCOMING) && + (status != SOCK_OPENING)); + } } waitResponse(); // Should be an OK at the end return sockets[mux % TINY_GSM_MUX_COUNT]->sock_connected; } - /* - * Utilities - */ - public: - // TODO(vshymanskyy): Optimize this! - int8_t waitResponse(uint32_t timeout_ms, String& data, - GsmConstStr r1 = GFP(GSM_OK), - GsmConstStr r2 = GFP(GSM_ERROR), + /* + * Utilities + */ + public: + // TODO(vshymanskyy): Optimize this! + int8_t waitResponse(uint32_t timeout_ms, String & data, + GsmConstStr r1 = GFP(GSM_OK), + GsmConstStr r2 = GFP(GSM_ERROR), #if defined TINY_GSM_DEBUG - GsmConstStr r3 = GFP(GSM_CME_ERROR), + GsmConstStr r3 = GFP(GSM_CME_ERROR), #else - GsmConstStr r3 = NULL, + GsmConstStr r3 = NULL, #endif - GsmConstStr r4 = NULL, GsmConstStr r5 = NULL) { + GsmConstStr r4 = NULL, GsmConstStr r5 = NULL) { /*String r1s(r1); r1s.trim(); String r2s(r2); r2s.trim(); String r3s(r3); r3s.trim(); @@ -705,6 +710,6 @@ class TinyGsmSequansMonarch Stream& stream; GsmClientSequansMonarch* sockets[TINY_GSM_MUX_COUNT]; const char* gsmNL = GSM_NL; -}; + }; #endif // SRC_TINYGSMCLIENTSEQUANSMONARCH_H_