From patchwork Sun Jan 19 13:23:08 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jeff King X-Patchwork-Id: 13944458 Received: from cloud.peff.net (cloud.peff.net [104.130.231.41]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 70FA12913 for ; Sun, 19 Jan 2025 13:23:10 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=104.130.231.41 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1737292993; cv=none; b=Z/OwCEx1lblGKq2aCNjKU1UIDRkT2xSBfksNW0BNaiCqjrmzLNjDn1e4TyaGZVOzX6U7h1lh5ctfltPqUJkDhMZtgtSht8VbtMA4plgPVYXHYVDUuSVZWEW8cjuMBbZhkeQCTkK6NK/0clUrwlY+j8IqLtRUZ7kmvJ38o2+NQdA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1737292993; c=relaxed/simple; bh=lld4gzhICbA6zpIv2SHx+SqkuomgJcI2nvmXVB0gQS8=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=i9n955l5pk9GwmCuDRKEAXYvDbXSeOBqXE+2sKm1VfExEiFivhd3I7WCi4bu7T9MyJbyEAtizLCXXwc20B8OPYc7EAq0uYRzjpZfQ5AtOBXN6DHePr0cqaIFHdlqO2+2Z+RL5q+kPpAOExV8BQl42+cSrN7WXo7wLwZl/Pp/1LA= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=peff.net; spf=pass smtp.mailfrom=peff.net; dkim=pass (2048-bit key) header.d=peff.net header.i=@peff.net header.b=hnpJqUEr; arc=none smtp.client-ip=104.130.231.41 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=peff.net Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=peff.net Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=peff.net header.i=@peff.net header.b="hnpJqUEr" Received: (qmail 7965 invoked by uid 109); 19 Jan 2025 13:23:09 -0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed; d=peff.net; h=date:from:to:cc:subject:message-id:references:mime-version:content-type:in-reply-to; s=20240930; bh=lld4gzhICbA6zpIv2SHx+SqkuomgJcI2nvmXVB0gQS8=; b=hnpJqUErNz2HPvmUri74MoFjXLZQgZJVwiIppvTSswi4ftelrxxwqAx0ZEQUyWvH8JEYUJlWXXECUriHxQ05nfLX8iWeK+fDeChSqJyb0LLuaTjmC+V6Zody/P265Sy2SGtNxUgDWf/fqCTKK5xW2NF1uL2KQCtmkKfQfy89QfXcnRt7lw7n4Nqy22Hv02TQpswST9u5nteP96sF/djHJCJaN87rWj4juYowoxOmILoZn1tWSmIDJAUaMpJQVe2UEQf/sOVzpMUpJbTvsXV80SGBJ1tkgH2fuV+q6cy3dO/XZ4qkC2IOcTILP+C74H3jb+/E2NYaRkrECRJdRgDmcQ== Received: from Unknown (HELO peff.net) (10.0.1.2) by cloud.peff.net (qpsmtpd/0.94) with ESMTP; Sun, 19 Jan 2025 13:23:09 +0000 Authentication-Results: cloud.peff.net; auth=none Received: (qmail 734 invoked by uid 111); 19 Jan 2025 13:23:09 -0000 Received: from coredump.intra.peff.net (HELO coredump.intra.peff.net) (10.0.0.2) by peff.net (qpsmtpd/0.94) with (TLS_AES_256_GCM_SHA384 encrypted) ESMTPS; Sun, 19 Jan 2025 08:23:09 -0500 Authentication-Results: peff.net; auth=none Date: Sun, 19 Jan 2025 08:23:08 -0500 From: Jeff King To: Koakuma Cc: Junio C Hamano , "git@vger.kernel.org" Subject: [PATCH v2 1/5] bswap.h: squelch potential sparse -Wcast-truncate warnings Message-ID: <20250119132308.GA1552263@coredump.intra.peff.net> References: <20250119131224.GA1541095@coredump.intra.peff.net> Precedence: bulk X-Mailing-List: git@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: <20250119131224.GA1541095@coredump.intra.peff.net> From: Junio C Hamano In put_be32(), we right-shift a uint32_t value various amounts and then assign the low 8-bits to individual "unsigned char" bytes, throwing away the high bits. For shifts smaller than 24 bits, those thrown away bits will be arbitrary bits from the original uint32_t. This works exactly as we want, but if you feed a constant, then sparse complains. For example if we write this (which we plan to do in a future patch): put_be32(hdr, PACK_SIGNATURE); then "make sparse" produces: compat/bswap.h:175:22: error: cast truncates bits from constant value (5041 becomes 41) compat/bswap.h:176:22: error: cast truncates bits from constant value (504143 becomes 43) compat/bswap.h:177:22: error: cast truncates bits from constant value (5041434b becomes 4b) And the same issue exists in the other put_be*() functions, when used with a constant. We can silence this warning by explicitly masking off the truncated bits. The compiler is smart enough to know the result is the same, and the asm generated by gcc (with both -O0 and -O2) is identical. Curiously this line already exists: put_be32(&hdr_version, INDEX_EXTENSION_VERSION2); in the fsmonitor.c file, but it does not get flagged because the CPP macro expands to a small integer (2). Signed-off-by: Jeff King --- I tweaked the commit message to make more sense in context. Probably should get Junio's signoff. :) I noticed that reftable/basics.c has its own version of these macros, and it also does the masking. compat/bswap.h | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/compat/bswap.h b/compat/bswap.h index 512f6f4b99..b34054f2bd 100644 --- a/compat/bswap.h +++ b/compat/bswap.h @@ -171,23 +171,23 @@ static inline uint64_t get_be64(const void *ptr) static inline void put_be32(void *ptr, uint32_t value) { unsigned char *p = ptr; - p[0] = value >> 24; - p[1] = value >> 16; - p[2] = value >> 8; - p[3] = value >> 0; + p[0] = (value >> 24) & 0xff; + p[1] = (value >> 16) & 0xff; + p[2] = (value >> 8) & 0xff; + p[3] = (value >> 0) & 0xff; } static inline void put_be64(void *ptr, uint64_t value) { unsigned char *p = ptr; - p[0] = value >> 56; - p[1] = value >> 48; - p[2] = value >> 40; - p[3] = value >> 32; - p[4] = value >> 24; - p[5] = value >> 16; - p[6] = value >> 8; - p[7] = value >> 0; + p[0] = (value >> 56) & 0xff; + p[1] = (value >> 48) & 0xff; + p[2] = (value >> 40) & 0xff; + p[3] = (value >> 32) & 0xff; + p[4] = (value >> 24) & 0xff; + p[5] = (value >> 16) & 0xff; + p[6] = (value >> 8) & 0xff; + p[7] = (value >> 0) & 0xff; } #endif /* COMPAT_BSWAP_H */