Fix bug #54327: V2.1.0rc1 pbuf.c misses stdint.h include

This commit is contained in:
Dirk Ziegelmeier 2018-07-17 20:44:54 +02:00
parent cd1dd4f5b1
commit 4e74421dac

View File

@ -70,13 +70,13 @@
#include "lwip/opt.h" #include "lwip/opt.h"
#include "lwip/pbuf.h"
#include "lwip/stats.h"
#include "lwip/def.h" #include "lwip/def.h"
#include "lwip/mem.h" #include "lwip/mem.h"
#include "lwip/memp.h" #include "lwip/memp.h"
#include "lwip/sys.h"
#include "lwip/netif.h" #include "lwip/netif.h"
#include "lwip/pbuf.h"
#include "lwip/stats.h"
#include "lwip/sys.h"
#if LWIP_TCP && TCP_QUEUE_OOSEQ #if LWIP_TCP && TCP_QUEUE_OOSEQ
#include "lwip/priv/tcp_priv.h" #include "lwip/priv/tcp_priv.h"
#endif #endif
@ -86,7 +86,7 @@
#include <string.h> #include <string.h>
#define SIZEOF_STRUCT_PBUF LWIP_MEM_ALIGN_SIZE(sizeof(struct pbuf)) #define SIZEOF_STRUCT_PBUF LWIP_MEM_ALIGN_SIZE(sizeof(struct pbuf))
/* Since the pool is created in memp, PBUF_POOL_BUFSIZE will be automatically /* Since the pool is created in memp, PBUF_POOL_BUFSIZE will be automatically
aligned there. Therefore, PBUF_POOL_BUFSIZE_ALIGNED can be used here. */ aligned there. Therefore, PBUF_POOL_BUFSIZE_ALIGNED can be used here. */
#define PBUF_POOL_BUFSIZE_ALIGNED LWIP_MEM_ALIGN_SIZE(PBUF_POOL_BUFSIZE) #define PBUF_POOL_BUFSIZE_ALIGNED LWIP_MEM_ALIGN_SIZE(PBUF_POOL_BUFSIZE)
@ -101,12 +101,14 @@ pbuf_skip_const(const struct pbuf *in, u16_t in_offset, u16_t *out_offset);
#if !NO_SYS #if !NO_SYS
#ifndef PBUF_POOL_FREE_OOSEQ_QUEUE_CALL #ifndef PBUF_POOL_FREE_OOSEQ_QUEUE_CALL
#include "lwip/tcpip.h" #include "lwip/tcpip.h"
#define PBUF_POOL_FREE_OOSEQ_QUEUE_CALL() do { \ #define PBUF_POOL_FREE_OOSEQ_QUEUE_CALL() \
if (tcpip_try_callback(pbuf_free_ooseq_callback, NULL) != ERR_OK) { \ do { \
SYS_ARCH_PROTECT(old_level); \ if (tcpip_try_callback(pbuf_free_ooseq_callback, NULL) != ERR_OK) { \
pbuf_free_ooseq_pending = 0; \ SYS_ARCH_PROTECT(old_level); \
SYS_ARCH_UNPROTECT(old_level); \ pbuf_free_ooseq_pending = 0; \
} } while(0) SYS_ARCH_UNPROTECT(old_level); \
} \
} while (0)
#endif /* PBUF_POOL_FREE_OOSEQ_QUEUE_CALL */ #endif /* PBUF_POOL_FREE_OOSEQ_QUEUE_CALL */
#endif /* !NO_SYS */ #endif /* !NO_SYS */
@ -124,8 +126,8 @@ volatile u8_t pbuf_free_ooseq_pending;
#if !NO_SYS #if !NO_SYS
static static
#endif /* !NO_SYS */ #endif /* !NO_SYS */
void void
pbuf_free_ooseq(void) pbuf_free_ooseq(void)
{ {
struct tcp_pcb *pcb; struct tcp_pcb *pcb;
SYS_ARCH_SET(pbuf_free_ooseq_pending, 0); SYS_ARCH_SET(pbuf_free_ooseq_pending, 0);
@ -158,11 +160,11 @@ pbuf_pool_is_empty(void)
{ {
#ifndef PBUF_POOL_FREE_OOSEQ_QUEUE_CALL #ifndef PBUF_POOL_FREE_OOSEQ_QUEUE_CALL
SYS_ARCH_SET(pbuf_free_ooseq_pending, 1); SYS_ARCH_SET(pbuf_free_ooseq_pending, 1);
#else /* PBUF_POOL_FREE_OOSEQ_QUEUE_CALL */ #else /* PBUF_POOL_FREE_OOSEQ_QUEUE_CALL */
u8_t queued; u8_t queued;
SYS_ARCH_DECL_PROTECT(old_level); SYS_ARCH_DECL_PROTECT(old_level);
SYS_ARCH_PROTECT(old_level); SYS_ARCH_PROTECT(old_level);
queued = pbuf_free_ooseq_pending; queued = pbuf_free_ooseq_pending;
pbuf_free_ooseq_pending = 1; pbuf_free_ooseq_pending = 1;
SYS_ARCH_UNPROTECT(old_level); SYS_ARCH_UNPROTECT(old_level);
@ -178,14 +180,14 @@ pbuf_pool_is_empty(void)
static void static void
pbuf_init_alloced_pbuf(struct pbuf *p, void *payload, u16_t tot_len, u16_t len, pbuf_type type, u8_t flags) pbuf_init_alloced_pbuf(struct pbuf *p, void *payload, u16_t tot_len, u16_t len, pbuf_type type, u8_t flags)
{ {
p->next = NULL; p->next = NULL;
p->payload = payload; p->payload = payload;
p->tot_len = tot_len; p->tot_len = tot_len;
p->len = len; p->len = len;
p->type_internal = (u8_t)type; p->type_internal = (u8_t)type;
p->flags = flags; p->flags = flags;
p->ref = 1; p->ref = 1;
p->if_idx = NETIF_NO_INDEX; p->if_idx = NETIF_NO_INDEX;
} }
/** /**
@ -225,7 +227,7 @@ pbuf_alloc(pbuf_layer layer, u16_t length, pbuf_type type)
{ {
struct pbuf *p; struct pbuf *p;
u16_t offset = (u16_t)layer; u16_t offset = (u16_t)layer;
LWIP_DEBUGF(PBUF_DEBUG | LWIP_DBG_TRACE, ("pbuf_alloc(length=%"U16_F")\n", length)); LWIP_DEBUGF(PBUF_DEBUG | LWIP_DBG_TRACE, ("pbuf_alloc(length=%" U16_F ")\n", length));
switch (type) { switch (type) {
case PBUF_REF: /* fall through */ case PBUF_REF: /* fall through */
@ -235,8 +237,8 @@ pbuf_alloc(pbuf_layer layer, u16_t length, pbuf_type type)
case PBUF_POOL: { case PBUF_POOL: {
struct pbuf *q, *last; struct pbuf *q, *last;
u16_t rem_len; /* remaining length */ u16_t rem_len; /* remaining length */
p = NULL; p = NULL;
last = NULL; last = NULL;
rem_len = length; rem_len = length;
do { do {
u16_t qlen; u16_t qlen;
@ -251,12 +253,11 @@ pbuf_alloc(pbuf_layer layer, u16_t length, pbuf_type type)
return NULL; return NULL;
} }
qlen = LWIP_MIN(rem_len, (u16_t)(PBUF_POOL_BUFSIZE_ALIGNED - LWIP_MEM_ALIGN_SIZE(offset))); qlen = LWIP_MIN(rem_len, (u16_t)(PBUF_POOL_BUFSIZE_ALIGNED - LWIP_MEM_ALIGN_SIZE(offset)));
pbuf_init_alloced_pbuf(q, LWIP_MEM_ALIGN((void *)((u8_t *)q + SIZEOF_STRUCT_PBUF + offset)), pbuf_init_alloced_pbuf(
rem_len, qlen, type, 0); q, LWIP_MEM_ALIGN((void *)((u8_t *)q + SIZEOF_STRUCT_PBUF + offset)), rem_len, qlen, type, 0);
LWIP_ASSERT("pbuf_alloc: pbuf q->payload properly aligned", LWIP_ASSERT("pbuf_alloc: pbuf q->payload properly aligned", ((mem_ptr_t)q->payload % MEM_ALIGNMENT) == 0);
((mem_ptr_t)q->payload % MEM_ALIGNMENT) == 0);
LWIP_ASSERT("PBUF_POOL_BUFSIZE must be bigger than MEM_ALIGNMENT", LWIP_ASSERT("PBUF_POOL_BUFSIZE must be bigger than MEM_ALIGNMENT",
(PBUF_POOL_BUFSIZE_ALIGNED - LWIP_MEM_ALIGN_SIZE(offset)) > 0 ); (PBUF_POOL_BUFSIZE_ALIGNED - LWIP_MEM_ALIGN_SIZE(offset)) > 0);
if (p == NULL) { if (p == NULL) {
/* allocated head of pbuf chain (into p) */ /* allocated head of pbuf chain (into p) */
p = q; p = q;
@ -264,19 +265,18 @@ pbuf_alloc(pbuf_layer layer, u16_t length, pbuf_type type)
/* make previous pbuf point to this pbuf */ /* make previous pbuf point to this pbuf */
last->next = q; last->next = q;
} }
last = q; last = q;
rem_len = (u16_t)(rem_len - qlen); rem_len = (u16_t)(rem_len - qlen);
offset = 0; offset = 0;
} while (rem_len > 0); } while (rem_len > 0);
break; break;
} }
case PBUF_RAM: { case PBUF_RAM: {
u16_t payload_len = (u16_t)(LWIP_MEM_ALIGN_SIZE(offset) + LWIP_MEM_ALIGN_SIZE(length)); u16_t payload_len = (u16_t)(LWIP_MEM_ALIGN_SIZE(offset) + LWIP_MEM_ALIGN_SIZE(length));
mem_size_t alloc_len = (mem_size_t)(LWIP_MEM_ALIGN_SIZE(SIZEOF_STRUCT_PBUF) + payload_len); mem_size_t alloc_len = (mem_size_t)(LWIP_MEM_ALIGN_SIZE(SIZEOF_STRUCT_PBUF) + payload_len);
/* bug #50040: Check for integer overflow when calculating alloc_len */ /* bug #50040: Check for integer overflow when calculating alloc_len */
if ((payload_len < LWIP_MEM_ALIGN_SIZE(length)) || if ((payload_len < LWIP_MEM_ALIGN_SIZE(length)) || (alloc_len < LWIP_MEM_ALIGN_SIZE(length))) {
(alloc_len < LWIP_MEM_ALIGN_SIZE(length))) {
return NULL; return NULL;
} }
@ -285,17 +285,16 @@ pbuf_alloc(pbuf_layer layer, u16_t length, pbuf_type type)
if (p == NULL) { if (p == NULL) {
return NULL; return NULL;
} }
pbuf_init_alloced_pbuf(p, LWIP_MEM_ALIGN((void *)((u8_t *)p + SIZEOF_STRUCT_PBUF + offset)), pbuf_init_alloced_pbuf(
length, length, type, 0); p, LWIP_MEM_ALIGN((void *)((u8_t *)p + SIZEOF_STRUCT_PBUF + offset)), length, length, type, 0);
LWIP_ASSERT("pbuf_alloc: pbuf->payload properly aligned", LWIP_ASSERT("pbuf_alloc: pbuf->payload properly aligned", ((mem_ptr_t)p->payload % MEM_ALIGNMENT) == 0);
((mem_ptr_t)p->payload % MEM_ALIGNMENT) == 0);
break; break;
} }
default: default:
LWIP_ASSERT("pbuf_alloc: erroneous type", 0); LWIP_ASSERT("pbuf_alloc: erroneous type", 0);
return NULL; return NULL;
} }
LWIP_DEBUGF(PBUF_DEBUG | LWIP_DBG_TRACE, ("pbuf_alloc(length=%"U16_F") == %p\n", length, (void *)p)); LWIP_DEBUGF(PBUF_DEBUG | LWIP_DBG_TRACE, ("pbuf_alloc(length=%" U16_F ") == %p\n", length, (void *)p));
return p; return p;
} }
@ -331,16 +330,15 @@ pbuf_alloc_reference(void *payload, u16_t length, pbuf_type type)
/* only allocate memory for the pbuf structure */ /* only allocate memory for the pbuf structure */
p = (struct pbuf *)memp_malloc(MEMP_PBUF); p = (struct pbuf *)memp_malloc(MEMP_PBUF);
if (p == NULL) { if (p == NULL) {
LWIP_DEBUGF(PBUF_DEBUG | LWIP_DBG_LEVEL_SERIOUS, LWIP_DEBUGF(
("pbuf_alloc_reference: Could not allocate MEMP_PBUF for PBUF_%s.\n", PBUF_DEBUG | LWIP_DBG_LEVEL_SERIOUS,
(type == PBUF_ROM) ? "ROM" : "REF")); ("pbuf_alloc_reference: Could not allocate MEMP_PBUF for PBUF_%s.\n", (type == PBUF_ROM) ? "ROM" : "REF"));
return NULL; return NULL;
} }
pbuf_init_alloced_pbuf(p, payload, length, length, type, 0); pbuf_init_alloced_pbuf(p, payload, length, length, type, 0);
return p; return p;
} }
#if LWIP_SUPPORT_CUSTOM_PBUF #if LWIP_SUPPORT_CUSTOM_PBUF
/** /**
* @ingroup pbuf * @ingroup pbuf
@ -360,15 +358,20 @@ pbuf_alloc_reference(void *payload, u16_t length, pbuf_type type)
* big enough to hold 'length' plus the header size * big enough to hold 'length' plus the header size
*/ */
struct pbuf * struct pbuf *
pbuf_alloced_custom(pbuf_layer l, u16_t length, pbuf_type type, struct pbuf_custom *p, pbuf_alloced_custom(pbuf_layer l,
void *payload_mem, u16_t payload_mem_len) u16_t length,
pbuf_type type,
struct pbuf_custom *p,
void *payload_mem,
u16_t payload_mem_len)
{ {
u16_t offset = (u16_t)l; u16_t offset = (u16_t)l;
void *payload; void *payload;
LWIP_DEBUGF(PBUF_DEBUG | LWIP_DBG_TRACE, ("pbuf_alloced_custom(length=%"U16_F")\n", length)); LWIP_DEBUGF(PBUF_DEBUG | LWIP_DBG_TRACE, ("pbuf_alloced_custom(length=%" U16_F ")\n", length));
if (LWIP_MEM_ALIGN_SIZE(offset) + length > payload_mem_len) { if (LWIP_MEM_ALIGN_SIZE(offset) + length > payload_mem_len) {
LWIP_DEBUGF(PBUF_DEBUG | LWIP_DBG_LEVEL_WARNING, ("pbuf_alloced_custom(length=%"U16_F") buffer too short\n", length)); LWIP_DEBUGF(PBUF_DEBUG | LWIP_DBG_LEVEL_WARNING,
("pbuf_alloced_custom(length=%" U16_F ") buffer too short\n", length));
return NULL; return NULL;
} }
@ -419,7 +422,7 @@ pbuf_realloc(struct pbuf *p, u16_t new_len)
/* first, step over any pbufs that should remain in the chain */ /* first, step over any pbufs that should remain in the chain */
rem_len = new_len; rem_len = new_len;
q = p; q = p;
/* should this pbuf be kept? */ /* should this pbuf be kept? */
while (rem_len > q->len) { while (rem_len > q->len) {
/* decrease remaining length by pbuf length */ /* decrease remaining length by pbuf length */
@ -439,13 +442,13 @@ pbuf_realloc(struct pbuf *p, u16_t new_len)
#if LWIP_SUPPORT_CUSTOM_PBUF #if LWIP_SUPPORT_CUSTOM_PBUF
&& ((q->flags & PBUF_FLAG_IS_CUSTOM) == 0) && ((q->flags & PBUF_FLAG_IS_CUSTOM) == 0)
#endif /* LWIP_SUPPORT_CUSTOM_PBUF */ #endif /* LWIP_SUPPORT_CUSTOM_PBUF */
) { ) {
/* reallocate and adjust the length of the pbuf that will be split */ /* reallocate and adjust the length of the pbuf that will be split */
q = (struct pbuf *)mem_trim(q, (mem_size_t)(((u8_t *)q->payload - (u8_t *)q) + rem_len)); q = (struct pbuf *)mem_trim(q, (mem_size_t)(((u8_t *)q->payload - (u8_t *)q) + rem_len));
LWIP_ASSERT("mem_trim returned q == NULL", q != NULL); LWIP_ASSERT("mem_trim returned q == NULL", q != NULL);
} }
/* adjust length fields for new last pbuf */ /* adjust length fields for new last pbuf */
q->len = rem_len; q->len = rem_len;
q->tot_len = q->len; q->tot_len = q->len;
/* any remaining pbufs in chain? */ /* any remaining pbufs in chain? */
@ -455,7 +458,6 @@ pbuf_realloc(struct pbuf *p, u16_t new_len)
} }
/* q is last packet in chain */ /* q is last packet in chain */
q->next = NULL; q->next = NULL;
} }
/** /**
@ -498,9 +500,10 @@ pbuf_add_header_impl(struct pbuf *p, size_t header_size_increment, u8_t force)
payload = (u8_t *)p->payload - header_size_increment; payload = (u8_t *)p->payload - header_size_increment;
/* boundary check fails? */ /* boundary check fails? */
if ((u8_t *)payload < (u8_t *)p + SIZEOF_STRUCT_PBUF) { if ((u8_t *)payload < (u8_t *)p + SIZEOF_STRUCT_PBUF) {
LWIP_DEBUGF( PBUF_DEBUG | LWIP_DBG_TRACE, LWIP_DEBUGF(PBUF_DEBUG | LWIP_DBG_TRACE,
("pbuf_add_header: failed as %p < %p (not enough space for new header size)\n", ("pbuf_add_header: failed as %p < %p (not enough space for new header size)\n",
(void *)payload, (void *)((u8_t *)p + SIZEOF_STRUCT_PBUF))); (void *)payload,
(void *)((u8_t *)p + SIZEOF_STRUCT_PBUF)));
/* bail out unsuccessfully */ /* bail out unsuccessfully */
return 1; return 1;
} }
@ -515,15 +518,15 @@ pbuf_add_header_impl(struct pbuf *p, size_t header_size_increment, u8_t force)
return 1; return 1;
} }
} }
LWIP_DEBUGF(PBUF_DEBUG | LWIP_DBG_TRACE, ("pbuf_add_header: old %p new %p (%"U16_F")\n", LWIP_DEBUGF(
(void *)p->payload, (void *)payload, increment_magnitude)); PBUF_DEBUG | LWIP_DBG_TRACE,
("pbuf_add_header: old %p new %p (%" U16_F ")\n", (void *)p->payload, (void *)payload, increment_magnitude));
/* modify pbuf fields */ /* modify pbuf fields */
p->payload = payload; p->payload = payload;
p->len = (u16_t)(p->len + increment_magnitude); p->len = (u16_t)(p->len + increment_magnitude);
p->tot_len = (u16_t)(p->tot_len + increment_magnitude); p->tot_len = (u16_t)(p->tot_len + increment_magnitude);
return 0; return 0;
} }
@ -603,11 +606,12 @@ pbuf_remove_header(struct pbuf *p, size_t header_size_decrement)
/* increase payload pointer (guarded by length check above) */ /* increase payload pointer (guarded by length check above) */
p->payload = (u8_t *)p->payload + header_size_decrement; p->payload = (u8_t *)p->payload + header_size_decrement;
/* modify pbuf length fields */ /* modify pbuf length fields */
p->len = (u16_t)(p->len - increment_magnitude); p->len = (u16_t)(p->len - increment_magnitude);
p->tot_len = (u16_t)(p->tot_len - increment_magnitude); p->tot_len = (u16_t)(p->tot_len - increment_magnitude);
LWIP_DEBUGF(PBUF_DEBUG | LWIP_DBG_TRACE, ("pbuf_remove_header: old %p new %p (%"U16_F")\n", LWIP_DEBUGF(
(void *)payload, (void *)p->payload, increment_magnitude)); PBUF_DEBUG | LWIP_DBG_TRACE,
("pbuf_remove_header: old %p new %p (%" U16_F ")\n", (void *)payload, (void *)p->payload, increment_magnitude));
return 0; return 0;
} }
@ -616,7 +620,7 @@ static u8_t
pbuf_header_impl(struct pbuf *p, s16_t header_size_increment, u8_t force) pbuf_header_impl(struct pbuf *p, s16_t header_size_increment, u8_t force)
{ {
if (header_size_increment < 0) { if (header_size_increment < 0) {
return pbuf_remove_header(p, (size_t) - header_size_increment); return pbuf_remove_header(p, (size_t)-header_size_increment);
} else { } else {
return pbuf_add_header_impl(p, (size_t)header_size_increment, force); return pbuf_add_header_impl(p, (size_t)header_size_increment, force);
} }
@ -670,14 +674,14 @@ pbuf_header_force(struct pbuf *p, s16_t header_size_increment)
struct pbuf * struct pbuf *
pbuf_free_header(struct pbuf *q, u16_t size) pbuf_free_header(struct pbuf *q, u16_t size)
{ {
struct pbuf *p = q; struct pbuf *p = q;
u16_t free_left = size; u16_t free_left = size;
while (free_left && p) { while (free_left && p) {
if (free_left >= p->len) { if (free_left >= p->len) {
struct pbuf *f = p; struct pbuf *f = p;
free_left = (u16_t)(free_left - p->len); free_left = (u16_t)(free_left - p->len);
p = p->next; p = p->next;
f->next = 0; f->next = 0;
pbuf_free(f); pbuf_free(f);
} else { } else {
pbuf_remove_header(p, free_left); pbuf_remove_header(p, free_left);
@ -731,8 +735,7 @@ pbuf_free(struct pbuf *p)
if (p == NULL) { if (p == NULL) {
LWIP_ASSERT("p != NULL", p != NULL); LWIP_ASSERT("p != NULL", p != NULL);
/* if assertions are disabled, proceed with debug output */ /* if assertions are disabled, proceed with debug output */
LWIP_DEBUGF(PBUF_DEBUG | LWIP_DBG_LEVEL_SERIOUS, LWIP_DEBUGF(PBUF_DEBUG | LWIP_DBG_LEVEL_SERIOUS, ("pbuf_free(p == NULL) was called.\n"));
("pbuf_free(p == NULL) was called.\n"));
return 0; return 0;
} }
LWIP_DEBUGF(PBUF_DEBUG | LWIP_DBG_TRACE, ("pbuf_free(%p)\n", (void *)p)); LWIP_DEBUGF(PBUF_DEBUG | LWIP_DBG_TRACE, ("pbuf_free(%p)\n", (void *)p));
@ -758,7 +761,7 @@ pbuf_free(struct pbuf *p)
if (ref == 0) { if (ref == 0) {
/* remember next pbuf in chain for next iteration */ /* remember next pbuf in chain for next iteration */
q = p->next; q = p->next;
LWIP_DEBUGF( PBUF_DEBUG | LWIP_DBG_TRACE, ("pbuf_free: deallocating %p\n", (void *)p)); LWIP_DEBUGF(PBUF_DEBUG | LWIP_DBG_TRACE, ("pbuf_free: deallocating %p\n", (void *)p));
alloc_src = pbuf_get_allocsrc(p); alloc_src = pbuf_get_allocsrc(p);
#if LWIP_SUPPORT_CUSTOM_PBUF #if LWIP_SUPPORT_CUSTOM_PBUF
/* is this a custom pbuf? */ /* is this a custom pbuf? */
@ -789,7 +792,8 @@ pbuf_free(struct pbuf *p)
/* p->ref > 0, this pbuf is still referenced to */ /* p->ref > 0, this pbuf is still referenced to */
/* (and so the remaining pbufs in chain as well) */ /* (and so the remaining pbufs in chain as well) */
} else { } else {
LWIP_DEBUGF( PBUF_DEBUG | LWIP_DBG_TRACE, ("pbuf_free: %p has ref %"U16_F", ending here.\n", (void *)p, (u16_t)ref)); LWIP_DEBUGF(PBUF_DEBUG | LWIP_DBG_TRACE,
("pbuf_free: %p has ref %" U16_F ", ending here.\n", (void *)p, (u16_t)ref));
/* stop walking through the chain */ /* stop walking through the chain */
p = NULL; p = NULL;
} }
@ -854,8 +858,7 @@ pbuf_cat(struct pbuf *h, struct pbuf *t)
{ {
struct pbuf *p; struct pbuf *p;
LWIP_ERROR("(h != NULL) && (t != NULL) (programmer violates API)", LWIP_ERROR("(h != NULL) && (t != NULL) (programmer violates API)", ((h != NULL) && (t != NULL)), return;);
((h != NULL) && (t != NULL)), return;);
/* proceed to last pbuf of chain */ /* proceed to last pbuf of chain */
for (p = h; p->next != NULL; p = p->next) { for (p = h; p->next != NULL; p = p->next) {
@ -962,12 +965,12 @@ pbuf_copy(struct pbuf *p_to, const struct pbuf *p_from)
{ {
size_t offset_to = 0, offset_from = 0, len; size_t offset_to = 0, offset_from = 0, len;
LWIP_DEBUGF(PBUF_DEBUG | LWIP_DBG_TRACE, ("pbuf_copy(%p, %p)\n", LWIP_DEBUGF(PBUF_DEBUG | LWIP_DBG_TRACE, ("pbuf_copy(%p, %p)\n", (const void *)p_to, (const void *)p_from));
(const void *)p_to, (const void *)p_from));
/* is the target big enough to hold the source? */ /* is the target big enough to hold the source? */
LWIP_ERROR("pbuf_copy: target not big enough to hold source", ((p_to != NULL) && LWIP_ERROR("pbuf_copy: target not big enough to hold source",
(p_from != NULL) && (p_to->tot_len >= p_from->tot_len)), return ERR_ARG;); ((p_to != NULL) && (p_from != NULL) && (p_to->tot_len >= p_from->tot_len)),
return ERR_ARG;);
/* iterate through pbuf chain */ /* iterate through pbuf chain */
do { do {
@ -987,24 +990,22 @@ pbuf_copy(struct pbuf *p_to, const struct pbuf *p_from)
if (offset_from >= p_from->len) { if (offset_from >= p_from->len) {
/* on to next p_from (if any) */ /* on to next p_from (if any) */
offset_from = 0; offset_from = 0;
p_from = p_from->next; p_from = p_from->next;
} }
if (offset_to == p_to->len) { if (offset_to == p_to->len) {
/* on to next p_to (if any) */ /* on to next p_to (if any) */
offset_to = 0; offset_to = 0;
p_to = p_to->next; p_to = p_to->next;
LWIP_ERROR("p_to != NULL", (p_to != NULL) || (p_from == NULL), return ERR_ARG;); LWIP_ERROR("p_to != NULL", (p_to != NULL) || (p_from == NULL), return ERR_ARG;);
} }
if ((p_from != NULL) && (p_from->len == p_from->tot_len)) { if ((p_from != NULL) && (p_from->len == p_from->tot_len)) {
/* don't copy more than one packet! */ /* don't copy more than one packet! */
LWIP_ERROR("pbuf_copy() does not allow packet queues!", LWIP_ERROR("pbuf_copy() does not allow packet queues!", (p_from->next == NULL), return ERR_VAL;);
(p_from->next == NULL), return ERR_VAL;);
} }
if ((p_to != NULL) && (p_to->len == p_to->tot_len)) { if ((p_to != NULL) && (p_to->len == p_to->tot_len)) {
/* don't copy more than one packet! */ /* don't copy more than one packet! */
LWIP_ERROR("pbuf_copy() does not allow packet queues!", LWIP_ERROR("pbuf_copy() does not allow packet queues!", (p_to->next == NULL), return ERR_VAL;);
(p_to->next == NULL), return ERR_VAL;);
} }
} while (p_from); } while (p_from);
LWIP_DEBUGF(PBUF_DEBUG | LWIP_DBG_TRACE, ("pbuf_copy: end of chain reached.\n")); LWIP_DEBUGF(PBUF_DEBUG | LWIP_DBG_TRACE, ("pbuf_copy: end of chain reached.\n"));
@ -1048,9 +1049,9 @@ pbuf_copy_partial(const struct pbuf *buf, void *dataptr, u16_t len, u16_t offset
/* copy the necessary parts of the buffer */ /* copy the necessary parts of the buffer */
MEMCPY(&((char *)dataptr)[left], &((char *)p->payload)[offset], buf_copy_len); MEMCPY(&((char *)dataptr)[left], &((char *)p->payload)[offset], buf_copy_len);
copied_total = (u16_t)(copied_total + buf_copy_len); copied_total = (u16_t)(copied_total + buf_copy_len);
left = (u16_t)(left + buf_copy_len); left = (u16_t)(left + buf_copy_len);
len = (u16_t)(len - buf_copy_len); len = (u16_t)(len - buf_copy_len);
offset = 0; offset = 0;
} }
} }
return copied_total; return copied_total;
@ -1074,7 +1075,7 @@ void *
pbuf_get_contiguous(const struct pbuf *p, void *buffer, size_t bufsize, u16_t len, u16_t offset) pbuf_get_contiguous(const struct pbuf *p, void *buffer, size_t bufsize, u16_t len, u16_t offset)
{ {
const struct pbuf *q; const struct pbuf *q;
uint16_t out_offset; u16_t out_offset;
LWIP_ERROR("pbuf_get_contiguous: invalid buf", (p != NULL), return NULL;); LWIP_ERROR("pbuf_get_contiguous: invalid buf", (p != NULL), return NULL;);
LWIP_ERROR("pbuf_get_contiguous: invalid dataptr", (buffer != NULL), return NULL;); LWIP_ERROR("pbuf_get_contiguous: invalid dataptr", (buffer != NULL), return NULL;);
@ -1110,19 +1111,20 @@ pbuf_get_contiguous(const struct pbuf *p, void *buffer, size_t bufsize, u16_t le
* @param p the pbuf queue to be split * @param p the pbuf queue to be split
* @param rest pointer to store the remainder (after the first 64K) * @param rest pointer to store the remainder (after the first 64K)
*/ */
void pbuf_split_64k(struct pbuf *p, struct pbuf **rest) void
pbuf_split_64k(struct pbuf *p, struct pbuf **rest)
{ {
*rest = NULL; *rest = NULL;
if ((p != NULL) && (p->next != NULL)) { if ((p != NULL) && (p->next != NULL)) {
u16_t tot_len_front = p->len; u16_t tot_len_front = p->len;
struct pbuf *i = p; struct pbuf *i = p;
struct pbuf *r = p->next; struct pbuf *r = p->next;
/* continue until the total length (summed up as u16_t) overflows */ /* continue until the total length (summed up as u16_t) overflows */
while ((r != NULL) && ((u16_t)(tot_len_front + r->len) >= tot_len_front)) { while ((r != NULL) && ((u16_t)(tot_len_front + r->len) >= tot_len_front)) {
tot_len_front = (u16_t)(tot_len_front + r->len); tot_len_front = (u16_t)(tot_len_front + r->len);
i = r; i = r;
r = r->next; r = r->next;
} }
/* i now points to last packet of the first segment. Set next /* i now points to last packet of the first segment. Set next
pointer to NULL */ pointer to NULL */
@ -1132,8 +1134,7 @@ void pbuf_split_64k(struct pbuf *p, struct pbuf **rest)
/* Update the tot_len field in the first part */ /* Update the tot_len field in the first part */
for (i = p; i != NULL; i = i->next) { for (i = p; i != NULL; i = i->next) {
i->tot_len = (u16_t)(i->tot_len - r->tot_len); i->tot_len = (u16_t)(i->tot_len - r->tot_len);
LWIP_ASSERT("tot_len/len mismatch in last pbuf", LWIP_ASSERT("tot_len/len mismatch in last pbuf", (i->next != NULL) || (i->tot_len == i->len));
(i->next != NULL) || (i->tot_len == i->len));
} }
if (p->flags & PBUF_FLAG_TCP_FIN) { if (p->flags & PBUF_FLAG_TCP_FIN) {
r->flags |= PBUF_FLAG_TCP_FIN; r->flags |= PBUF_FLAG_TCP_FIN;
@ -1151,13 +1152,13 @@ void pbuf_split_64k(struct pbuf *p, struct pbuf **rest)
static const struct pbuf * static const struct pbuf *
pbuf_skip_const(const struct pbuf *in, u16_t in_offset, u16_t *out_offset) pbuf_skip_const(const struct pbuf *in, u16_t in_offset, u16_t *out_offset)
{ {
u16_t offset_left = in_offset; u16_t offset_left = in_offset;
const struct pbuf *q = in; const struct pbuf *q = in;
/* get the correct pbuf */ /* get the correct pbuf */
while ((q != NULL) && (q->len <= offset_left)) { while ((q != NULL) && (q->len <= offset_left)) {
offset_left = (u16_t)(offset_left - q->len); offset_left = (u16_t)(offset_left - q->len);
q = q->next; q = q->next;
} }
if (out_offset != NULL) { if (out_offset != NULL) {
*out_offset = offset_left; *out_offset = offset_left;
@ -1198,7 +1199,7 @@ pbuf_take(struct pbuf *buf, const void *dataptr, u16_t len)
struct pbuf *p; struct pbuf *p;
size_t buf_copy_len; size_t buf_copy_len;
size_t total_copy_len = len; size_t total_copy_len = len;
size_t copied_total = 0; size_t copied_total = 0;
LWIP_ERROR("pbuf_take: invalid buf", (buf != NULL), return ERR_ARG;); LWIP_ERROR("pbuf_take: invalid buf", (buf != NULL), return ERR_ARG;);
LWIP_ERROR("pbuf_take: invalid dataptr", (dataptr != NULL), return ERR_ARG;); LWIP_ERROR("pbuf_take: invalid dataptr", (dataptr != NULL), return ERR_ARG;);
@ -1331,8 +1332,7 @@ pbuf_clone(pbuf_layer layer, pbuf_type type, struct pbuf *p)
* within the (first) pbuf (no pbuf queues!) * within the (first) pbuf (no pbuf queues!)
*/ */
err_t err_t
pbuf_fill_chksum(struct pbuf *p, u16_t start_offset, const void *dataptr, pbuf_fill_chksum(struct pbuf *p, u16_t start_offset, const void *dataptr, u16_t len, u16_t *chksum)
u16_t len, u16_t *chksum)
{ {
u32_t acc; u32_t acc;
u16_t copy_chksum; u16_t copy_chksum;
@ -1346,7 +1346,7 @@ pbuf_fill_chksum(struct pbuf *p, u16_t start_offset, const void *dataptr,
return ERR_ARG; return ERR_ARG;
} }
dst_ptr = ((char *)p->payload) + start_offset; dst_ptr = ((char *)p->payload) + start_offset;
copy_chksum = LWIP_CHKSUM_COPY(dst_ptr, dataptr, len); copy_chksum = LWIP_CHKSUM_COPY(dst_ptr, dataptr, len);
if ((start_offset & 1) != 0) { if ((start_offset & 1) != 0) {
copy_chksum = SWAP_BYTES_IN_WORD(copy_chksum); copy_chksum = SWAP_BYTES_IN_WORD(copy_chksum);
@ -1433,7 +1433,7 @@ pbuf_put_at(struct pbuf *p, u16_t offset, u8_t data)
u16_t u16_t
pbuf_memcmp(const struct pbuf *p, u16_t offset, const void *s2, u16_t n) pbuf_memcmp(const struct pbuf *p, u16_t offset, const void *s2, u16_t n)
{ {
u16_t start = offset; u16_t start = offset;
const struct pbuf *q = p; const struct pbuf *q = p;
u16_t i; u16_t i;
@ -1445,7 +1445,7 @@ pbuf_memcmp(const struct pbuf *p, u16_t offset, const void *s2, u16_t n)
/* get the correct pbuf from chain. We know it succeeds because of p->tot_len check above. */ /* get the correct pbuf from chain. We know it succeeds because of p->tot_len check above. */
while ((q != NULL) && (q->len <= start)) { while ((q != NULL) && (q->len <= start)) {
start = (u16_t)(start - q->len); start = (u16_t)(start - q->len);
q = q->next; q = q->next;
} }
/* return requested data if pbuf is OK */ /* return requested data if pbuf is OK */