From: Joe Perches Subject: Re: [PATCH bpf-next] bpf: btf: Avoid variable length array Date: Wed, 23 May 2018 11:11:14 -0700 Message-ID: <9c5a46d890dc5dbb65d1a2e6f9cb0d04a775add7.camel@perches.com> References: <20180523174659.354660-1-kafai@fb.com> Mime-Version: 1.0 Content-Type: text/plain; charset="ISO-8859-1" Content-Transfer-Encoding: 7bit Cc: Alexei Starovoitov , Daniel Borkmann , kernel-team@fb.com To: Martin KaFai Lau , netdev@vger.kernel.org Return-path: Received: from smtprelay0032.hostedemail.com ([216.40.44.32]:42599 "EHLO smtprelay.hostedemail.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1752282AbeEWSLS (ORCPT ); Wed, 23 May 2018 14:11:18 -0400 In-Reply-To: <20180523174659.354660-1-kafai@fb.com> Sender: netdev-owner@vger.kernel.org List-ID: On Wed, 2018-05-23 at 10:46 -0700, Martin KaFai Lau wrote: > Sparse warning: > kernel/bpf/btf.c:1985:34: warning: Variable length array is used. Perhaps use ARRAY_SIZE directly instead of indirectly via a #define > diff --git a/kernel/bpf/btf.c b/kernel/bpf/btf.c [] > @@ -1970,6 +1970,8 @@ static const size_t btf_sec_info_offset[] = { > offsetof(struct btf_header, str_off), > }; > > +#define NR_SECS ARRAY_SIZE(btf_sec_info_offset) > + > static int btf_sec_info_cmp(const void *a, const void *b) > { > const struct btf_sec_info *x = a; > @@ -1981,8 +1983,7 @@ static int btf_sec_info_cmp(const void *a, const void *b) > static int btf_check_sec_info(struct btf_verifier_env *env, > u32 btf_data_size) > { > - const unsigned int nr_secs = ARRAY_SIZE(btf_sec_info_offset); > - struct btf_sec_info secs[nr_secs]; > + struct btf_sec_info secs[NR_SECS]; struct btf_sec_info secs[ARRAY_SIZE(btf_sec_info_offset)]; > u32 total, expected_total, i; > const struct btf_header *hdr; > const struct btf *btf; > @@ -1991,17 +1992,17 @@ static int btf_check_sec_info(struct btf_verifier_env *env, > hdr = &btf->hdr; > > /* Populate the secs from hdr */ > - for (i = 0; i < nr_secs; i++) > + for (i = 0; i < NR_SECS; i++) for (i = 0; i < ARRAY_SIZE(btf_sec_info_offset); i++) > secs[i] = *(struct btf_sec_info *)((void *)hdr + > btf_sec_info_offset[i]); which makes this loop more intelligible. > - sort(secs, nr_secs, sizeof(struct btf_sec_info), > + sort(secs, NR_SECS, sizeof(struct btf_sec_info), > btf_sec_info_cmp, NULL); etc...