Skip to content

Commit ffb4f59

Browse files
committed
Do not use pointer if memory allocation fails
[pull request 125](me-no-dev#125)
1 parent c732402 commit ffb4f59

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

src/AsyncTCP.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,9 @@ static bool _start_async_task(){
247247

248248
static int8_t _tcp_clear_events(void * arg) {
249249
lwip_event_packet_t * e = (lwip_event_packet_t *)malloc(sizeof(lwip_event_packet_t));
250+
if (e == NULL) {
251+
return ERR_MEM;
252+
}
250253
e->event = LWIP_TCP_CLEAR;
251254
e->arg = arg;
252255
if (!_prepend_async_event(&e)) {
@@ -258,6 +261,9 @@ static int8_t _tcp_clear_events(void * arg) {
258261
static int8_t _tcp_connected(void * arg, tcp_pcb * pcb, int8_t err) {
259262
//ets_printf("+C: 0x%08x\n", pcb);
260263
lwip_event_packet_t * e = (lwip_event_packet_t *)malloc(sizeof(lwip_event_packet_t));
264+
if (e == NULL) {
265+
return ERR_MEM;
266+
}
261267
e->event = LWIP_TCP_CONNECTED;
262268
e->arg = arg;
263269
e->connected.pcb = pcb;
@@ -271,6 +277,9 @@ static int8_t _tcp_connected(void * arg, tcp_pcb * pcb, int8_t err) {
271277
static int8_t _tcp_poll(void * arg, struct tcp_pcb * pcb) {
272278
//ets_printf("+P: 0x%08x\n", pcb);
273279
lwip_event_packet_t * e = (lwip_event_packet_t *)malloc(sizeof(lwip_event_packet_t));
280+
if (e == NULL) {
281+
return ERR_MEM;
282+
}
274283
e->event = LWIP_TCP_POLL;
275284
e->arg = arg;
276285
e->poll.pcb = pcb;
@@ -282,6 +291,9 @@ static int8_t _tcp_poll(void * arg, struct tcp_pcb * pcb) {
282291

283292
static int8_t _tcp_recv(void * arg, struct tcp_pcb * pcb, struct pbuf *pb, int8_t err) {
284293
lwip_event_packet_t * e = (lwip_event_packet_t *)malloc(sizeof(lwip_event_packet_t));
294+
if (e == NULL) {
295+
return ERR_MEM;
296+
}
285297
e->arg = arg;
286298
if(pb){
287299
//ets_printf("+R: 0x%08x\n", pcb);
@@ -306,6 +318,9 @@ static int8_t _tcp_recv(void * arg, struct tcp_pcb * pcb, struct pbuf *pb, int8_
306318
static int8_t _tcp_sent(void * arg, struct tcp_pcb * pcb, uint16_t len) {
307319
//ets_printf("+S: 0x%08x\n", pcb);
308320
lwip_event_packet_t * e = (lwip_event_packet_t *)malloc(sizeof(lwip_event_packet_t));
321+
if (e == NULL) {
322+
return ERR_MEM;
323+
}
309324
e->event = LWIP_TCP_SENT;
310325
e->arg = arg;
311326
e->sent.pcb = pcb;
@@ -319,6 +334,9 @@ static int8_t _tcp_sent(void * arg, struct tcp_pcb * pcb, uint16_t len) {
319334
static void _tcp_error(void * arg, int8_t err) {
320335
//ets_printf("+E: 0x%08x\n", arg);
321336
lwip_event_packet_t * e = (lwip_event_packet_t *)malloc(sizeof(lwip_event_packet_t));
337+
if (e == NULL) {
338+
return /*ERR_MEM*/;
339+
}
322340
e->event = LWIP_TCP_ERROR;
323341
e->arg = arg;
324342
e->error.err = err;
@@ -329,6 +347,9 @@ static void _tcp_error(void * arg, int8_t err) {
329347

330348
static void _tcp_dns_found(const char * name, struct ip_addr * ipaddr, void * arg) {
331349
lwip_event_packet_t * e = (lwip_event_packet_t *)malloc(sizeof(lwip_event_packet_t));
350+
if (e == NULL) {
351+
return /*ERR_MEM*/;
352+
}
332353
//ets_printf("+DNS: name=%s ipaddr=0x%08x arg=%x\n", name, ipaddr, arg);
333354
e->event = LWIP_TCP_DNS;
334355
e->arg = arg;
@@ -346,6 +367,9 @@ static void _tcp_dns_found(const char * name, struct ip_addr * ipaddr, void * ar
346367
//Used to switch out from LwIP thread
347368
static int8_t _tcp_accept(void * arg, AsyncClient * client) {
348369
lwip_event_packet_t * e = (lwip_event_packet_t *)malloc(sizeof(lwip_event_packet_t));
370+
if (e == NULL) {
371+
return ERR_MEM;
372+
}
349373
e->event = LWIP_TCP_ACCEPT;
350374
e->arg = arg;
351375
e->accept.client = client;

0 commit comments

Comments
 (0)