Whamcloud - gitweb
LU-17504 libcfs: safer LIBCFS_ALLOC 15/55015/2
authorShaun Tancheff <shaun.tancheff@hpe.com>
Mon, 6 May 2024 05:11:15 +0000 (12:11 +0700)
committerOleg Drokin <green@whamcloud.com>
Wed, 29 May 2024 04:47:52 +0000 (04:47 +0000)
Make the LIBCFS_ALLOC() family of macros safer by adding
parenthesis around arguments such as (size) to avoid uninteded
expansion.

CoverityID: 415056 ("Integer handling issues")

Fixes: 718e3f3e68 ("LU-17504 build: fix gcc-13 [-Werror=stringop-overread] error")
Signed-off-by: Shaun Tancheff <shaun.tancheff@hpe.com>
Change-Id: I9701f87025bc5ce038a6bf34413b64a3f019d998
Reviewed-on: https://review.whamcloud.com/c/fs/lustre-release/+/55015
Tested-by: jenkins <devops@whamcloud.com>
Tested-by: Maloo <maloo@whamcloud.com>
Reviewed-by: Andreas Dilger <adilger@whamcloud.com>
Reviewed-by: Arshad Hussain <arshad.hussain@aeoncomputing.com>
Reviewed-by: James Simmons <jsimmons@infradead.org>
Reviewed-by: Oleg Drokin <green@whamcloud.com>
libcfs/include/libcfs/libcfs_private.h
libcfs/libcfs/hash.c

index bbc7e04..2c50e96 100644 (file)
@@ -128,12 +128,12 @@ extern atomic64_t libcfs_kmem;
 
 # define libcfs_kmem_inc(ptr, size)            \
 do {                                           \
-       atomic64_add(size, &libcfs_kmem);       \
+       atomic64_add((size), &libcfs_kmem);     \
 } while (0)
 
 # define libcfs_kmem_dec(ptr, size)            \
 do {                                           \
-       atomic64_sub(size, &libcfs_kmem);       \
+       atomic64_sub((size), &libcfs_kmem);     \
 } while (0)
 
 # define libcfs_kmem_read()                    \
@@ -169,13 +169,13 @@ do {                                                                            \
                       libcfs_kmem_read());                                   \
        } else {                                                              \
                libcfs_kmem_inc((ptr), (size));                               \
-               LIBCFS_MEM_MSG(ptr, size, name);                              \
+               LIBCFS_MEM_MSG(ptr, (size), name);                            \
        }                                                                     \
 } while (0)
 
 #define LIBCFS_FREE_PRE(ptr, size, name)                               \
        libcfs_kmem_dec((ptr), (size));                                 \
-       LIBCFS_MEM_MSG(ptr, size, name)
+       LIBCFS_MEM_MSG(ptr, (size), name)
 
 /**
  * allocate memory with GFP flags @mask
@@ -193,13 +193,13 @@ do {                                                                          \
  * default allocator
  */
 #define LIBCFS_ALLOC(ptr, size) \
-       LIBCFS_ALLOC_GFP(ptr, size, GFP_NOFS)
+       LIBCFS_ALLOC_GFP(ptr, (size), GFP_NOFS)
 
 /**
  * non-sleeping allocator
  */
 #define LIBCFS_ALLOC_ATOMIC(ptr, size) \
-       LIBCFS_ALLOC_GFP(ptr, size, GFP_ATOMIC)
+       LIBCFS_ALLOC_GFP(ptr, (size), GFP_ATOMIC)
 
 /**
  * allocate memory for specified CPU partition
@@ -218,7 +218,7 @@ do {                                                                            \
 
 /** default numa allocator */
 #define LIBCFS_CPT_ALLOC(ptr, cptab, cpt, size)                                    \
-       LIBCFS_CPT_ALLOC_GFP(ptr, cptab, cpt, size, GFP_NOFS)
+       LIBCFS_CPT_ALLOC_GFP(ptr, (cptab), (cpt), (size), GFP_NOFS)
 
 #define LIBCFS_FREE(ptr, size)                                         \
 do {                                                                   \
@@ -228,7 +228,7 @@ do {                                                                        \
                       "%s:%d\n", s, __FILE__, __LINE__);               \
                break;                                                  \
        }                                                               \
-       LIBCFS_FREE_PRE(ptr, size, "kfreed");                           \
+       LIBCFS_FREE_PRE(ptr, (size), "kfreed");                         \
        if (unlikely(s > LIBCFS_VMALLOC_SIZE))                          \
                libcfs_vfree_atomic(ptr);                               \
        else                                                            \
index b69f638..63fdb0f 100644 (file)
@@ -1096,7 +1096,7 @@ cfs_hash_create(char *name, unsigned cur_bits, unsigned max_bits,
         if (hs->hs_buckets != NULL)
                 return hs;
 
-       LIBCFS_FREE(hs, offsetof(struct cfs_hash, hs_name[len]));
+       LIBCFS_FREE(hs, sizeof(struct cfs_hash) + len);
         RETURN(NULL);
 }
 EXPORT_SYMBOL(cfs_hash_create);
@@ -1162,7 +1162,7 @@ cfs_hash_destroy(struct kref *kref)
                              0, CFS_HASH_NBKT(hs));
        i = cfs_hash_with_bigname(hs) ?
            CFS_HASH_BIGNAME_LEN : CFS_HASH_NAME_LEN;
-       LIBCFS_FREE(hs, offsetof(struct cfs_hash, hs_name[i]));
+       LIBCFS_FREE(hs, sizeof(struct cfs_hash) + i);
 
        EXIT;
 }