]> git.ipfire.org Git - thirdparty/linux.git/commitdiff
mm/util: Swap kmemdup_array() arguments
authorJean-Philippe Brucker <jean-philippe@linaro.org>
Thu, 6 Jun 2024 14:46:09 +0000 (15:46 +0100)
committerKees Cook <kees@kernel.org>
Thu, 6 Jun 2024 15:55:20 +0000 (08:55 -0700)
GCC 14.1 complains about the argument usage of kmemdup_array():

  drivers/soc/tegra/fuse/fuse-tegra.c:130:65: error: 'kmemdup_array' sizes specified with 'sizeof' in the earlier argument and not in the later argument [-Werror=calloc-transposed-args]
    130 |         fuse->lookups = kmemdup_array(fuse->soc->lookups, sizeof(*fuse->lookups),
        |                                                                 ^
  drivers/soc/tegra/fuse/fuse-tegra.c:130:65: note: earlier argument should specify number of elements, later size of each element

The annotation introduced by commit 7d78a7773355 ("string: Add
additional __realloc_size() annotations for "dup" helpers") lets the
compiler think that kmemdup_array() follows the same format as calloc(),
with the number of elements preceding the size of one element. So we
could simply swap the arguments to __realloc_size() to get rid of that
warning, but it seems cleaner to instead have kmemdup_array() follow the
same format as krealloc_array(), memdup_array_user(), calloc() etc.

Fixes: 7d78a7773355 ("string: Add additional __realloc_size() annotations for "dup" helpers")
Signed-off-by: Jean-Philippe Brucker <jean-philippe@linaro.org>
Reviewed-by: Andy Shevchenko <andy.shevchenko@gmail.com>
Link: https://lore.kernel.org/r/20240606144608.97817-2-jean-philippe@linaro.org
Signed-off-by: Kees Cook <kees@kernel.org>
drivers/soc/tegra/fuse/fuse-tegra.c
include/linux/string.h
lib/fortify_kunit.c
mm/util.c

index b6bfd6729df3904adbe5affd1208e59f9a5608dc..d276672838465c60fc0b89c2a77174a0d9ab99d5 100644 (file)
@@ -127,8 +127,8 @@ static void tegra_fuse_print_sku_info(struct tegra_sku_info *tegra_sku_info)
 
 static int tegra_fuse_add_lookups(struct tegra_fuse *fuse)
 {
-       fuse->lookups = kmemdup_array(fuse->soc->lookups, sizeof(*fuse->lookups),
-                                     fuse->soc->num_lookups, GFP_KERNEL);
+       fuse->lookups = kmemdup_array(fuse->soc->lookups, fuse->soc->num_lookups,
+                                     sizeof(*fuse->lookups), GFP_KERNEL);
        if (!fuse->lookups)
                return -ENOMEM;
 
index 60168aa2af075d26e59aa66dcd736ba835d93939..9edace076ddbfd8c44ddc788c7184389c157adce 100644 (file)
@@ -289,7 +289,7 @@ extern void *kmemdup_noprof(const void *src, size_t len, gfp_t gfp) __realloc_si
 
 extern void *kvmemdup(const void *src, size_t len, gfp_t gfp) __realloc_size(2);
 extern char *kmemdup_nul(const char *s, size_t len, gfp_t gfp);
-extern void *kmemdup_array(const void *src, size_t element_size, size_t count, gfp_t gfp)
+extern void *kmemdup_array(const void *src, size_t count, size_t element_size, gfp_t gfp)
                __realloc_size(2, 3);
 
 /* lib/argv_split.c */
index f9cc467334ce3de06fbf101ace3dcab87566cbf0..e17d520f532cfb86eeba68563cfd03a598911fbc 100644 (file)
@@ -374,7 +374,7 @@ static const char * const test_strs[] = {
        for (i = 0; i < ARRAY_SIZE(test_strs); i++) {                   \
                len = strlen(test_strs[i]);                             \
                KUNIT_EXPECT_EQ(test, __builtin_constant_p(len), 0);    \
-               checker(len, kmemdup_array(test_strs[i], len, 1, gfp),  \
+               checker(len, kmemdup_array(test_strs[i], 1, len, gfp),  \
                        kfree(p));                                      \
                checker(len, kmemdup(test_strs[i], len, gfp),           \
                        kfree(p));                                      \
index c9e519e6811f55cccca2065124ecd0b6e83e9dd6..6682097372efcd92e6d6d0b05f5358453e52c9e8 100644 (file)
--- a/mm/util.c
+++ b/mm/util.c
@@ -139,14 +139,14 @@ EXPORT_SYMBOL(kmemdup_noprof);
  * kmemdup_array - duplicate a given array.
  *
  * @src: array to duplicate.
- * @element_size: size of each element of array.
  * @count: number of elements to duplicate from array.
+ * @element_size: size of each element of array.
  * @gfp: GFP mask to use.
  *
  * Return: duplicated array of @src or %NULL in case of error,
  * result is physically contiguous. Use kfree() to free.
  */
-void *kmemdup_array(const void *src, size_t element_size, size_t count, gfp_t gfp)
+void *kmemdup_array(const void *src, size_t count, size_t element_size, gfp_t gfp)
 {
        return kmemdup(src, size_mul(element_size, count), gfp);
 }