diff options
Diffstat (limited to 'packet.c')
-rw-r--r-- | packet.c | 52 |
1 files changed, 28 insertions, 24 deletions
@@ -22,39 +22,43 @@ #include "util.h" #include "log.h" +/** + * packet_check_range() - Check if a packet memory range is valid + * @p: Packet pool + * @offset: Offset of data range in packet descriptor + * @len: Length of desired data range + * @start: Start of the packet descriptor + * @func: For tracing: name of calling function + * @line: For tracing: caller line of function call + * + * Return: 0 if the range is valid, -1 otherwise + */ static int packet_check_range(const struct pool *p, size_t offset, size_t len, const char *start, const char *func, int line) { - ASSERT(p->buf); + if (p->buf_size == 0) { + int ret; - if (p->buf_size == 0) - return vu_packet_check_range((void *)p->buf, offset, len, start, - func, line); + ret = vu_packet_check_range((void *)p->buf, offset, len, start); - if (start < p->buf) { - if (func) { - trace("add packet start %p before buffer start %p, " - "%s:%i", (void *)start, (void *)p->buf, func, line); - } - return -1; + if (ret == -1) + trace("cannot find region, %s:%i", func, line); + + return ret; } - if (start + len + offset > p->buf + p->buf_size) { - if (func) { - trace("packet offset plus length %lu from size %lu, " - "%s:%i", start - p->buf + len + offset, - p->buf_size, func, line); - } + if (start < p->buf) { + trace("packet start %p before buffer start %p, " + "%s:%i", (void *)start, (void *)p->buf, func, line); return -1; } -#if UINTPTR_MAX == UINT64_MAX - if ((uintptr_t)start - (uintptr_t)p->buf > UINT32_MAX) { - trace("add packet start %p, buffer start %p, %s:%i", - (void *)start, (void *)p->buf, func, line); + if (start + len + offset > p->buf + p->buf_size) { + trace("packet offset plus length %lu from size %lu, " + "%s:%i", start - p->buf + len + offset, + p->buf_size, func, line); return -1; } -#endif return 0; } @@ -114,10 +118,10 @@ void *packet_get_do(const struct pool *p, size_t idx, size_t offset, return NULL; } - if (len > UINT16_MAX || len + offset > UINT32_MAX) { + if (len > UINT16_MAX) { if (func) { - trace("packet data length %zu, offset %zu, %s:%i", - len, offset, func, line); + trace("packet data length %zu, %s:%i", + len, func, line); } return NULL; } |