linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] dvm: use struct_size over open coded arithmetic
@ 2022-01-28  8:02 cgel.zte
  2022-01-28  9:01 ` Kalle Valo
  2022-01-28 21:03 ` [PATCH] " kernel test robot
  0 siblings, 2 replies; 5+ messages in thread
From: cgel.zte @ 2022-01-28  8:02 UTC (permalink / raw)
  To: luciano.coelho
  Cc: kvalo, davem, kuba, trix, johannes.berg, linux-wireless, netdev,
	linux-kernel, Minghao Chi, Zeal Robot

From: Minghao Chi (CGEL ZTE) <chi.minghao@zte.com.cn>

Replace zero-length array with flexible-array member and make use
of the struct_size() helper in kmalloc(). For example:

struct iwl_wipan_noa_data {
	...
	u8 data[];
};

Make use of the struct_size() helper instead of an open-coded version
in order to avoid any potential type mistakes.

Reported-by: Zeal Robot <zealci@zte.com.cn>
Signed-off-by: Minghao Chi (CGEL ZTE) <chi.minghao@zte.com.cn>
---
 drivers/net/wireless/intel/iwlwifi/dvm/rx.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/net/wireless/intel/iwlwifi/dvm/rx.c b/drivers/net/wireless/intel/iwlwifi/dvm/rx.c
index db0c41bbeb0e..d0d842b25b86 100644
--- a/drivers/net/wireless/intel/iwlwifi/dvm/rx.c
+++ b/drivers/net/wireless/intel/iwlwifi/dvm/rx.c
@@ -915,7 +915,7 @@ static void iwlagn_rx_noa_notification(struct iwl_priv *priv,
 		len += 1 + 2;
 		copylen += 1 + 2;
 
-		new_data = kmalloc(sizeof(*new_data) + len, GFP_ATOMIC);
+		new_data = kmalloc(struct_size(*new_data, data, len), GFP_ATOMIC);
 		if (new_data) {
 			new_data->length = len;
 			new_data->data[0] = WLAN_EID_VENDOR_SPECIFIC;
-- 
2.25.1



^ permalink raw reply related	[flat|nested] 5+ messages in thread

* Re: [PATCH] dvm: use struct_size over open coded arithmetic
  2022-01-28  8:02 [PATCH] dvm: use struct_size over open coded arithmetic cgel.zte
@ 2022-01-28  9:01 ` Kalle Valo
  2022-01-28  9:31   ` [PATCH v2] iwlwifi: " cgel.zte
  2022-01-28 21:03 ` [PATCH] " kernel test robot
  1 sibling, 1 reply; 5+ messages in thread
From: Kalle Valo @ 2022-01-28  9:01 UTC (permalink / raw)
  To: cgel.zte
  Cc: luciano.coelho, davem, kuba, trix, johannes.berg, linux-wireless,
	netdev, linux-kernel, Minghao Chi, Zeal Robot

cgel.zte@gmail.com writes:

> From: Minghao Chi (CGEL ZTE) <chi.minghao@zte.com.cn>
>
> Replace zero-length array with flexible-array member and make use
> of the struct_size() helper in kmalloc(). For example:
>
> struct iwl_wipan_noa_data {
> 	...
> 	u8 data[];
> };
>
> Make use of the struct_size() helper instead of an open-coded version
> in order to avoid any potential type mistakes.
>
> Reported-by: Zeal Robot <zealci@zte.com.cn>
> Signed-off-by: Minghao Chi (CGEL ZTE) <chi.minghao@zte.com.cn>
> ---
>  drivers/net/wireless/intel/iwlwifi/dvm/rx.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)

The prefix should be "iwlwifi: dvm:".

-- 
https://patchwork.kernel.org/project/linux-wireless/list/

https://wireless.wiki.kernel.org/en/developers/documentation/submittingpatches

^ permalink raw reply	[flat|nested] 5+ messages in thread

* [PATCH v2] iwlwifi: dvm: use struct_size over open coded arithmetic
  2022-01-28  9:01 ` Kalle Valo
@ 2022-01-28  9:31   ` cgel.zte
  2022-01-28 12:34     ` Coelho, Luciano
  0 siblings, 1 reply; 5+ messages in thread
From: cgel.zte @ 2022-01-28  9:31 UTC (permalink / raw)
  To: kvalo
  Cc: cgel.zte, chi.minghao, davem, johannes.berg, kuba, linux-kernel,
	linux-wireless, luciano.coelho, netdev, trix, zealci

From: Minghao Chi (CGEL ZTE) <chi.minghao@zte.com.cn>

Replace zero-length array with flexible-array member and make use
of the struct_size() helper in kmalloc(). For example:

struct iwl_wipan_noa_data {
	...
	u8 data[];
}

Make use of the struct_size() helper instead of an open-coded version
in order to avoid any potential type mistakes.

Reported-by: Zeal Robot <zealci@zte.com.cn>
Signed-off-by: Minghao Chi (CGEL ZTE) <chi.minghao@zte.com.cn>
---
 drivers/net/wireless/intel/iwlwifi/dvm/rx.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/net/wireless/intel/iwlwifi/dvm/rx.c b/drivers/net/wireless/intel/iwlwifi/dvm/rx.c
index db0c41bbeb0e..d0d842b25b86 100644
--- a/drivers/net/wireless/intel/iwlwifi/dvm/rx.c
+++ b/drivers/net/wireless/intel/iwlwifi/dvm/rx.c
@@ -915,7 +915,7 @@ static void iwlagn_rx_noa_notification(struct iwl_priv *priv,
 		len += 1 + 2;
 		copylen += 1 + 2;
 
-		new_data = kmalloc(sizeof(*new_data) + len, GFP_ATOMIC);
+		new_data = kmalloc(struct_size(*new_data, data, len), GFP_ATOMIC);
 		if (new_data) {
 			new_data->length = len;
 			new_data->data[0] = WLAN_EID_VENDOR_SPECIFIC;
-- 
2.25.1


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* Re: [PATCH v2] iwlwifi: dvm: use struct_size over open coded arithmetic
  2022-01-28  9:31   ` [PATCH v2] iwlwifi: " cgel.zte
@ 2022-01-28 12:34     ` Coelho, Luciano
  0 siblings, 0 replies; 5+ messages in thread
From: Coelho, Luciano @ 2022-01-28 12:34 UTC (permalink / raw)
  To: kvalo, cgel.zte
  Cc: zealci, davem, Berg, Johannes, chi.minghao, linux-kernel,
	linux-wireless, kuba, trix, netdev

On Fri, 2022-01-28 at 09:31 +0000, cgel.zte@gmail.com wrote:
> From: Minghao Chi (CGEL ZTE) <chi.minghao@zte.com.cn>
> 
> Replace zero-length array with flexible-array member and make use
> of the struct_size() helper in kmalloc(). For example:

You're not replacing zero-length arrays in this patch, so this is not
correct.  Probably a copy/paste from a different patch?

--
Cheers,
Luca.

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] dvm: use struct_size over open coded arithmetic
  2022-01-28  8:02 [PATCH] dvm: use struct_size over open coded arithmetic cgel.zte
  2022-01-28  9:01 ` Kalle Valo
@ 2022-01-28 21:03 ` kernel test robot
  1 sibling, 0 replies; 5+ messages in thread
From: kernel test robot @ 2022-01-28 21:03 UTC (permalink / raw)
  To: cgel.zte, luciano.coelho
  Cc: kbuild-all, kvalo, davem, kuba, trix, johannes.berg,
	linux-wireless, netdev, linux-kernel, Minghao Chi

Hi,

Thank you for the patch! Yet something to improve:

[auto build test ERROR on linus/master]
[also build test ERROR on v5.17-rc1 next-20220128]
[If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch]

url:    https://github.com/0day-ci/linux/commits/cgel-zte-gmail-com/dvm-use-struct_size-over-open-coded-arithmetic/20220128-160349
base:   https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git 23a46422c56144939c091c76cf389aa863ce9c18
config: arm-allyesconfig (https://download.01.org/0day-ci/archive/20220129/202201290256.JxMfdzDu-lkp@intel.com/config)
compiler: arm-linux-gnueabi-gcc (GCC) 11.2.0
reproduce (this is a W=1 build):
        wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross
        chmod +x ~/bin/make.cross
        # https://github.com/0day-ci/linux/commit/6ce283968f032a338616dbe570097f1639a66b75
        git remote add linux-review https://github.com/0day-ci/linux
        git fetch --no-tags linux-review cgel-zte-gmail-com/dvm-use-struct_size-over-open-coded-arithmetic/20220128-160349
        git checkout 6ce283968f032a338616dbe570097f1639a66b75
        # save the config file to linux build tree
        mkdir build_dir
        COMPILER_INSTALL_PATH=$HOME/0day COMPILER=gcc-11.2.0 make.cross O=build_dir ARCH=arm SHELL=/bin/bash

If you fix the issue, kindly add following tag as appropriate
Reported-by: kernel test robot <lkp@intel.com>

All errors (new ones prefixed by >>):

   In file included from include/linux/mm.h:30,
                    from arch/arm/include/asm/cacheflush.h:10,
                    from include/linux/cacheflush.h:5,
                    from include/linux/highmem.h:8,
                    from include/linux/bvec.h:10,
                    from include/linux/skbuff.h:17,
                    from include/linux/if_ether.h:19,
                    from include/linux/etherdevice.h:20,
                    from drivers/net/wireless/intel/iwlwifi/dvm/rx.c:12:
   drivers/net/wireless/intel/iwlwifi/dvm/rx.c: In function 'iwlagn_rx_noa_notification':
>> include/linux/overflow.h:194:32: error: invalid type argument of '->' (have 'struct iwl_wipan_noa_data')
     194 |                     sizeof(*(p)->member) + __must_be_array((p)->member),\
         |                                ^~
   drivers/net/wireless/intel/iwlwifi/dvm/rx.c:918:36: note: in expansion of macro 'struct_size'
     918 |                 new_data = kmalloc(struct_size(*new_data, data, len), GFP_ATOMIC);
         |                                    ^~~~~~~~~~~
   In file included from include/linux/container_of.h:5,
                    from include/linux/kernel.h:21,
                    from include/linux/skbuff.h:13,
                    from include/linux/if_ether.h:19,
                    from include/linux/etherdevice.h:20,
                    from drivers/net/wireless/intel/iwlwifi/dvm/rx.c:12:
   include/linux/overflow.h:194:63: error: invalid type argument of '->' (have 'struct iwl_wipan_noa_data')
     194 |                     sizeof(*(p)->member) + __must_be_array((p)->member),\
         |                                                               ^~
   include/linux/build_bug.h:16:62: note: in definition of macro 'BUILD_BUG_ON_ZERO'
      16 | #define BUILD_BUG_ON_ZERO(e) ((int)(sizeof(struct { int:(-!!(e)); })))
         |                                                              ^
   include/linux/compiler.h:258:51: note: in expansion of macro '__same_type'
     258 | #define __must_be_array(a)      BUILD_BUG_ON_ZERO(__same_type((a), &(a)[0]))
         |                                                   ^~~~~~~~~~~
   include/linux/overflow.h:194:44: note: in expansion of macro '__must_be_array'
     194 |                     sizeof(*(p)->member) + __must_be_array((p)->member),\
         |                                            ^~~~~~~~~~~~~~~
   drivers/net/wireless/intel/iwlwifi/dvm/rx.c:918:36: note: in expansion of macro 'struct_size'
     918 |                 new_data = kmalloc(struct_size(*new_data, data, len), GFP_ATOMIC);
         |                                    ^~~~~~~~~~~
   include/linux/overflow.h:194:63: error: invalid type argument of '->' (have 'struct iwl_wipan_noa_data')
     194 |                     sizeof(*(p)->member) + __must_be_array((p)->member),\
         |                                                               ^~
   include/linux/build_bug.h:16:62: note: in definition of macro 'BUILD_BUG_ON_ZERO'
      16 | #define BUILD_BUG_ON_ZERO(e) ((int)(sizeof(struct { int:(-!!(e)); })))
         |                                                              ^
   include/linux/compiler.h:258:51: note: in expansion of macro '__same_type'
     258 | #define __must_be_array(a)      BUILD_BUG_ON_ZERO(__same_type((a), &(a)[0]))
         |                                                   ^~~~~~~~~~~
   include/linux/overflow.h:194:44: note: in expansion of macro '__must_be_array'
     194 |                     sizeof(*(p)->member) + __must_be_array((p)->member),\
         |                                            ^~~~~~~~~~~~~~~
   drivers/net/wireless/intel/iwlwifi/dvm/rx.c:918:36: note: in expansion of macro 'struct_size'
     918 |                 new_data = kmalloc(struct_size(*new_data, data, len), GFP_ATOMIC);
         |                                    ^~~~~~~~~~~
   include/linux/build_bug.h:16:51: error: bit-field '<anonymous>' width not an integer constant
      16 | #define BUILD_BUG_ON_ZERO(e) ((int)(sizeof(struct { int:(-!!(e)); })))
         |                                                   ^
   include/linux/compiler.h:258:33: note: in expansion of macro 'BUILD_BUG_ON_ZERO'
     258 | #define __must_be_array(a)      BUILD_BUG_ON_ZERO(__same_type((a), &(a)[0]))
         |                                 ^~~~~~~~~~~~~~~~~
   include/linux/overflow.h:194:44: note: in expansion of macro '__must_be_array'
     194 |                     sizeof(*(p)->member) + __must_be_array((p)->member),\
         |                                            ^~~~~~~~~~~~~~~
   drivers/net/wireless/intel/iwlwifi/dvm/rx.c:918:36: note: in expansion of macro 'struct_size'
     918 |                 new_data = kmalloc(struct_size(*new_data, data, len), GFP_ATOMIC);
         |                                    ^~~~~~~~~~~
   In file included from include/linux/mm.h:30,
                    from arch/arm/include/asm/cacheflush.h:10,
                    from include/linux/cacheflush.h:5,
                    from include/linux/highmem.h:8,
                    from include/linux/bvec.h:10,
                    from include/linux/skbuff.h:17,
                    from include/linux/if_ether.h:19,
                    from include/linux/etherdevice.h:20,
                    from drivers/net/wireless/intel/iwlwifi/dvm/rx.c:12:
>> include/linux/overflow.h:195:28: error: invalid type argument of unary '*' (have 'struct iwl_wipan_noa_data')
     195 |                     sizeof(*(p)))
         |                            ^~~~
   drivers/net/wireless/intel/iwlwifi/dvm/rx.c:918:36: note: in expansion of macro 'struct_size'
     918 |                 new_data = kmalloc(struct_size(*new_data, data, len), GFP_ATOMIC);
         |                                    ^~~~~~~~~~~


vim +194 include/linux/overflow.h

610b15c50e86eb Kees Cook           2018-05-07  180  
610b15c50e86eb Kees Cook           2018-05-07  181  /**
610b15c50e86eb Kees Cook           2018-05-07  182   * struct_size() - Calculate size of structure with trailing array.
610b15c50e86eb Kees Cook           2018-05-07  183   * @p: Pointer to the structure.
610b15c50e86eb Kees Cook           2018-05-07  184   * @member: Name of the array member.
b19d57d0f3cc6f Gustavo A. R. Silva 2020-06-08  185   * @count: Number of elements in the array.
610b15c50e86eb Kees Cook           2018-05-07  186   *
610b15c50e86eb Kees Cook           2018-05-07  187   * Calculates size of memory needed for structure @p followed by an
b19d57d0f3cc6f Gustavo A. R. Silva 2020-06-08  188   * array of @count number of @member elements.
610b15c50e86eb Kees Cook           2018-05-07  189   *
610b15c50e86eb Kees Cook           2018-05-07  190   * Return: number of bytes needed or SIZE_MAX on overflow.
610b15c50e86eb Kees Cook           2018-05-07  191   */
b19d57d0f3cc6f Gustavo A. R. Silva 2020-06-08  192  #define struct_size(p, member, count)					\
b19d57d0f3cc6f Gustavo A. R. Silva 2020-06-08  193  	__ab_c_size(count,						\
610b15c50e86eb Kees Cook           2018-05-07 @194  		    sizeof(*(p)->member) + __must_be_array((p)->member),\
610b15c50e86eb Kees Cook           2018-05-07 @195  		    sizeof(*(p)))
610b15c50e86eb Kees Cook           2018-05-07  196  

---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/kbuild-all@lists.01.org

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2022-01-28 21:04 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-01-28  8:02 [PATCH] dvm: use struct_size over open coded arithmetic cgel.zte
2022-01-28  9:01 ` Kalle Valo
2022-01-28  9:31   ` [PATCH v2] iwlwifi: " cgel.zte
2022-01-28 12:34     ` Coelho, Luciano
2022-01-28 21:03 ` [PATCH] " kernel test robot

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).