diff mbox series

RDMA: Replace zero-length array with flexible-array member

Message ID 20220401075406.2407294-1-lv.ruyi@zte.com.cn (mailing list archive)
State Rejected
Delegated to: Jason Gunthorpe
Headers show
Series RDMA: Replace zero-length array with flexible-array member | expand

Commit Message

CGEL April 1, 2022, 7:54 a.m. UTC
From: Lv Ruyi <lv.ruyi@zte.com.cn>

There is a regular need in the kernel to provide a way to declare
having a dynamically sized set of trailing elements in a structure.
Kernel code should always use “flexible array members”[1] for these
cases. The older style of one-element or zero-length arrays should
no longer be used[2].

[1] https://en.wikipedia.org/wiki/Flexible_array_member
[2] https://www.kernel.org/doc/html/v5.16/process/deprecated.html#zero-length-and-one-element-arrays

Reported-by: Zeal Robot <zealci@zte.com.cn>
Signed-off-by: Lv Ruyi <lv.ruyi@zte.com.cn>
---
 drivers/infiniband/hw/hfi1/mad.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

Comments

Jason Gunthorpe April 1, 2022, 11:50 a.m. UTC | #1
On Fri, Apr 01, 2022 at 07:54:06AM +0000, cgel.zte@gmail.com wrote:
> From: Lv Ruyi <lv.ruyi@zte.com.cn>
> 
> There is a regular need in the kernel to provide a way to declare
> having a dynamically sized set of trailing elements in a structure.
> Kernel code should always use “flexible array members”[1] for these
> cases. The older style of one-element or zero-length arrays should
> no longer be used[2].

If I recall properly this doesn't work if the flex array is nested
inside another member.. Not sure what this thinks it is doing anyhow

Jason
Dennis Dalessandro April 1, 2022, 1:32 p.m. UTC | #2
On 4/1/22 7:50 AM, Jason Gunthorpe wrote:
> On Fri, Apr 01, 2022 at 07:54:06AM +0000, cgel.zte@gmail.com wrote:
>> From: Lv Ruyi <lv.ruyi@zte.com.cn>
>>
>> There is a regular need in the kernel to provide a way to declare
>> having a dynamically sized set of trailing elements in a structure.
>> Kernel code should always use “flexible array members”[1] for these
>> cases. The older style of one-element or zero-length arrays should
>> no longer be used[2].
> 
> If I recall properly this doesn't work if the flex array is nested
> inside another member.. Not sure what this thinks it is doing anyhow

I was under the impression that the main goal of using the flex array was so
that people didn't calculate sizes incorrectly. Perhaps I'm wrong though.
I don't really see this being needed. If it even works when nested as Jason
mentioned.

Now that being said there is some clean up that needs to be done to this code,
but it's not this.

-Denny
diff mbox series

Patch

diff --git a/drivers/infiniband/hw/hfi1/mad.c b/drivers/infiniband/hw/hfi1/mad.c
index 4146a2113a95..09fe0eb87884 100644
--- a/drivers/infiniband/hw/hfi1/mad.c
+++ b/drivers/infiniband/hw/hfi1/mad.c
@@ -2437,7 +2437,7 @@  struct opa_port_data_counters_msg {
 			__be64 port_vl_xmit_wait_data;
 			__be64 port_vl_rcv_bubble;
 			__be64 port_vl_mark_fecn;
-		} vls[0];
+		} vls[];
 		/* array size defined by #bits set in vl_select_mask*/
 	} port[1]; /* array size defined by  #ports in attribute modifier */
 };
@@ -2470,7 +2470,7 @@  struct opa_port_error_counters64_msg {
 		u8 reserved3[7];
 		struct _vls_ectrs {
 			__be64 port_vl_xmit_discards;
-		} vls[0];
+		} vls[];
 		/* array size defined by #bits set in vl_select_mask */
 	} port[1]; /* array size defined by #ports in attribute modifier */
 };