From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail.ozlabs.org (mail.ozlabs.org [IPv6:2404:9400:2221:ea00::3]) by passt.top (Postfix) with ESMTPS id C4E4A5A0054 for ; Thu, 06 Jun 2024 12:10:09 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gibson.dropbear.id.au; s=202312; t=1717668601; bh=kYe4jljmiTD6N73mTYyLjfgfF9IuTGqH0ANMJXwKgzU=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=U5czB8mKHYxpalOhs3w4MxeVksoxxxZiroyiA9YUVOmjnX2290pUUDcHFTiN7uOIm e1xnYuJjmaFjls7ztaWeY9n1yO3/PMVJY/pHGMCKr1PfUYv90jdYFSJb4bLRCh3D3g 1Epywhe4coZVHwhwoGZALSE39x9v05lMlyjvuBwPr1gmznG1hKQGKI5ZDKqXOB1+Ka X4482I57LdC0Lyj1EvBlJy0xgDvgzSjdBuxmVsWL4p79Y2OubY9Usc3XuB8W1XWBcl 3DNskgpbzyjkcNc/XfYHWaQ0k9DhxgnBGb3tWt9U0zRYlXB3oNXEIWN4ohqD8IlFvV 4QTxJWFlGeMhA== Received: by gandalf.ozlabs.org (Postfix, from userid 1007) id 4Vw0ST4ZFkz4x0y; Thu, 6 Jun 2024 20:10:01 +1000 (AEST) From: David Gibson To: passt-dev@passt.top, Stefano Brivio Subject: [PATCH 6/9] util: Use unsigned indices for bits in bitmaps Date: Thu, 6 Jun 2024 20:09:46 +1000 Message-ID: <20240606100949.1250958-7-david@gibson.dropbear.id.au> X-Mailer: git-send-email 2.45.2 In-Reply-To: <20240606100949.1250958-1-david@gibson.dropbear.id.au> References: <20240606100949.1250958-1-david@gibson.dropbear.id.au> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Message-ID-Hash: QZAJPDQU53UR6UKGEWQHECDXRDRSKHUP X-Message-ID-Hash: QZAJPDQU53UR6UKGEWQHECDXRDRSKHUP 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 negative bit index in a bitmap doesn't make sense. Avoid this by construction by using unsigned indices. While we're there adjust bitmap_isset() to return a bool instead of an int. Signed-off-by: David Gibson --- util.c | 8 ++++---- util.h | 6 +++--- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/util.c b/util.c index cc1c73ba..5e854a26 100644 --- a/util.c +++ b/util.c @@ -232,7 +232,7 @@ int timespec_diff_ms(const struct timespec *a, const struct timespec *b) * @map: Pointer to bitmap * @bit: Bit number to set */ -void bitmap_set(uint8_t *map, int bit) +void bitmap_set(uint8_t *map, unsigned bit) { unsigned long *word = (unsigned long *)map + BITMAP_WORD(bit); @@ -244,7 +244,7 @@ void bitmap_set(uint8_t *map, int bit) * @map: Pointer to bitmap * @bit: Bit number to clear */ -void bitmap_clear(uint8_t *map, int bit) +void bitmap_clear(uint8_t *map, unsigned bit) { unsigned long *word = (unsigned long *)map + BITMAP_WORD(bit); @@ -256,9 +256,9 @@ void bitmap_clear(uint8_t *map, int bit) * @map: Pointer to bitmap * @bit: Bit number to check * - * Return: one if given bit is set, zero if it's not + * Return: true if given bit is set, false if it's not */ -int bitmap_isset(const uint8_t *map, int bit) +bool bitmap_isset(const uint8_t *map, unsigned bit) { const unsigned long *word = (const unsigned long *)map + BITMAP_WORD(bit); diff --git a/util.h b/util.h index aa6e4b48..cf9c4b66 100644 --- a/util.h +++ b/util.h @@ -148,9 +148,9 @@ int sock_l4(const struct ctx *c, sa_family_t af, uint8_t proto, uint32_t data); void sock_probe_mem(struct ctx *c); int timespec_diff_ms(const struct timespec *a, const struct timespec *b); -void bitmap_set(uint8_t *map, int bit); -void bitmap_clear(uint8_t *map, int bit); -int bitmap_isset(const uint8_t *map, int bit); +void bitmap_set(uint8_t *map, unsigned bit); +void bitmap_clear(uint8_t *map, unsigned bit); +bool bitmap_isset(const uint8_t *map, unsigned bit); void bitmap_or(uint8_t *dst, size_t size, const uint8_t *a, const uint8_t *b); char *line_read(char *buf, size_t len, int fd); void ns_enter(const struct ctx *c); -- 2.45.2