linux-wireless.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* drivers/net/wireless/rt2x00/: struct data_desc strangeness
@ 2007-10-14 17:50 Adrian Bunk
  2007-10-14 19:06 ` Ivo van Doorn
  0 siblings, 1 reply; 4+ messages in thread
From: Adrian Bunk @ 2007-10-14 17:50 UTC (permalink / raw)
  To: Ivo van Doorn, John W. Linville; +Cc: linux-wireless, linux-kernel

drivers/net/wireless/rt2x00/rt2x00ring.h contains the following:

<--  snip  -->

...
/*
 * data_desc
 * Each data entry also contains a descriptor which is used by the
 * device to determine what should be done with the packet and
 * what the current status is.
 * This structure is greatly simplified, but the descriptors
 * are basically a list of little endian 32 bit values.
 * Make the array by default 1 word big, this will allow us
 * to use sizeof() correctly.
 */
struct data_desc {
        __le32 word[1];
};
...
/*
 * TX/RX Descriptor access functions.
 */
static inline void rt2x00_desc_read(struct data_desc *desc,
                                    const u8 word, u32 *value)
{
        *value = le32_to_cpu(desc->word[word]);
}

static inline void rt2x00_desc_write(struct data_desc *desc,
                                     const u8 word, const u32 value)
{
        desc->word[word] = cpu_to_le32(value);
}
...

<--  snip  -->

I haven't checked whether it might work in all cases, but passing 
non-zero values as second parameter to rt2x00_desc_{read,write}()
(as is done in many cases) is even in the best case bad coding style.

Spotted by the Coverity checker.

cu
Adrian

-- 

       "Is there not promise of rain?" Ling Tan asked suddenly out
        of the darkness. There had been need of rain for many days.
       "Only a promise," Lao Er said.
                                       Pearl S. Buck - Dragon Seed


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

* Re: drivers/net/wireless/rt2x00/: struct data_desc strangeness
  2007-10-14 17:50 drivers/net/wireless/rt2x00/: struct data_desc strangeness Adrian Bunk
@ 2007-10-14 19:06 ` Ivo van Doorn
  2007-10-23 19:43   ` Adrian Bunk
  0 siblings, 1 reply; 4+ messages in thread
From: Ivo van Doorn @ 2007-10-14 19:06 UTC (permalink / raw)
  To: Adrian Bunk; +Cc: John W. Linville, linux-wireless, linux-kernel

On Sunday 14 October 2007, Adrian Bunk wrote:
> drivers/net/wireless/rt2x00/rt2x00ring.h contains the following:
> 
> <--  snip  -->
> 
> ...
> /*
>  * data_desc
>  * Each data entry also contains a descriptor which is used by the
>  * device to determine what should be done with the packet and
>  * what the current status is.
>  * This structure is greatly simplified, but the descriptors
>  * are basically a list of little endian 32 bit values.
>  * Make the array by default 1 word big, this will allow us
>  * to use sizeof() correctly.
>  */
> struct data_desc {
>         __le32 word[1];
> };
> ...
> /*
>  * TX/RX Descriptor access functions.
>  */
> static inline void rt2x00_desc_read(struct data_desc *desc,
>                                     const u8 word, u32 *value)
> {
>         *value = le32_to_cpu(desc->word[word]);
> }
> 
> static inline void rt2x00_desc_write(struct data_desc *desc,
>                                      const u8 word, const u32 value)
> {
>         desc->word[word] = cpu_to_le32(value);
> }
> ...
> 
> <--  snip  -->
> 
> I haven't checked whether it might work in all cases, but passing 
> non-zero values as second parameter to rt2x00_desc_{read,write}()
> (as is done in many cases) is even in the best case bad coding style.

Access to the array is correct, even with non-zero values the code that is
reading/writing to the array knows the exact size of the descriptor. Within
rt2x00 are however 5 drivers who have different descriptor sizes. That means
I can't create a structure which has the correct array length.

The structure itself is just a simple map over preallocated memory
(skb->data in case of USB or dma in case of PCI).

So possible solutions would be:
 - remove struct data_desc and make it a void* or __le32*
   This is at the cost of code readibility since the above pointers
   have less meaning then a pointer to a structure which can be nicely
   documented.
 - increase the word[] array to something that fits all (+/- 20 entries)
   This wouldn't really be a problem, all it requires is fixing the sizeof() 
   statements. But then the code should contain a big note about that it
   is not allowed to read/write _all_ words in the entry since it depends on
   the driver that uses it.

What would in this case be the best solution for good coding style?

Ivo

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

* Re: drivers/net/wireless/rt2x00/: struct data_desc strangeness
  2007-10-14 19:06 ` Ivo van Doorn
@ 2007-10-23 19:43   ` Adrian Bunk
  2007-10-23 20:13     ` Ivo van Doorn
  0 siblings, 1 reply; 4+ messages in thread
From: Adrian Bunk @ 2007-10-23 19:43 UTC (permalink / raw)
  To: Ivo van Doorn; +Cc: John W. Linville, linux-wireless, linux-kernel

On Sun, Oct 14, 2007 at 09:06:20PM +0200, Ivo van Doorn wrote:
> On Sunday 14 October 2007, Adrian Bunk wrote:
> > drivers/net/wireless/rt2x00/rt2x00ring.h contains the following:
> > 
> > <--  snip  -->
> > 
> > ...
> > /*
> >  * data_desc
> >  * Each data entry also contains a descriptor which is used by the
> >  * device to determine what should be done with the packet and
> >  * what the current status is.
> >  * This structure is greatly simplified, but the descriptors
> >  * are basically a list of little endian 32 bit values.
> >  * Make the array by default 1 word big, this will allow us
> >  * to use sizeof() correctly.
> >  */
> > struct data_desc {
> >         __le32 word[1];
> > };
> > ...
> > /*
> >  * TX/RX Descriptor access functions.
> >  */
> > static inline void rt2x00_desc_read(struct data_desc *desc,
> >                                     const u8 word, u32 *value)
> > {
> >         *value = le32_to_cpu(desc->word[word]);
> > }
> > 
> > static inline void rt2x00_desc_write(struct data_desc *desc,
> >                                      const u8 word, const u32 value)
> > {
> >         desc->word[word] = cpu_to_le32(value);
> > }
> > ...
> > 
> > <--  snip  -->
> > 
> > I haven't checked whether it might work in all cases, but passing 
> > non-zero values as second parameter to rt2x00_desc_{read,write}()
> > (as is done in many cases) is even in the best case bad coding style.
> 
> Access to the array is correct, even with non-zero values the code that is
> reading/writing to the array knows the exact size of the descriptor. Within
> rt2x00 are however 5 drivers who have different descriptor sizes. That means
> I can't create a structure which has the correct array length.
> 
> The structure itself is just a simple map over preallocated memory
> (skb->data in case of USB or dma in case of PCI).
> 
> So possible solutions would be:
>  - remove struct data_desc and make it a void* or __le32*
>    This is at the cost of code readibility since the above pointers
>    have less meaning then a pointer to a structure which can be nicely
>    documented.
>...

The worst is a wrong meaning.
__le32 word[1] is an array with _one_ element.

And an __le32* can be used as an array.

> Ivo

cu
Adrian

-- 

       "Is there not promise of rain?" Ling Tan asked suddenly out
        of the darkness. There had been need of rain for many days.
       "Only a promise," Lao Er said.
                                       Pearl S. Buck - Dragon Seed


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

* Re: drivers/net/wireless/rt2x00/: struct data_desc strangeness
  2007-10-23 19:43   ` Adrian Bunk
@ 2007-10-23 20:13     ` Ivo van Doorn
  0 siblings, 0 replies; 4+ messages in thread
From: Ivo van Doorn @ 2007-10-23 20:13 UTC (permalink / raw)
  To: Adrian Bunk; +Cc: John W. Linville, linux-wireless, linux-kernel

Hi,

> > > I haven't checked whether it might work in all cases, but passing 
> > > non-zero values as second parameter to rt2x00_desc_{read,write}()
> > > (as is done in many cases) is even in the best case bad coding style.
> > 
> > Access to the array is correct, even with non-zero values the code that is
> > reading/writing to the array knows the exact size of the descriptor. Within
> > rt2x00 are however 5 drivers who have different descriptor sizes. That means
> > I can't create a structure which has the correct array length.
> > 
> > The structure itself is just a simple map over preallocated memory
> > (skb->data in case of USB or dma in case of PCI).
> > 
> > So possible solutions would be:
> >  - remove struct data_desc and make it a void* or __le32*
> >    This is at the cost of code readibility since the above pointers
> >    have less meaning then a pointer to a structure which can be nicely
> >    documented.
> >...
> 
> The worst is a wrong meaning.
> __le32 word[1] is an array with _one_ element.
> 
> And an __le32* can be used as an array.

Ok. I'll fix this in 1 or 2 days.

Ivo

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

end of thread, other threads:[~2007-10-23 19:54 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2007-10-14 17:50 drivers/net/wireless/rt2x00/: struct data_desc strangeness Adrian Bunk
2007-10-14 19:06 ` Ivo van Doorn
2007-10-23 19:43   ` Adrian Bunk
2007-10-23 20:13     ` Ivo van Doorn

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