Skip to contentSkip to conversation

bpf: btf: Avoid variable length array

PATCH Superseded Martin KaFai Lau 2 participants 2 messages May 23, 2018

Subsystems (3)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 bpf: btf: Avoid variable length array

Sparse warning:
kernel/bpf/btf.c:1985:34: warning: Variable length array is used.

This patch moves the nr_secs from btf_check_sec_info() to a macro.

Fixes f80442a4cd18 ("bpf: btf: Change how section is supported in btf_header")
---
 kernel/bpf/btf.c | 11 ++++++-----
 1 file changed, 6 insertions(+), 5 deletions(-)
kernel/bpf/btf.c +6 -5
index 9cbeabb5aca3..517296712774 100644
@@ -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];
 	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++)
 		secs[i] = *(struct btf_sec_info *)((void *)hdr +
 						   btf_sec_info_offset[i]);
 
-	sort(secs, nr_secs, sizeof(struct btf_sec_info),
+	sort(secs, NR_SECS, sizeof(struct btf_sec_info),
 	     btf_sec_info_cmp, NULL);
 
 	/* Check for gaps and overlap among sections */
 	total = 0;
 	expected_total = btf_data_size - hdr->hdr_len;
-	for (i = 0; i < nr_secs; i++) {
+	for (i = 0; i < NR_SECS; i++) {
 		if (expected_total < secs[i].off) {
 			btf_verifier_log(env, "Invalid section offset");
 			return -EINVAL;
Signature · 1 line
2.9.5
#1 lore raw
Quoted 2 lines · Martin KaFai Lau

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

kernel/bpf/btf.c

Quoted 1 line
kernel/bpf/btf.c +0 -0

[]

Quoted 10 lines
kernel/bpf/btf.c +2 -0
@@ -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;

kernel/bpf/btf.c static int btf_sec_info_cmp(const void *a, const void *b) · line 1986

Quoted 6 lines
kernel/bpf/btf.c +1 -2
@@ -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)];

kernel/bpf/btf.c static int btf_check_sec_info(struct btf_verifier_env *env, · line 1995

Quoted 9 lines
kernel/bpf/btf.c +1 -1
 	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++)

kernel/bpf/btf.c static int btf_check_sec_info(struct btf_verifier_env *env, · line 1997

Quoted 2 lines
kernel/bpf/btf.c +0 -0
 		secs[i] = *(struct btf_sec_info *)((void *)hdr +
 						   btf_sec_info_offset[i]);

which makes this loop more intelligible.

kernel/bpf/btf.c static int btf_check_sec_info(struct btf_verifier_env *env, · line 2000

Quoted 3 lines
kernel/bpf/btf.c +1 -1
-	sort(secs, nr_secs, sizeof(struct btf_sec_info),
+	sort(secs, NR_SECS, sizeof(struct btf_sec_info),
 	     btf_sec_info_cmp, NULL);

etc...