Skip to content

Commit c5ade97

Browse files
Do not crash if no userdata is passed to the printXXXCb callbacks
Previously, the userdata parameter was used as-is, interpreted as a Print*. If no userdata was supplied when registering the callback, NULL is passed, resulting an invalid method lookup and a call to a random piece of memory (often resulting in a lockup). By checking for this, forgetting to pass userdata will not lockup, but also not show any error message (since there is no obvious place to print it to...). If an invalid pointer is passed, things will still break, of course.
1 parent 4485bb1 commit c5ade97

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

Printers.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,14 @@ void printHex(Print& p, const uint8_t* buf, size_t len, const __FlashStringHelpe
2424

2525
void printErrorCb(uint8_t code, uintptr_t data) {
2626
Print *p = (Print*)data;
27+
if (!p) return;
2728
p->print(F("Error reading API packet. Error code: "));
2829
p->println(code);
2930
}
3031

3132
void printErrorCb(ZBTxStatusResponse& r, uintptr_t data) {
3233
Print *p = (Print*)data;
34+
if (!p) return;
3335
if (!r.isSuccess()) {
3436
p->print(F("Error sending Zigbee packet. Delivery status: "));
3537
p->println(r.getDeliveryStatus());
@@ -38,6 +40,7 @@ void printErrorCb(ZBTxStatusResponse& r, uintptr_t data) {
3840

3941
void printErrorCb(TxStatusResponse& r, uintptr_t data) {
4042
Print *p = (Print*)data;
43+
if (!p) return;
4144
if (!r.isSuccess()) {
4245
p->print(F("Error sending packet. Delivery status: "));
4346
p->println(r.getStatus());
@@ -46,6 +49,7 @@ void printErrorCb(TxStatusResponse& r, uintptr_t data) {
4649

4750
void printErrorCb(AtCommandResponse& r, uintptr_t data) {
4851
Print *p = (Print*)data;
52+
if (!p) return;
4953
if (!r.isOk()) {
5054
p->print(F("Error sending "));
5155
p->write(r.getCommand(), 2);
@@ -56,6 +60,7 @@ void printErrorCb(AtCommandResponse& r, uintptr_t data) {
5660

5761
void printErrorCb(RemoteAtCommandResponse& r, uintptr_t data) {
5862
Print *p = (Print*)data;
63+
if (!p) return;
5964
if (!r.isOk()) {
6065
p->print(F("Error sending remote "));
6166
p->write(r.getCommand(), 2);
@@ -90,6 +95,7 @@ void printErrorCb(XBeeResponse& r, uintptr_t data) {
9095

9196
void printRawResponseCb(XBeeResponse& response, uintptr_t data) {
9297
Print *p = (Print*)data;
98+
if (!p) return;
9399
p->print("Response: ");
94100
// Reconstruct the original packet
95101
uint8_t header[] = {START_BYTE, response.getMsbLength(), response.getLsbLength(), response.getApiId()};
@@ -114,6 +120,7 @@ static void printField(Print* p, const __FlashStringHelper *prefix, T data) {
114120

115121
void printResponseCb(ZBTxStatusResponse& status, uintptr_t data) {
116122
Print *p = (Print*)data;
123+
if (!p) return;
117124
p->println(F("ZBTxStatusResponse:"));
118125
printField(p, F(" FrameId: 0x"), status.getFrameId());
119126
printField(p, F(" To: 0x"), status.getRemoteAddress());
@@ -123,6 +130,7 @@ void printResponseCb(ZBTxStatusResponse& status, uintptr_t data) {
123130

124131
void printResponseCb(ZBRxResponse& rx, uintptr_t data) {
125132
Print *p = (Print*)data;
133+
if (!p) return;
126134
p->println(F("ZBRxResponse:"));
127135
printField(p, F(" From: 0x"), rx.getRemoteAddress64());
128136
printField(p, F(" From: 0x"), rx.getRemoteAddress16());
@@ -137,6 +145,7 @@ void printResponseCb(ZBRxResponse& rx, uintptr_t data) {
137145

138146
void printResponseCb(ZBExplicitRxResponse& rx, uintptr_t data) {
139147
Print *p = (Print*)data;
148+
if (!p) return;
140149
p->println(F("ZBExplicitRxResponse:"));
141150
printField(p, F(" From: 0x"), rx.getRemoteAddress64());
142151
printField(p, F(" From: 0x"), rx.getRemoteAddress16());
@@ -155,6 +164,7 @@ void printResponseCb(ZBExplicitRxResponse& rx, uintptr_t data) {
155164

156165
void printResponseCb(ZBRxIoSampleResponse& rx, uintptr_t data) {
157166
Print *p = (Print*)data;
167+
if (!p) return;
158168
p->println(F("ZBRxIoSampleResponse:"));
159169
printField(p, F(" From: 0x"), rx.getRemoteAddress64());
160170
printField(p, F(" From: 0x"), rx.getRemoteAddress16());
@@ -181,13 +191,15 @@ void printResponseCb(ZBRxIoSampleResponse& rx, uintptr_t data) {
181191

182192
void printResponseCb(TxStatusResponse& status, uintptr_t data) {
183193
Print *p = (Print*)data;
194+
if (!p) return;
184195
p->println(F("TxStatusResponse:"));
185196
printField(p, F(" FrameId: 0x"), status.getFrameId());
186197
printField(p, F(" Status: 0x"), status.getStatus());
187198
}
188199

189200
void printResponseCb(Rx16Response& rx, uintptr_t data) {
190201
Print *p = (Print*)data;
202+
if (!p) return;
191203
p->println("Rx16Response:");
192204
printField(p, F(" From: 0x"), rx.getRemoteAddress16());
193205
printField(p, F(" Rssi: 0x"), rx.getRssi());
@@ -202,6 +214,7 @@ void printResponseCb(Rx16Response& rx, uintptr_t data) {
202214

203215
void printResponseCb(Rx64Response& rx, uintptr_t data) {
204216
Print *p = (Print*)data;
217+
if (!p) return;
205218
p->println("Rx64Response:");
206219
printField(p, F(" From: 0x"), rx.getRemoteAddress64());
207220
printField(p, F(" Rssi: 0x"), rx.getRssi());
@@ -247,6 +260,7 @@ static void printSamples(Print* p, RxIoSampleBaseResponse& rx) {
247260

248261
void printResponseCb(Rx16IoSampleResponse& rx, uintptr_t data) {
249262
Print *p = (Print*)data;
263+
if (!p) return;
250264
p->println("Rx16IoSampleResponse:");
251265
printField(p, F(" From: 0x"), rx.getRemoteAddress16());
252266
printField(p, F(" Rssi: 0x"), rx.getRssi());
@@ -257,6 +271,7 @@ void printResponseCb(Rx16IoSampleResponse& rx, uintptr_t data) {
257271

258272
void printResponseCb(Rx64IoSampleResponse& rx, uintptr_t data) {
259273
Print *p = (Print*)data;
274+
if (!p) return;
260275
p->println("Rx64IoSampleResponse:");
261276
printField(p, F(" From: 0x"), rx.getRemoteAddress64());
262277
printField(p, F(" Rssi: 0x"), rx.getRssi());
@@ -267,12 +282,14 @@ void printResponseCb(Rx64IoSampleResponse& rx, uintptr_t data) {
267282

268283
void printResponseCb(ModemStatusResponse& status, uintptr_t data) {
269284
Print *p = (Print*)data;
285+
if (!p) return;
270286
p->println("ModemStatusResponse:");
271287
printField(p, F(" Status: 0x"), status.getStatus());
272288
}
273289

274290
void printResponseCb(AtCommandResponse& at, uintptr_t data) {
275291
Print *p = (Print*)data;
292+
if (!p) return;
276293
p->println("AtCommandResponse:");
277294
p->print(F(" Command: "));
278295
p->write(at.getCommand(), 2);
@@ -287,6 +304,7 @@ void printResponseCb(AtCommandResponse& at, uintptr_t data) {
287304

288305
void printResponseCb(RemoteAtCommandResponse& at, uintptr_t data) {
289306
Print *p = (Print*)data;
307+
if (!p) return;
290308
p->println("AtRemoteCommandResponse:");
291309
printField(p, F(" To: 0x"), at.getRemoteAddress64());
292310
printField(p, F(" To: 0x"), at.getRemoteAddress16());

0 commit comments

Comments
 (0)