Implement a more readable fix for pbuf_memcmp than my last fix

This commit is contained in:
Dirk Ziegelmeier 2016-08-31 20:24:37 +02:00
parent b944ceb89d
commit ac6b64cf66

View File

@ -1300,28 +1300,29 @@ pbuf_memcmp(struct pbuf* p, u16_t offset, const void* s2, u16_t n)
{ {
u16_t start = offset; u16_t start = offset;
struct pbuf* q = p; struct pbuf* q = p;
u16_t i;
/* get the correct pbuf */ /* pbuf long enough to perform check? */
if(p->tot_len < (offset + n)) {
return 0xffff;
}
/* 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 -= q->len; start -= q->len;
q = q->next; q = q->next;
} }
/* return requested data if pbuf is OK */ /* return requested data if pbuf is OK */
if ((q != NULL) && (q->len > start)) { for (i = 0; i < n; i++) {
u16_t i; /* We know pbuf_get_at() succeeds because of p->tot_len check above. */
for (i = 0; i < n; i++) { u8_t a = pbuf_get_at(q, start + i);
u8_t b = ((const u8_t*)s2)[i]; u8_t b = ((const u8_t*)s2)[i];
int a = pbuf_try_get_at(q, start + i); if (a != b) {
if (a < 0) { return i+1;
return 0xffff;
}
if (a != b) {
return i+1;
}
} }
return 0;
} }
return 0xffff; return 0;
} }
/** /**