From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from gandalf.ozlabs.org (mail.ozlabs.org [IPv6:2404:9400:2221:ea00::3]) by passt.top (Postfix) with ESMTPS id 7A0845A026F for ; Fri, 15 Sep 2023 08:43:43 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gibson.dropbear.id.au; s=201602; t=1694760219; bh=PcALst0NzLdVxklrTLkQKq5I16UKCw3Hswdhsq/OGko=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=LIjTcUXOxVJYCL5eL6OA0GbdwT18184TxVpE8/lNgYp665bmPx86jJMz+f4MuyLTF FyBa112W/8fT5EHgqAjNDiVQIUvZpv2oQTJUOXbWtqnsjE8Tg+lfH7WMV6N45CqlBj nI2qoPRn7YhnSu+G1nxT5SA3j52QNp/uGNeMpMwE= Received: by gandalf.ozlabs.org (Postfix, from userid 1007) id 4Rn4Qg4RZSz4wxg; Fri, 15 Sep 2023 16:43:39 +1000 (AEST) From: David Gibson To: passt-dev@passt.top, Stefano Brivio Subject: [PATCH 1/2] packet: Avoid shadowing index(3) Date: Fri, 15 Sep 2023 16:43:36 +1000 Message-ID: <20230915064337.2380211-2-david@gibson.dropbear.id.au> X-Mailer: git-send-email 2.41.0 In-Reply-To: <20230915064337.2380211-1-david@gibson.dropbear.id.au> References: <20230915064337.2380211-1-david@gibson.dropbear.id.au> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Message-ID-Hash: UDW7PRVB3ORKRFTGK74HT3EEM32END7U X-Message-ID-Hash: UDW7PRVB3ORKRFTGK74HT3EEM32END7U X-MailFrom: dgibson@gandalf.ozlabs.org X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; emergency; loop; banned-address; member-moderation; nonmember-moderation; administrivia; implicit-dest; max-recipients; max-size; news-moderation; no-subject; digests; suspicious-header CC: David Gibson X-Mailman-Version: 3.3.8 Precedence: list List-Id: Development discussion and patches for passt Archived-At: Archived-At: List-Archive: List-Archive: List-Help: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: 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 is included, but that is so common that as a rule it's best to just avoid it always. We have a few places in packet.[ch] which hit this trap so rename the variables to avoid it. Signed-off-by: David Gibson --- packet.c | 28 ++++++++++++++-------------- packet.h | 10 +++++----- 2 files changed, 19 insertions(+), 19 deletions(-) diff --git a/packet.c b/packet.c index ce807e2..8e3a87c 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++; } @@ -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 { \ -- 2.41.0