From 326b8ff7285fe9dfbe60fb1641e73638ecec1502 Mon Sep 17 00:00:00 2001 From: likewise Date: Wed, 4 Jun 2003 19:09:11 +0000 Subject: [PATCH] Fixed a memory leak when only a part of a PBUF_POOL chain could be allocated. --- src/core/pbuf.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/core/pbuf.c b/src/core/pbuf.c index cf00cbde..41da5236 100644 --- a/src/core/pbuf.c +++ b/src/core/pbuf.c @@ -251,6 +251,8 @@ pbuf_alloc(pbuf_layer l, u16_t length, pbuf_flag flag) p->len = length > PBUF_POOL_BUFSIZE - offset? PBUF_POOL_BUFSIZE - offset: length; /* set pbuf type */ p->flags = PBUF_FLAG_POOL; + /* set reference count (needed here in case we fail) */ + p->ref = 1; /* now allocate the tail of the pbuf chain */ @@ -266,11 +268,12 @@ pbuf_alloc(pbuf_layer l, u16_t length, pbuf_flag flag) #ifdef PBUF_STATS ++lwip_stats.pbuf.err; #endif /* PBUF_STATS */ - /* bail out unsuccesfully */ + /* free chain so far allocated */ pbuf_free(p); + /* bail out unsuccesfully */ return NULL; } - /*q->next = NULL;*/ + q->next = NULL; /* make previous pbuf point to this pbuf */ r->next = q; /* set total length of this pbuf and next in chain */ @@ -326,6 +329,7 @@ pbuf_alloc(pbuf_layer l, u16_t length, pbuf_flag flag) LWIP_ASSERT("pbuf_alloc: erroneous flag", 0); return NULL; } + /* set reference count */ p->ref = 1; return p; }