linux-clk.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* re: clk: stm32: Add clock driver for STM32F4[23]xxx devices
@ 2015-06-26 10:31 Dan Carpenter
  2015-06-26 10:53 ` Daniel Thompson
  0 siblings, 1 reply; 3+ messages in thread
From: Dan Carpenter @ 2015-06-26 10:31 UTC (permalink / raw)
  To: daniel.thompson; +Cc: linux-clk

Hello Daniel Thompson,

The patch 358bdf892f6b: "clk: stm32: Add clock driver for
STM32F4[23]xxx devices" from Jun 10, 2015, leads to the following
static checker warning:

	drivers/clk/clk-stm32f4.c:271 stm32f4_rcc_lookup_clk_idx()
	warn: buffer overflow 'table' 3 <= 3

drivers/clk/clk-stm32f4.c
   258  static int stm32f4_rcc_lookup_clk_idx(u8 primary, u8 secondary)
   259  {
   260          u64 table[ARRAY_SIZE(stm32f42xx_gate_map)];
   261  
   262          if (primary == 1) {
   263                  if (WARN_ON(secondary > FCLK))

Is this off by one? >= instead of >?

   264                          return -EINVAL;
   265                  return secondary;
   266          }
   267  
   268          memcpy(table, stm32f42xx_gate_map, sizeof(table));
   269  
   270          /* only bits set in table can be used as indices */
   271          if (WARN_ON(secondary > 8 * sizeof(table) ||

This one is definitely off by one.  Also it might be easier to read as:

		if (WARN_ON(secondary >= 64 * ARRAY_SIZE(table) ||

   272                      0 == (table[BIT_ULL_WORD(secondary)] &
   273                            BIT_ULL_MASK(secondary))))
   274                  return -EINVAL;
   275  
   276          /* mask out bits above our current index */
   277          table[BIT_ULL_WORD(secondary)] &=
   278              GENMASK_ULL(secondary % BITS_PER_LONG_LONG, 0);
   279  
   280          return FCLK + hweight64(table[0]) +
   281                 (BIT_ULL_WORD(secondary) >= 1 ? hweight64(table[1]) : 0) +
   282                 (BIT_ULL_WORD(secondary) >= 2 ? hweight64(table[2]) : 0);
   283  }

regards,
dan carpenter

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

* Re: clk: stm32: Add clock driver for STM32F4[23]xxx devices
  2015-06-26 10:31 clk: stm32: Add clock driver for STM32F4[23]xxx devices Dan Carpenter
@ 2015-06-26 10:53 ` Daniel Thompson
  2015-06-26 10:56   ` Dan Carpenter
  0 siblings, 1 reply; 3+ messages in thread
From: Daniel Thompson @ 2015-06-26 10:53 UTC (permalink / raw)
  To: Dan Carpenter; +Cc: linux-clk

On 26/06/15 11:31, Dan Carpenter wrote:
> Hello Daniel Thompson,
>
> The patch 358bdf892f6b: "clk: stm32: Add clock driver for
> STM32F4[23]xxx devices" from Jun 10, 2015, leads to the following
> static checker warning:
>
> 	drivers/clk/clk-stm32f4.c:271 stm32f4_rcc_lookup_clk_idx()
> 	warn: buffer overflow 'table' 3 <= 3
>
> drivers/clk/clk-stm32f4.c
>     258  static int stm32f4_rcc_lookup_clk_idx(u8 primary, u8 secondary)
>     259  {
>     260          u64 table[ARRAY_SIZE(stm32f42xx_gate_map)];
>     261
>     262          if (primary == 1) {
>     263                  if (WARN_ON(secondary > FCLK))
>
> Is this off by one? >= instead of >?

This one is ok (FCLK is a valid value for secondary when primary is 1).


>     264                          return -EINVAL;
>     265                  return secondary;
>     266          }
>     267
>     268          memcpy(table, stm32f42xx_gate_map, sizeof(table));
>     269
>     270          /* only bits set in table can be used as indices */
>     271          if (WARN_ON(secondary > 8 * sizeof(table) ||
>
> This one is definitely off by one.

Agreed.


 > Also it might be easier to read as:
> 		if (WARN_ON(secondary >= 64 * ARRAY_SIZE(table) ||

I guess the thing we want to be clear is that we are comparing against 
the number of bits in table.

Maybe just "BITS_PER_BYTE * sizeof(table)" achieves that best?

BTW... you're happy for me to send a patch on this (with a Reported-by:)?


Daniel.

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

* Re: clk: stm32: Add clock driver for STM32F4[23]xxx devices
  2015-06-26 10:53 ` Daniel Thompson
@ 2015-06-26 10:56   ` Dan Carpenter
  0 siblings, 0 replies; 3+ messages in thread
From: Dan Carpenter @ 2015-06-26 10:56 UTC (permalink / raw)
  To: Daniel Thompson; +Cc: linux-clk

On Fri, Jun 26, 2015 at 11:53:33AM +0100, Daniel Thompson wrote:
> Maybe just "BITS_PER_BYTE * sizeof(table)" achieves that best?
> 

That works.

> BTW... you're happy for me to send a patch on this (with a Reported-by:)?

Yes, please.

regards,
dan carpenter


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

end of thread, other threads:[~2015-06-26 10:56 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2015-06-26 10:31 clk: stm32: Add clock driver for STM32F4[23]xxx devices Dan Carpenter
2015-06-26 10:53 ` Daniel Thompson
2015-06-26 10:56   ` Dan Carpenter

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).