From 05ee8ad65c1c7f1318e1185ddf299a2cce6c6474 Mon Sep 17 00:00:00 2001 From: Guus Sliepen Date: Thu, 29 Oct 2015 16:26:54 +0100 Subject: [PATCH] Always check the return value of malloc(). --- test.c | 4 ++++ utcp.c | 4 ++++ 2 files changed, 8 insertions(+) diff --git a/test.c b/test.c index 47732e4..3491e29 100644 --- a/test.c +++ b/test.c @@ -73,6 +73,10 @@ ssize_t do_send(struct utcp *utcp, const void *data, size_t len) { return len; if(!reorder_data && drand48() < reorder) { reorder_data = malloc(len); + if(!reorder_data) { + debug("Out of memory\n"); + return len; + } reorder_len = len; memcpy(reorder_data, data, len); reorder_countdown = 1 + drand48() * reorder_dist; diff --git a/utcp.c b/utcp.c index 607af2d..95a82c4 100644 --- a/utcp.c +++ b/utcp.c @@ -88,6 +88,10 @@ static void print_packet(struct utcp *utcp, const char *dir, const void *pkt, si if(len > sizeof hdr) { uint32_t datalen = len - sizeof hdr; uint8_t *str = malloc((datalen << 1) + 7); + if(!str) { + debug("out of memory"); + return; + } memcpy(str, " data=", 6); uint8_t *strptr = str + 6; const uint8_t *data = pkt; -- 2.39.5