A classic gotcha of the standard C library is that its unwise to call any variable 'index' because it will shadow the standard string library function index(3). This can cause warnings from cppcheck amongst others, and it also means that if the variable is removed you tend to get confusing type errors (or sometimes nothing at all) instead of a nice simple "name is not defined" error. Strictly speaking this only occurs if <string.h> is included, but that is so common that as a rule it's best to just avoid it always. We have a number of places which hit this trap, so rename variables and parameters to avoid it. Signed-off-by: David Gibson <david(a)gibson.dropbear.id.au> --- packet.c | 30 +++++++++++++++--------------- packet.h | 10 +++++----- tcp.c | 14 +++++++------- tcp_splice.c | 2 +- util.c | 12 ++++++------ util.h | 2 +- 6 files changed, 35 insertions(+), 35 deletions(-) diff --git a/packet.c b/packet.c index ce807e2..693e034 100644 --- a/packet.c +++ b/packet.c @@ -33,11 +33,11 @@ void packet_add_do(struct pool *p, size_t len, const char *start, const char *func, int line) { - size_t index = p->count; + size_t idx = p->count; - if (index >= p->size) { + if (idx >= p->size) { trace("add packet index %lu to pool with size %lu, %s:%i", - index, p->size, func, line); + idx, p->size, func, line); return; } @@ -66,8 +66,8 @@ void packet_add_do(struct pool *p, size_t len, const char *start, } #endif - p->pkt[index].offset = start - p->buf; - p->pkt[index].len = len; + p->pkt[idx].offset = start - p->buf; + p->pkt[idx].len = len; p->count++; } @@ -75,7 +75,7 @@ void packet_add_do(struct pool *p, size_t len, const char *start, /** * packet_get_do() - Get data range from packet descriptor from given pool * @p: Packet pool - * @index: Index of packet descriptor in pool + * @idx: Index of packet descriptor in pool * @offset: Offset of data range in packet descriptor * @len: Length of desired data range * @left: Length of available data after range, set on return, can be NULL @@ -84,13 +84,13 @@ void packet_add_do(struct pool *p, size_t len, const char *start, * * Return: pointer to start of data range, NULL on invalid range or descriptor */ -void *packet_get_do(const struct pool *p, size_t index, size_t offset, +void *packet_get_do(const struct pool *p, size_t idx, size_t offset, size_t len, size_t *left, const char *func, int line) { - if (index >= p->size || index >= p->count) { + if (idx >= p->size || idx >= p->count) { if (func) { trace("packet %lu from pool size: %lu, count: %lu, " - "%s:%i", index, p->size, p->count, func, line); + "%s:%i", idx, p->size, p->count, func, line); } return NULL; } @@ -103,28 +103,28 @@ void *packet_get_do(const struct pool *p, size_t index, size_t offset, return NULL; } - if (p->pkt[index].offset + len + offset > p->buf_size) { + if (p->pkt[idx].offset + len + offset > p->buf_size) { if (func) { trace("packet offset plus length %lu from size %lu, " - "%s:%i", p->pkt[index].offset + len + offset, + "%s:%i", p->pkt[idx].offset + len + offset, p->buf_size, func, line); } return NULL; } - if (len + offset > p->pkt[index].len) { + if (len + offset > p->pkt[idx].len) { if (func) { trace("data length %lu, offset %lu from length %u, " - "%s:%i", len, offset, p->pkt[index].len, + "%s:%i", len, offset, p->pkt[idx].len, func, line); } return NULL; } if (left) - *left = p->pkt[index].len - offset - len; + *left = p->pkt[idx].len - offset - len; - return p->buf + p->pkt[index].offset + offset; + return p->buf + p->pkt[idx].offset + offset; } /** diff --git a/packet.h b/packet.h index 9e5fc74..a784b07 100644 --- a/packet.h +++ b/packet.h @@ -34,7 +34,7 @@ struct pool { void packet_add_do(struct pool *p, size_t len, const char *start, const char *func, int line); -void *packet_get_do(const struct pool *p, const size_t index, +void *packet_get_do(const struct pool *p, const size_t idx, size_t offset, size_t len, size_t *left, const char *func, int line); void pool_flush(struct pool *p); @@ -42,11 +42,11 @@ void pool_flush(struct pool *p); #define packet_add(p, len, start) \ packet_add_do(p, len, start, __func__, __LINE__) -#define packet_get(p, index, offset, len, left) \ - packet_get_do(p, index, offset, len, left, __func__, __LINE__) +#define packet_get(p, idx, offset, len, left) \ + packet_get_do(p, idx, offset, len, left, __func__, __LINE__) -#define packet_get_try(p, index, offset, len, left) \ - packet_get_do(p, index, offset, len, left, NULL, 0) +#define packet_get_try(p, idx, offset, len, left) \ + packet_get_do(p, idx, offset, len, left, NULL, 0) #define PACKET_POOL_DECL(_name, _size, _buf) \ struct _name ## _t { \ diff --git a/tcp.c b/tcp.c index dd3142d..e58625d 100644 --- a/tcp.c +++ b/tcp.c @@ -563,20 +563,20 @@ static unsigned int tcp6_l2_flags_buf_used; /* TCP connections */ union tcp_conn tc[TCP_MAX_CONNS]; -#define CONN(index) (&tc[(index)].tap) +#define CONN(idx) (&tc[(idx)].tap) #define CONN_IDX(conn) ((union tcp_conn *)(conn) - tc) /** conn_at_idx() - Find a connection by index, if present - * @index: Index of connection to lookup + * @idx: Index of connection to lookup * - * Return: pointer to connection, or NULL if @index is out of bounds + * Return: pointer to connection, or NULL if @idx is out of bounds */ -static inline struct tcp_tap_conn *conn_at_idx(int index) +static inline struct tcp_tap_conn *conn_at_idx(int idx) { - if ((index < 0) || (index >= TCP_MAX_CONNS)) + if ((idx < 0) || (idx >= TCP_MAX_CONNS)) return NULL; - ASSERT(!(CONN(index)->c.spliced)); - return CONN(index); + ASSERT(!(CONN(idx)->c.spliced)); + return CONN(idx); } /* Table for lookup from remote address, local port, remote port */ diff --git a/tcp_splice.c b/tcp_splice.c index 5b36975..be01908 100644 --- a/tcp_splice.c +++ b/tcp_splice.c @@ -73,7 +73,7 @@ static int splice_pipe_pool [TCP_SPLICE_PIPE_POOL_SIZE][2][2]; #define CONN_V6(x) (x->flags & SPLICE_V6) #define CONN_V4(x) (!CONN_V6(x)) #define CONN_HAS(conn, set) ((conn->events & (set)) == (set)) -#define CONN(index) (&tc[(index)].splice) +#define CONN(idx) (&tc[(idx)].splice) #define CONN_IDX(conn) ((union tcp_conn *)(conn) - tc) /* Display strings for connection events */ diff --git a/util.c b/util.c index d965f48..34c32b9 100644 --- a/util.c +++ b/util.c @@ -38,15 +38,15 @@ /** * ipv6_l4hdr() - Find pointer to L4 header in IPv6 packet and extract protocol - * @p: Packet pool, packet number @index has IPv6 header at @offset - * @index: Index of packet in pool + * @p: Packet pool, packet number @idx has IPv6 header at @offset + * @idx: Index of packet in pool * @offset: Pre-calculated IPv6 header offset * @proto: Filled with L4 protocol number * @dlen: Data length (payload excluding header extensions), set on return * * Return: pointer to L4 header, NULL if not found */ -char *ipv6_l4hdr(const struct pool *p, int index, size_t offset, uint8_t *proto, +char *ipv6_l4hdr(const struct pool *p, int idx, size_t offset, uint8_t *proto, size_t *dlen) { struct ipv6_opt_hdr *o; @@ -55,8 +55,8 @@ char *ipv6_l4hdr(const struct pool *p, int index, size_t offset, uint8_t *proto, int hdrlen; uint8_t nh; - base = packet_get(p, index, 0, 0, NULL); - ip6h = packet_get(p, index, offset, sizeof(*ip6h), dlen); + base = packet_get(p, idx, 0, 0, NULL); + ip6h = packet_get(p, idx, offset, sizeof(*ip6h), dlen); if (!ip6h) return NULL; @@ -66,7 +66,7 @@ char *ipv6_l4hdr(const struct pool *p, int index, size_t offset, uint8_t *proto, if (!IPV6_NH_OPT(nh)) goto found; - while ((o = packet_get_try(p, index, offset, sizeof(*o), dlen))) { + while ((o = packet_get_try(p, idx, offset, sizeof(*o), dlen))) { nh = o->nexthdr; hdrlen = (o->hdrlen + 1) * 8; diff --git a/util.h b/util.h index 195023f..41cf123 100644 --- a/util.h +++ b/util.h @@ -205,7 +205,7 @@ struct ipv6_opt_hdr { /* cppcheck-suppress funcArgNamesDifferent */ __attribute__ ((weak)) int ffsl(long int i) { return __builtin_ffsl(i); } -char *ipv6_l4hdr(const struct pool *p, int index, size_t offset, uint8_t *proto, +char *ipv6_l4hdr(const struct pool *p, int idx, size_t offset, uint8_t *proto, size_t *dlen); int sock_l4(const struct ctx *c, int af, uint8_t proto, const void *bind_addr, const char *ifname, uint16_t port, -- 2.41.0