staging: rtl8723bs: avoid duplicate size computation in rtw_spt_band_alloc
PATCH 1/1 staging: rtl8723bs: avoid duplicate size computation in rtw_spt_band_alloc
Introduce a local variable to store the channel array size and reuse it for both the allocation and the bitrates pointer offset, replacing a redundant open-coded multiplication with the same array_size() result.
Resolves the Coccinelle warning:
WARNING: array_size is already used (line 124) to compute the same size
--- drivers/staging/rtl8723bs/os_dep/ioctl_cfg80211.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-)
drivers/staging/rtl8723bs/os_dep/ioctl_cfg80211.c +4 -3
@@ -111,7 +111,7 @@ static struct ieee80211_supported_band *rtw_spt_band_alloc( {struct ieee80211_supported_band *spt_band =NULL ;int n_channels, n_bitrates; -size_t alloc_sz; +size_t alloc_sz, channels_sz;if (band ==NL80211_BAND_2GHZ ) { n_channels =RTW_2G_CHANNELS_NUM ;
@@ -120,15 +120,16 @@ static struct ieee80211_supported_band *rtw_spt_band_alloc( goto exit; } + channels_sz =array_size (n_channels,sizeof (struct ieee80211_channel )); alloc_sz =sizeof (*spt_band); - alloc_sz =size_add (alloc_sz,array_size (n_channels,sizeof (struct ieee80211_channel ))); + alloc_sz =size_add (alloc_sz, channels_sz); alloc_sz =size_add (alloc_sz,array_size (n_bitrates,sizeof (struct ieee80211_rate ))); spt_band =kzalloc (alloc_sz,GFP_KERNEL );if (!spt_band) goto exit; spt_band->channels = (struct ieee80211_channel *)(((u8 *)spt_band) +sizeof (struct ieee80211_supported_band )); - spt_band->bitrates = (struct ieee80211_rate *)(((u8 *)spt_band->channels) +sizeof (struct ieee80211_channel ) * n_channels); + spt_band->bitrates = (struct ieee80211_rate *)(((u8 *)spt_band->channels) + channels_sz); spt_band->band = band; spt_band->n_channels = n_channels; spt_band->n_bitrates = n_bitrates;
Signature · 1 line
2.54.0
Quoted 26 lines · Serhat Kumral
Introduce a local variable to store the channel array size and reuse it for both the allocation and the bitrates pointer offset, replacing a redundant open-coded multiplication with the same
array_size()result.Resolves the Coccinelle warning:
WARNING:array_sizeis already used (line 124) to compute the same sizeSigned-off-by Serhat Kumral <[email protected]>--- drivers/staging/rtl8723bs/os_dep/ioctl_cfg80211.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-)drivers/staging/rtl8723bs/os_dep/ioctl_cfg80211.c +1
-1@@ -111,7 +111,7 @@ static struct ieee80211_supported_band *rtw_spt_band_alloc( {struct ieee80211_supported_band *spt_band =NULL ;int n_channels, n_bitrates; -size_t alloc_sz; +size_t alloc_sz, channels_sz;if (band ==NL80211_BAND_2GHZ ) { n_channels =RTW_2G_CHANNELS_NUM ;
drivers/staging/rtl8723bs/os_dep/ioctl_cfg80211.c static struct ieee80211_supported_band *rtw_spt_band_alloc( · line 127
Quoted 9 lines
drivers/staging/rtl8723bs/os_dep/ioctl_cfg80211.c +2
-1@@ -120,15 +120,16 @@ static struct ieee80211_supported_band *rtw_spt_band_alloc( goto exit; } + channels_sz =array_size (n_channels,sizeof (struct ieee80211_channel )); alloc_sz =sizeof (*spt_band); - alloc_sz =size_add (alloc_sz,array_size (n_channels,sizeof (struct ieee80211_channel ))); + alloc_sz =size_add (alloc_sz, channels_sz); alloc_sz =size_add (alloc_sz,array_size (n_bitrates,sizeof (struct ieee80211_rate ))); spt_band =kzalloc (alloc_sz,GFP_KERNEL );
This is still a mess. First of all these size values are constant so the layers of indirection are unnecessary. Pretending they are variable makes code auditors have to check for integer overflows so it wastes valuable time and makes the whole kernel less secure.
channels_sz = array_size(RTW_2G_CHANNELS_NUM, sizeof(struct ieee80211_channel)); bitrate_size = array_size(RTW_G_RATES_NUM, sizeof(struct ieee80211_rate)); spt_band = kzalloc(sizeof(*spt_band) + channels_sz + bitrate_size, GFP_KERNEL); spt_band->channels = (void *)spt_band + sizeof(*spt_band); spt_band->bitrates = (void *)spt_band + sizeof(*spt_band) + channels_sz;
regards,
dan carpenter