Skip to content

Commit fb3f858

Browse files
committed
Some more null pointer checks, fix #186
Signed-off-by: Sara Damiano <sdamiano@stroudcenter.org>
1 parent b9473c6 commit fb3f858

13 files changed

+98
-59
lines changed

src/TinyGsmClientA6.h

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -471,7 +471,7 @@ class TinyGsmA6 : public TinyGsmModem<TinyGsmA6>,
471471
String r5s(r5); r5s.trim();
472472
DBG("### ..:", r1s, ",", r2s, ",", r3s, ",", r4s, ",", r5s);*/
473473
data.reserve(64);
474-
uint8_t index = 0;
474+
uint8_t index = 0;
475475
uint32_t startMillis = millis();
476476
do {
477477
TINY_GSM_YIELD();
@@ -501,24 +501,28 @@ class TinyGsmA6 : public TinyGsmModem<TinyGsmA6>,
501501
index = 5;
502502
goto finish;
503503
} else if (data.endsWith(GF("+CIPRCV:"))) {
504-
int8_t mux = streamGetIntBefore(',');
505-
int16_t len = streamGetIntBefore(',');
504+
int8_t mux = streamGetIntBefore(',');
505+
int16_t len = streamGetIntBefore(',');
506506
int16_t len_orig = len;
507-
if (len > sockets[mux]->rx.free()) {
508-
DBG("### Buffer overflow: ", len, "->", sockets[mux]->rx.free());
509-
} else {
510-
DBG("### Got: ", len, "->", sockets[mux]->rx.free());
511-
}
512-
while (len--) { moveCharFromStreamToFifo(mux); }
513-
// TODO(?) Deal with missing characters
514-
if (len_orig > sockets[mux]->available()) {
515-
DBG("### Fewer characters received than expected: ",
516-
sockets[mux]->available(), " vs ", len_orig);
507+
if (mux >= 0 && mux < TINY_GSM_MUX_COUNT && sockets[mux]) {
508+
if (len > sockets[mux]->rx.free()) {
509+
DBG("### Buffer overflow: ", len, "->", sockets[mux]->rx.free());
510+
} else {
511+
DBG("### Got: ", len, "->", sockets[mux]->rx.free());
512+
}
513+
while (len--) {
514+
moveCharFromStreamToFifo(mux);
515+
}
516+
// TODO(?) Deal with missing characters
517+
if (len_orig > sockets[mux]->available()) {
518+
DBG("### Fewer characters received than expected: ",
519+
sockets[mux]->available(), " vs ", len_orig);
520+
}
517521
}
518522
data = "";
519523
} else if (data.endsWith(GF("+TCPCLOSED:"))) {
520524
int8_t mux = streamGetIntBefore('\n');
521-
if (mux >= 0 && mux < TINY_GSM_MUX_COUNT) {
525+
if (mux >= 0 && mux < TINY_GSM_MUX_COUNT && sockets[mux]) {
522526
sockets[mux]->sock_connected = false;
523527
}
524528
data = "";
@@ -529,7 +533,9 @@ class TinyGsmA6 : public TinyGsmModem<TinyGsmA6>,
529533
finish:
530534
if (!index) {
531535
data.trim();
532-
if (data.length()) { DBG("### Unhandled:", data); }
536+
if (data.length()) {
537+
DBG("### Unhandled:", data);
538+
}
533539
data = "";
534540
}
535541
// data.replace(GSM_NL, "/");
@@ -561,10 +567,11 @@ class TinyGsmA6 : public TinyGsmModem<TinyGsmA6>,
561567
}
562568

563569
public:
564-
Stream& stream;
570+
Stream& stream;
571+
565572
protected:
566573
GsmClientA6* sockets[TINY_GSM_MUX_COUNT];
567-
const char* gsmNL = GSM_NL;
574+
const char* gsmNL = GSM_NL;
568575
};
569576

570577
#endif // SRC_TINYGSMCLIENTA6_H_

src/TinyGsmClientBG96.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -522,6 +522,7 @@ class TinyGsmBG96 : public TinyGsmModem<TinyGsmBG96>,
522522
}
523523

524524
size_t modemRead(size_t size, uint8_t mux) {
525+
if (!sockets[mux]) return 0;
525526
sendAT(GF("+QIRD="), mux, ',', (uint16_t)size);
526527
if (waitResponse(GF("+QIRD:")) != 1) { return 0; }
527528
int16_t len = streamGetIntBefore('\n');
@@ -534,6 +535,7 @@ class TinyGsmBG96 : public TinyGsmModem<TinyGsmBG96>,
534535
}
535536

536537
size_t modemGetAvailable(uint8_t mux) {
538+
if (!sockets[mux]) return 0;
537539
sendAT(GF("+QIRD="), mux, GF(",0"));
538540
size_t result = 0;
539541
if (waitResponse(GF("+QIRD:")) == 1) {
@@ -675,6 +677,7 @@ class TinyGsmBG96 : public TinyGsmModem<TinyGsmBG96>,
675677

676678
public:
677679
Stream& stream;
680+
678681
protected:
679682
GsmClientBG96* sockets[TINY_GSM_MUX_COUNT];
680683
const char* gsmNL = GSM_NL;

src/TinyGsmClientESP8266.h

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -315,9 +315,8 @@ class TinyGsmESP8266 : public TinyGsmModem<TinyGsmESP8266>,
315315
// if the status is anything but 3, there are no connections open
316316
waitResponse(); // Returns an OK after the status
317317
for (int muxNo = 0; muxNo < TINY_GSM_MUX_COUNT; muxNo++) {
318-
GsmClientESP8266* sock = sockets[muxNo];
319-
if (sock) {
320-
sock->sock_connected = false;
318+
if (sockets[muxNo]) {
319+
sockets[muxNo]->sock_connected = false;
321320
}
322321
}
323322
return false;
@@ -339,9 +338,8 @@ class TinyGsmESP8266 : public TinyGsmModem<TinyGsmESP8266>,
339338
if (has_status == 2) break; // once we get to the ok, stop
340339
}
341340
for (int muxNo = 0; muxNo < TINY_GSM_MUX_COUNT; muxNo++) {
342-
GsmClientESP8266* sock = sockets[muxNo];
343-
if (sock) {
344-
sock->sock_connected = verified_connections[muxNo];
341+
if (sockets[muxNo]) {
342+
sockets[muxNo]->sock_connected = verified_connections[muxNo];
345343
}
346344
}
347345
return verified_connections[mux];
@@ -391,17 +389,21 @@ class TinyGsmESP8266 : public TinyGsmModem<TinyGsmESP8266>,
391389
int8_t mux = streamGetIntBefore(',');
392390
int16_t len = streamGetIntBefore(':');
393391
int16_t len_orig = len;
394-
if (len > sockets[mux]->rx.free()) {
395-
DBG("### Buffer overflow: ", len, "received vs",
396-
sockets[mux]->rx.free(), "available");
397-
} else {
398-
// DBG("### Got Data: ", len, "on", mux);
399-
}
400-
while (len--) { moveCharFromStreamToFifo(mux); }
401-
// TODO(SRGDamia1): deal with buffer overflow/missed characters
402-
if (len_orig > sockets[mux]->available()) {
403-
DBG("### Fewer characters received than expected: ",
404-
sockets[mux]->available(), " vs ", len_orig);
392+
if (mux >= 0 && mux < TINY_GSM_MUX_COUNT && sockets[mux]) {
393+
if (len > sockets[mux]->rx.free()) {
394+
DBG("### Buffer overflow: ", len, "received vs",
395+
sockets[mux]->rx.free(), "available");
396+
} else {
397+
// DBG("### Got Data: ", len, "on", mux);
398+
}
399+
while (len--) {
400+
moveCharFromStreamToFifo(mux);
401+
}
402+
// TODO(SRGDamia1): deal with buffer overflow/missed characters
403+
if (len_orig > sockets[mux]->available()) {
404+
DBG("### Fewer characters received than expected: ",
405+
sockets[mux]->available(), " vs ", len_orig);
406+
}
405407
}
406408
data = "";
407409
} else if (data.endsWith(GF("CLOSED"))) {
@@ -443,6 +445,7 @@ class TinyGsmESP8266 : public TinyGsmModem<TinyGsmESP8266>,
443445

444446
public:
445447
Stream& stream;
448+
446449
protected:
447450
GsmClientESP8266* sockets[TINY_GSM_MUX_COUNT];
448451
const char* gsmNL = GSM_NL;

src/TinyGsmClientM590.h

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -395,22 +395,26 @@ class TinyGsmM590 : public TinyGsmModem<TinyGsmM590>,
395395
int8_t mux = streamGetIntBefore(',');
396396
int16_t len = streamGetIntBefore(',');
397397
int16_t len_orig = len;
398-
if (len > sockets[mux]->rx.free()) {
399-
DBG("### Buffer overflow: ", len, "->", sockets[mux]->rx.free());
400-
} else {
401-
DBG("### Got: ", len, "->", sockets[mux]->rx.free());
402-
}
403-
while (len--) { moveCharFromStreamToFifo(mux); }
404-
// TODO(?): Handle lost characters
405-
if (len_orig > sockets[mux]->available()) {
406-
DBG("### Fewer characters received than expected: ",
407-
sockets[mux]->available(), " vs ", len_orig);
398+
if (mux >= 0 && mux < TINY_GSM_MUX_COUNT && sockets[mux]) {
399+
if (len > sockets[mux]->rx.free()) {
400+
DBG("### Buffer overflow: ", len, "->", sockets[mux]->rx.free());
401+
} else {
402+
DBG("### Got: ", len, "->", sockets[mux]->rx.free());
403+
}
404+
while (len--) {
405+
moveCharFromStreamToFifo(mux);
406+
}
407+
// TODO(?): Handle lost characters
408+
if (len_orig > sockets[mux]->available()) {
409+
DBG("### Fewer characters received than expected: ",
410+
sockets[mux]->available(), " vs ", len_orig);
411+
}
408412
}
409413
data = "";
410414
} else if (data.endsWith(GF("+TCPCLOSE:"))) {
411415
int8_t mux = streamGetIntBefore(',');
412416
streamSkipUntil('\n');
413-
if (mux >= 0 && mux < TINY_GSM_MUX_COUNT) {
417+
if (mux >= 0 && mux < TINY_GSM_MUX_COUNT && sockets[mux]) {
414418
sockets[mux]->sock_connected = false;
415419
}
416420
data = "";
@@ -454,6 +458,7 @@ class TinyGsmM590 : public TinyGsmModem<TinyGsmM590>,
454458

455459
public:
456460
Stream& stream;
461+
457462
protected:
458463
GsmClientM590* sockets[TINY_GSM_MUX_COUNT];
459464
const char* gsmNL = GSM_NL;

src/TinyGsmClientM95.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -445,6 +445,7 @@ class TinyGsmM95 : public TinyGsmModem<TinyGsmM95>,
445445
}
446446

447447
size_t modemRead(size_t size, uint8_t mux) {
448+
if (!sockets[mux]) return 0;
448449
// TODO(?): Does this work????
449450
// AT+QIRD=<id>,<sc>,<sid>,<len>
450451
// id = GPRS context number = 0, set in GPRS connect
@@ -482,6 +483,7 @@ class TinyGsmM95 : public TinyGsmModem<TinyGsmM95>,
482483
}
483484
}
484485

486+
// Not possible to check the number of characters remaining in buffer
485487
size_t modemGetAvailable(uint8_t) {
486488
return 0;
487489
}
@@ -618,6 +620,7 @@ class TinyGsmM95 : public TinyGsmModem<TinyGsmM95>,
618620

619621
public:
620622
Stream& stream;
623+
621624
protected:
622625
GsmClientM95* sockets[TINY_GSM_MUX_COUNT];
623626
const char* gsmNL = GSM_NL;

src/TinyGsmClientMC60.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -405,7 +405,8 @@ class TinyGsmMC60 : public TinyGsmModem<TinyGsmMC60>,
405405
}
406406

407407
size_t modemRead(size_t size, uint8_t mux) {
408-
// TODO(?): Does this work????
408+
if (!sockets[mux]) return 0;
409+
// TODO(?): Does this even work????
409410
// AT+QIRD=<id>,<sc>,<sid>,<len>
410411
// id = GPRS context number = 0, set in GPRS connect
411412
// sc = role in connection = 1, client of connection
@@ -441,6 +442,7 @@ class TinyGsmMC60 : public TinyGsmModem<TinyGsmMC60>,
441442
}
442443
}
443444

445+
// Not possible to check the number of characters remaining in buffer
444446
size_t modemGetAvailable(uint8_t) {
445447
return 0;
446448
}
@@ -590,6 +592,7 @@ class TinyGsmMC60 : public TinyGsmModem<TinyGsmMC60>,
590592

591593
public:
592594
Stream& stream;
595+
593596
protected:
594597
GsmClientMC60* sockets[TINY_GSM_MUX_COUNT];
595598
const char* gsmNL = GSM_NL;

src/TinyGsmClientSIM5360.h

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -517,6 +517,7 @@ class TinyGsmSim5360 : public TinyGsmModem<TinyGsmSim5360>,
517517
}
518518

519519
size_t modemRead(size_t size, uint8_t mux) {
520+
if (!sockets[mux]) return 0;
520521
#ifdef TINY_GSM_USE_HEX
521522
sendAT(GF("+CIPRXGET=3,"), mux, ',', (uint16_t)size);
522523
if (waitResponse(GF("+CIPRXGET:")) != 1) { return 0; }
@@ -560,6 +561,7 @@ class TinyGsmSim5360 : public TinyGsmModem<TinyGsmSim5360>,
560561
}
561562

562563
size_t modemGetAvailable(uint8_t mux) {
564+
if (!sockets[mux]) return 0;
563565
sendAT(GF("+CIPRXGET=4,"), mux);
564566
size_t result = 0;
565567
if (waitResponse(GF("+CIPRXGET:")) == 1) {
@@ -579,15 +581,13 @@ class TinyGsmSim5360 : public TinyGsmModem<TinyGsmSim5360>,
579581
if (waitResponse(GF("+CIPCLOSE:")) != 1) { return false; }
580582
for (int muxNo = 0; muxNo < TINY_GSM_MUX_COUNT; muxNo++) {
581583
// +CIPCLOSE:<link0_state>,<link1_state>,...,<link9_state>
582-
bool thisMuxState = stream.parseInt();
583-
// Need to make sure a socket instace for the socket number exists
584-
// before setting its state
585-
GsmClientSim5360* sock = sockets[muxNo];
586-
if (sock) {
587-
sock->sock_connected = thisMuxState;
584+
bool muxState = stream.parseInt();
585+
if (sockets[muxNo]) {
586+
sockets[muxNo]->sock_connected = muxState;
588587
}
589588
}
590589
waitResponse(); // Should be an OK at the end
590+
if (!sockets[mux]) return false;
591591
return sockets[mux]->sock_connected;
592592
}
593593

@@ -715,6 +715,7 @@ class TinyGsmSim5360 : public TinyGsmModem<TinyGsmSim5360>,
715715

716716
public:
717717
Stream& stream;
718+
718719
protected:
719720
GsmClientSim5360* sockets[TINY_GSM_MUX_COUNT];
720721
const char* gsmNL = GSM_NL;

src/TinyGsmClientSIM7000.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -550,6 +550,7 @@ class TinyGsmSim7000 : public TinyGsmModem<TinyGsmSim7000>,
550550
}
551551

552552
size_t modemRead(size_t size, uint8_t mux) {
553+
if (!sockets[mux]) return 0;
553554
#ifdef TINY_GSM_USE_HEX
554555
sendAT(GF("+CIPRXGET=3,"), mux, ',', (uint16_t)size);
555556
if (waitResponse(GF("+CIPRXGET:")) != 1) { return 0; }
@@ -597,6 +598,7 @@ class TinyGsmSim7000 : public TinyGsmModem<TinyGsmSim7000>,
597598
}
598599

599600
size_t modemGetAvailable(uint8_t mux) {
601+
if (!sockets[mux]) return 0;
600602
sendAT(GF("+CIPRXGET=4,"), mux);
601603
size_t result = 0;
602604
if (waitResponse(GF("+CIPRXGET:")) == 1) {
@@ -756,6 +758,7 @@ class TinyGsmSim7000 : public TinyGsmModem<TinyGsmSim7000>,
756758

757759
public:
758760
Stream& stream;
761+
759762
protected:
760763
GsmClientSim7000* sockets[TINY_GSM_MUX_COUNT];
761764
const char* gsmNL = GSM_NL;

src/TinyGsmClientSIM7600.h

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -608,6 +608,7 @@ class TinyGsmSim7600 : public TinyGsmModem<TinyGsmSim7600>,
608608
}
609609

610610
size_t modemRead(size_t size, uint8_t mux) {
611+
if (!sockets[mux]) return 0;
611612
#ifdef TINY_GSM_USE_HEX
612613
sendAT(GF("+CIPRXGET=3,"), mux, ',', (uint16_t)size);
613614
if (waitResponse(GF("+CIPRXGET:")) != 1) { return 0; }
@@ -651,6 +652,7 @@ class TinyGsmSim7600 : public TinyGsmModem<TinyGsmSim7600>,
651652
}
652653

653654
size_t modemGetAvailable(uint8_t mux) {
655+
if (!sockets[mux]) return 0;
654656
sendAT(GF("+CIPRXGET=4,"), mux);
655657
size_t result = 0;
656658
if (waitResponse(GF("+CIPRXGET:")) == 1) {
@@ -672,15 +674,13 @@ class TinyGsmSim7600 : public TinyGsmModem<TinyGsmSim7600>,
672674
}
673675
for (int muxNo = 0; muxNo < TINY_GSM_MUX_COUNT; muxNo++) {
674676
// +CIPCLOSE:<link0_state>,<link1_state>,...,<link9_state>
675-
bool thisMuxState = stream.parseInt();
676-
// Need to make sure a socket instace for the socket number exists
677-
// before setting its state
678-
GsmClientSim7600* sock = sockets[muxNo];
679-
if (sock) {
680-
sock->sock_connected = thisMuxState;
677+
bool muxState = stream.parseInt();
678+
if (sockets[muxNo]) {
679+
sockets[muxNo]->sock_connected = muxState;
681680
}
682681
}
683682
waitResponse(); // Should be an OK at the end
683+
if (!sockets[mux]) return false;
684684
return sockets[mux]->sock_connected;
685685
}
686686

@@ -808,6 +808,7 @@ class TinyGsmSim7600 : public TinyGsmModem<TinyGsmSim7600>,
808808

809809
public:
810810
Stream& stream;
811+
811812
protected:
812813
GsmClientSim7600* sockets[TINY_GSM_MUX_COUNT];
813814
const char* gsmNL = GSM_NL;

src/TinyGsmClientSIM800.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -528,6 +528,7 @@ class TinyGsmSim800 : public TinyGsmModem<TinyGsmSim800>,
528528
}
529529

530530
size_t modemRead(size_t size, uint8_t mux) {
531+
if (!sockets[mux]) return 0;
531532
#ifdef TINY_GSM_USE_HEX
532533
sendAT(GF("+CIPRXGET=3,"), mux, ',', (uint16_t)size);
533534
if (waitResponse(GF("+CIPRXGET:")) != 1) { return 0; }
@@ -575,6 +576,7 @@ class TinyGsmSim800 : public TinyGsmModem<TinyGsmSim800>,
575576
}
576577

577578
size_t modemGetAvailable(uint8_t mux) {
579+
if (!sockets[mux]) return 0;
578580
sendAT(GF("+CIPRXGET=4,"), mux);
579581
size_t result = 0;
580582
if (waitResponse(GF("+CIPRXGET:")) == 1) {
@@ -734,6 +736,7 @@ class TinyGsmSim800 : public TinyGsmModem<TinyGsmSim800>,
734736

735737
public:
736738
Stream& stream;
739+
737740
protected:
738741
GsmClientSim800* sockets[TINY_GSM_MUX_COUNT];
739742
const char* gsmNL = GSM_NL;

0 commit comments

Comments
 (0)