Received: by 2002:a25:23cc:0:0:0:0:0 with SMTP id j195csp1030067ybj; Thu, 7 May 2020 13:08:00 -0700 (PDT) X-Google-Smtp-Source: APiQypLjCFxPYXjupdbxcj1cW4cOphXPAKDfEDnUS5v/veK3pxqN7hoPot9d5xejvzpRrj/qTT7C X-Received: by 2002:a05:6402:379:: with SMTP id s25mr14116296edw.69.1588882080493; Thu, 07 May 2020 13:08:00 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1588882080; cv=none; d=google.com; s=arc-20160816; b=dw/AGwsX58Dhfib8pK7/Gp92vEKF9QjT5Yru0IjXWB1SYM18jfGstgPrIGdr69+aMN 0krm5cflSuGYmZBn12ztOrSdalWApreIWz/f7Ou7yKJ6llG5AVdFvC27WaQfQVIdJbXy HBjHMK/AYRqYYPTt6Uba2SrnQP6kHNKUQ50/6RcQRWASZcA/g+td3IPS3r8b3/DebjXX yuhtKw8SSqtqo4BKllshXUCw83g6/MbdUI8gSA+VOFTgTLdu0HzNIXkJcfOqvEXGiAN8 WX1C2v2qznQYm3Bha3RCNAgC6qSALFxLr+sR24pVOA5rqCxC6DLwqgJ5tzOyLnxTEGy4 TrVw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:in-reply-to:content-disposition :mime-version:references:message-id:subject:cc:to:from:date; bh=Ks+H4bfC/Oj/QJpVhFNzZgx8D+z8HMDq7CiCJCqjXec=; b=YogiKOdYhXZFZUNl6XAHkG4mjtvY3JU7zPSbDk6/9cTzWw5Gh1AB+xJYVtUlKeDLqG 2AXFD/7JvY9Pezc7hNeNIrviR4nhCFY2odMkbN+ecTn1iSHSsQsT5GQIpTy4NhghRk1U 1p+EfBADY1D0NFYReMxc/KDUHajfKV+dgD/dOaAnOwtam4XsW43kbamIoVk/NjhhwaGD ffooqJia7vYKhGlnnOTnfDETzdAWKlTb1+SHzOjjjWbnz9dfnF/Mj96YrXXY4EHcr5Xr DlZbS5ult7B7ywyx0zl+b6MwyrSOSzir6F8yVgtGf5OAGANNgjA63Pjccz5yBixWWntC pSkw== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id d24si3713495edy.387.2020.05.07.13.07.37; Thu, 07 May 2020 13:08:00 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728635AbgEGUEv (ORCPT + 99 others); Thu, 7 May 2020 16:04:51 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45150 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-FAIL-OK-FAIL) by vger.kernel.org with ESMTP id S1726320AbgEGUEt (ORCPT ); Thu, 7 May 2020 16:04:49 -0400 Received: from smtp.tuxdriver.com (tunnel92311-pt.tunnel.tserv13.ash1.ipv6.he.net [IPv6:2001:470:7:9c9::2]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id E5DC4C05BD43; Thu, 7 May 2020 13:04:48 -0700 (PDT) Received: from [107.15.85.130] (helo=localhost) by smtp.tuxdriver.com with esmtpsa (TLSv1:AES256-SHA:256) (Exim 4.63) (envelope-from ) id 1jWml4-0006aV-S7; Thu, 07 May 2020 16:04:44 -0400 Date: Thu, 7 May 2020 16:04:33 -0400 From: Neil Horman To: "Gustavo A. R. Silva" Cc: Vlad Yasevich , Marcelo Ricardo Leitner , linux-sctp@vger.kernel.org, linux-kernel@vger.kernel.org Subject: Re: [PATCH] sctp: Replace zero-length array with flexible-array Message-ID: <20200507200433.GA2838429@hmswarspite.think-freely.org> References: <20200507192152.GA16230@embeddedor> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20200507192152.GA16230@embeddedor> X-Spam-Score: -2.9 (--) X-Spam-Status: No Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Thu, May 07, 2020 at 02:21:52PM -0500, Gustavo A. R. Silva wrote: > The current codebase makes use of the zero-length array language > extension to the C90 standard, but the preferred mechanism to declare > variable-length types such as these ones is a flexible array member[1][2], > introduced in C99: > > struct foo { > int stuff; > struct boo array[]; > }; > > By making use of the mechanism above, we will get a compiler warning > in case the flexible array does not occur last in the structure, which > will help us prevent some kind of undefined behavior bugs from being > inadvertently introduced[3] to the codebase from now on. > > Also, notice that, dynamic memory allocations won't be affected by > this change: > > "Flexible array members have incomplete type, and so the sizeof operator > may not be applied. As a quirk of the original implementation of > zero-length arrays, sizeof evaluates to zero."[1] > > sizeof(flexible-array-member) triggers a warning because flexible array > members have incomplete type[1]. There are some instances of code in > which the sizeof operator is being incorrectly/erroneously applied to > zero-length arrays and the result is zero. Such instances may be hiding > some bugs. So, this work (flexible-array member conversions) will also > help to get completely rid of those sorts of issues. > > This issue was found with the help of Coccinelle. > > [1] https://gcc.gnu.org/onlinedocs/gcc/Zero-Length.html > [2] https://github.com/KSPP/linux/issues/21 > [3] commit 76497732932f ("cxgb3/l2t: Fix undefined behaviour") > > Signed-off-by: Gustavo A. R. Silva > --- > include/linux/sctp.h | 36 ++++++++++++++++++------------------ > 1 file changed, 18 insertions(+), 18 deletions(-) > > diff --git a/include/linux/sctp.h b/include/linux/sctp.h > index 8ccd82105de8..76731230bbc5 100644 > --- a/include/linux/sctp.h > +++ b/include/linux/sctp.h > @@ -221,7 +221,7 @@ struct sctp_datahdr { > __be16 stream; > __be16 ssn; > __u32 ppid; > - __u8 payload[0]; > + __u8 payload[]; > }; > > struct sctp_data_chunk { > @@ -269,7 +269,7 @@ struct sctp_inithdr { > __be16 num_outbound_streams; > __be16 num_inbound_streams; > __be32 initial_tsn; > - __u8 params[0]; > + __u8 params[]; > }; > > struct sctp_init_chunk { > @@ -299,13 +299,13 @@ struct sctp_cookie_preserve_param { > /* Section 3.3.2.1 Host Name Address (11) */ > struct sctp_hostname_param { > struct sctp_paramhdr param_hdr; > - uint8_t hostname[0]; > + uint8_t hostname[]; > }; > > /* Section 3.3.2.1 Supported Address Types (12) */ > struct sctp_supported_addrs_param { > struct sctp_paramhdr param_hdr; > - __be16 types[0]; > + __be16 types[]; > }; > > /* ADDIP Section 3.2.6 Adaptation Layer Indication */ > @@ -317,25 +317,25 @@ struct sctp_adaptation_ind_param { > /* ADDIP Section 4.2.7 Supported Extensions Parameter */ > struct sctp_supported_ext_param { > struct sctp_paramhdr param_hdr; > - __u8 chunks[0]; > + __u8 chunks[]; > }; > > /* AUTH Section 3.1 Random */ > struct sctp_random_param { > struct sctp_paramhdr param_hdr; > - __u8 random_val[0]; > + __u8 random_val[]; > }; > > /* AUTH Section 3.2 Chunk List */ > struct sctp_chunks_param { > struct sctp_paramhdr param_hdr; > - __u8 chunks[0]; > + __u8 chunks[]; > }; > > /* AUTH Section 3.3 HMAC Algorithm */ > struct sctp_hmac_algo_param { > struct sctp_paramhdr param_hdr; > - __be16 hmac_ids[0]; > + __be16 hmac_ids[]; > }; > > /* RFC 2960. Section 3.3.3 Initiation Acknowledgement (INIT ACK) (2): > @@ -350,7 +350,7 @@ struct sctp_initack_chunk { > /* Section 3.3.3.1 State Cookie (7) */ > struct sctp_cookie_param { > struct sctp_paramhdr p; > - __u8 body[0]; > + __u8 body[]; > }; > > /* Section 3.3.3.1 Unrecognized Parameters (8) */ > @@ -384,7 +384,7 @@ struct sctp_sackhdr { > __be32 a_rwnd; > __be16 num_gap_ack_blocks; > __be16 num_dup_tsns; > - union sctp_sack_variable variable[0]; > + union sctp_sack_variable variable[]; > }; > > struct sctp_sack_chunk { > @@ -436,7 +436,7 @@ struct sctp_shutdown_chunk { > struct sctp_errhdr { > __be16 cause; > __be16 length; > - __u8 variable[0]; > + __u8 variable[]; > }; > > struct sctp_operr_chunk { > @@ -594,7 +594,7 @@ struct sctp_fwdtsn_skip { > > struct sctp_fwdtsn_hdr { > __be32 new_cum_tsn; > - struct sctp_fwdtsn_skip skip[0]; > + struct sctp_fwdtsn_skip skip[]; > }; > > struct sctp_fwdtsn_chunk { > @@ -611,7 +611,7 @@ struct sctp_ifwdtsn_skip { > > struct sctp_ifwdtsn_hdr { > __be32 new_cum_tsn; > - struct sctp_ifwdtsn_skip skip[0]; > + struct sctp_ifwdtsn_skip skip[]; > }; > > struct sctp_ifwdtsn_chunk { > @@ -658,7 +658,7 @@ struct sctp_addip_param { > > struct sctp_addiphdr { > __be32 serial; > - __u8 params[0]; > + __u8 params[]; > }; > > struct sctp_addip_chunk { > @@ -718,7 +718,7 @@ struct sctp_addip_chunk { > struct sctp_authhdr { > __be16 shkey_id; > __be16 hmac_id; > - __u8 hmac[0]; > + __u8 hmac[]; > }; > > struct sctp_auth_chunk { > @@ -733,7 +733,7 @@ struct sctp_infox { > > struct sctp_reconf_chunk { > struct sctp_chunkhdr chunk_hdr; > - __u8 params[0]; > + __u8 params[]; > }; > > struct sctp_strreset_outreq { > @@ -741,13 +741,13 @@ struct sctp_strreset_outreq { > __be32 request_seq; > __be32 response_seq; > __be32 send_reset_at_tsn; > - __be16 list_of_streams[0]; > + __be16 list_of_streams[]; > }; > > struct sctp_strreset_inreq { > struct sctp_paramhdr param_hdr; > __be32 request_seq; > - __be16 list_of_streams[0]; > + __be16 list_of_streams[]; > }; > > struct sctp_strreset_tsnreq { > > Acked-by: Neil Horman