From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (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 DBE6819C54F for ; Thu, 11 Dec 2025 10:43:49 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1765449829; cv=none; b=LRaWDA/L5pRvDf6qHlUA+NwyuQwX9CAB7acBHTQ5jlSHbnDb3G16+1Qx/hvvdCNwtotxbxd/dIz4PnScS+1Ib1ilpWRLICETupj5Y2oj+gSqcOLZKC/WfNsZCf1q2EiChC9LfGDcpArvnrJo+VQsvdEmGdmSroyeRiywpLX9K+4= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1765449829; c=relaxed/simple; bh=SL08egAf+JvLRJMamE3oqe4gonfUljQf3NQhumvhwxc=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=L8eDeAnpHIiDStyI9fFmZpFHfT+7a9qCLZpPXfPsqN+ny9+SyB1Bk0kkXCXGNpawIgEatpepeb7nk3PEEhs7GZszqpbPqfwNVPGmQKAYFnvlJ2GtWZxe88jawnGPDWmF4t+eEnCodBMvGD5pRyAJz5IfBxaGnMksoaY3lIf/orU= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=TzCWGhmc; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="TzCWGhmc" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 51356C113D0; Thu, 11 Dec 2025 10:43:46 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1765449829; bh=SL08egAf+JvLRJMamE3oqe4gonfUljQf3NQhumvhwxc=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=TzCWGhmcsJOVikxD/0Atui8lv1QE9lu4h0KgG/QaQR9zJE+7ul8zV7he575FLyPQ3 AiDhS0jeb6Na3e94u6aqGPn562eEQjeCbhP80Ni75E/RJgsGIoRR1k8Ff/TGv+W7dU iFxP1pVv7KpzBOFmbTpC8fQIPjiA3fXCox2jPXglZVDW4pfdTU53yq9ISKAotNWsyE Bn6TOfQ+SplfouSyUtDo3Wy859GE+oKN8iVel/6WF7SDgfswCOcSbx3/Wa6iGkdaHy 1vRvbeJK0pdYIvXcPCRn0Jbrfs/hDh8oJk8KGPnJ0SNeU8VFBxeulR5vKqARjztXXS MDVxnbvEig62g== Date: Thu, 11 Dec 2025 11:43:44 +0100 From: Alejandro Colomar To: linux-kernel@vger.kernel.org, linux-mm@kvack.org Cc: Alejandro Colomar , Kees Cook , Christopher Bazley , Rasmus Villemoes , Marco Elver , Michal Hocko , Linus Torvalds , Al Viro , Alexander Potapenko , Dmitry Vyukov , Jann Horn , Andrew Morton , "Maciej W. Rozycki" Subject: [PATCH v6 0/4] Add ARRAY_END(), and use it to fix off-by-one bugs Message-ID: X-Mailer: git-send-email 2.51.0 References: Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline In-Reply-To: Add ARRAY_END(), and use it to fix off-by-one bugs ARRAY_END() is a macro to calculate a pointer to one past the last element of an array argument. This is a very common pointer, which is used to iterate over all elements of an array: for (T *p = a; p < ARRAY_END(a); p++) ... Of course, this pointer should never be dereferenced. A pointer one past the last element of an array should not be dereferenced; it's perfectly fine to hold such a pointer --and a good thing to do--, but the only thing it should be used for is comparing it with other pointers derived from the same array. Due to how special these pointers are, it would be good to use consistent naming. It's common to name such a pointer 'end' --in fact, we have many such cases in the kernel--. C++ even standardized this name with std::end(). Let's try naming such pointers 'end', and try also avoid using 'end' for pointers that are not the result of ARRAY_END(). It has been incorrectly suggested that these pointers are dangerous, and that they should never be used, suggesting to use something like #define ARRAY_LAST(a) ((a) + ARRAY_SIZE(a) - 1) for (T *p = a; p <= ARRAY_LAST(a); p++) ... This is bogus, as it doesn't scale down to arrays of 0 elements. In the case of an array of 0 elements, ARRAY_LAST() would underflow the pointer, which not only it can't be dereferenced, it can't even be held (it produces Undefined Behavior). That would be a footgun. Such arrays don't exist per the ISO C standard; however, GCC supports them as an extension (with partial support, though; GCC has a few bugs which need to be fixed). This patch set fixes a few places where it was intended to use the array end (that is, one past the last element), but accidentally a pointer to the last element was used instead, thus wasting one byte. It also replaces other places where the array end was correctly calculated with ARRAY_SIZE(), by using the simpler ARRAY_END(). Also, there was one drivers/ file that already defined this macro. We remove that definition, to not conflict with this one. Cc: Kees Cook Cc: Christopher Bazley Cc: Rasmus Villemoes Cc: Marco Elver Cc: Michal Hocko Cc: Linus Torvalds Cc: Al Viro Cc: Alexander Potapenko Cc: Dmitry Vyukov Cc: Jann Horn Cc: Andrew Morton Cc: "Maciej W. Rozycki" In-Reply-To: --- Hi, v6 adds a commit log to the last commit. Everything else is unchanged. See range-diff below. Have a lovely day! Alex Alejandro Colomar (4): array_size.h: Add ARRAY_END() mm: Fix benign off-by-one bugs kernel: Fix off-by-one benign bugs mm: Use ARRAY_END() instead of open-coding it drivers/block/floppy.c | 2 -- include/linux/array_size.h | 6 ++++++ kernel/kcsan/kcsan_test.c | 4 ++-- mm/kfence/kfence_test.c | 4 ++-- mm/kmemleak.c | 2 +- mm/kmsan/kmsan_test.c | 2 +- mm/memcontrol-v1.c | 4 ++-- 7 files changed, 14 insertions(+), 10 deletions(-) Range-diff against v5: 1: 5973cfb67419 = 1: 5973cfb67419 array_size.h: Add ARRAY_END() 2: 9c38dd009c17 = 2: 9c38dd009c17 mm: Fix benign off-by-one bugs 3: b4a945a4d40b = 3: b4a945a4d40b kernel: Fix off-by-one benign bugs 4: e7bde864b039 ! 4: 2335917d1238 mm: Use ARRAY_END() instead of open-coding it @@ Metadata ## Commit message ## mm: Use ARRAY_END() instead of open-coding it + There aren't any bugs in this code; it's purely cosmetic. + + By using ARRAY_END(), we prevent future issues, in case the code is + modified; it has less moving parts. Also, it should be more readable + (and perhaps more importantly, greppable), as there are several ways of + writing an expression that gets the end of an array, which are unified + by this API name. + Cc: Kees Cook Cc: Linus Torvalds Signed-off-by: Alejandro Colomar -- 2.51.0